1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Http\Middleware;
- use App\Helpers\Jwt;
- use App\Services\RedisService;
- use App\Services\ConfigService;
- use Closure;
- use Illuminate\Auth\Middleware\Authenticate as Middleware;
- class WebLogin extends Middleware
- {
- /**
- * 执行句柄
- * @param \Illuminate\Http\Request $request
- * @param Closure $next
- * @param mixed ...$guards
- * @return mixed
- * @throws \Illuminate\Auth\AuthenticationException
- * @since 2020/8/31
- * @author wesmiler
- */
- public function handle($request, Closure $next, ...$guards)
- {
- $action = app('request')->route()->getAction();
- $controller = class_basename($action['controller']);
- list($controller, $action) = explode('@', $controller);
- $noLoginActs = ['LoginController','TestController','NotifyController','IndexController','ArticleController','UploadController','TaskController'];
- $noSignActions = ['UploadController','setAvatar','NotifyController','TestController','TaskController'];
- $token = $request->headers->get('Authorization');
- if (strpos($token, 'Bearer ') !== false) {
- $token = str_replace("Bearer ", null, $token);
- $token = trim($token);
- if($token == 'app123'){
- $userId = ConfigService::make()->getConfigByCode('test_uid');
- $userId = $userId? $userId : 0;
- }else{
- // JWT解密token
- $jwt = new Jwt('jwt_rrc_app');
- $userId = $jwt->verifyToken($token);
- }
- } else {
- $userId = 0;
- }
- // 接口验证
- $sign = $request->headers->get('sign');
- $params = $request->except('s');
- $checkSign = getSign($params);
- if($sign != 'test6688' && !in_array($action, $noSignActions) && !in_array($controller, $noSignActions)) {
- // if ($sign != $checkSign) {
- // return response()->json(message(1005, false, [], 403))->setEncodingOptions(256);
- // }
- $time = isset($params['time']) ? $params['time'] : 0;
- if ($time && $time < time() - 20) {
- return response()->json(message(1012, false, null, 403))->setEncodingOptions(256);
- }
- }
- if (!$userId && !in_array($controller, $noLoginActs)) {
- // 判断用户未登录就跳转至登录页面
- // 在这里可以定制你想要的返回格式, 亦或者是 JSON 编码格式
- return response()->json(message(1004, false, [], 403))->setEncodingOptions(256);
- }
- $request->headers->set('token_uid' , $userId);
- //如果已登录则执行正常的请求
- return $next($request);
- }
- }
|