添加鉴权中间件

master
李春波 2 years ago
parent 323b712aec
commit 230792fc3b
  1. 2
      app/admin/controller/AuthController.php
  2. 5
      app/admin/middleware/AuthCheck.php
  3. 3
      composer.json
  4. 37
      composer.lock
  5. 2
      config/exception.php
  6. 45
      config/plugin/tinywan/exception-handler/app.php
  7. 2
      front/src/store/mutation-types.js

@ -13,7 +13,7 @@ class AuthController extends BaseController
/**
* 不需要登录的方法
*/
protected $noNeedLogin = ['login'];
protected $noNeedLogin = ['login', 'logout'];
public function login(Request $request): \support\Response
{

@ -15,6 +15,7 @@
namespace app\admin\middleware;
use ReflectionClass;
use ReflectionException;
use Tinywan\Jwt\JwtToken;
use Webman\Http\Request;
use Webman\Http\Response;
@ -26,12 +27,14 @@ use Webman\MiddlewareInterface;
*/
class AuthCheck implements MiddlewareInterface
{
/**
* @throws ReflectionException
*/
public function process(Request $request, callable $handler): Response
{
// 通过反射获取控制器哪些方法不需要登录
$controller = new ReflectionClass($request->controller);
$noNeedLogin = $controller->getDefaultProperties()['noNeedLogin'] ?? [];
var_dump($noNeedLogin);
// 访问的方法需要登录
if (!in_array($request->action, $noNeedLogin)) {
// 拦截请求,返回一个重定向响应,请求停止向洋葱芯穿越

@ -31,7 +31,8 @@
"vlucas/phpdotenv": "^5.6",
"ext-simplexml": "*",
"webman/think-orm": "^1.1",
"tinywan/jwt": "^1.8"
"tinywan/jwt": "^1.8",
"tinywan/exception-handler": "^1.2"
},
"suggest": {
"ext-event": "For better performance. "

37
composer.lock generated

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c26c61decbc3d54cfca2980798fcc13d",
"content-hash": "dad11a64820c7df0160bac4d2bb6934a",
"packages": [
{
"name": "easywechat-composer/easywechat-composer",
@ -2330,6 +2330,41 @@
],
"time": "2023-07-20T07:21:16+00:00"
},
{
"name": "tinywan/exception-handler",
"version": "v1.2.2",
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/tinywan/exception-handler/v1.2.2/tinywan-exception-handler-v1.2.2.zip",
"reference": "18af45b76fac5f012ba34115378b766c4781a86c",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": ">=7.2",
"workerman/webman-framework": "^1.2.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.6",
"phpstan/phpstan": "^1.4",
"tinywan/jwt": "^1.2",
"tinywan/storage": "^0.2.2",
"tinywan/validate": "^0.0.6",
"webman/think-orm": "^1.1",
"workerman/webman": "^1.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Tinywan\\ExceptionHandler\\": "src"
}
},
"license": [
"MIT"
],
"description": "webman exception handler plugin",
"time": "2023-11-19T05:43:26+00:00"
},
{
"name": "tinywan/jwt",
"version": "v1.8.5",

@ -13,5 +13,5 @@
*/
return [
'' => support\exception\Handler::class,
'' => \Tinywan\ExceptionHandler\Handler::class,
];

@ -0,0 +1,45 @@
<?php
return [
'enable' => true,
// 错误异常配置
'exception_handler' => [
// 不需要记录错误日志
'dont_report' => [
Tinywan\ExceptionHandler\Exception\BadRequestHttpException::class,
Tinywan\ExceptionHandler\Exception\UnauthorizedHttpException::class,
Tinywan\ExceptionHandler\Exception\ForbiddenHttpException::class,
Tinywan\ExceptionHandler\Exception\NotFoundHttpException::class,
Tinywan\ExceptionHandler\Exception\RouteNotFoundException::class,
Tinywan\ExceptionHandler\Exception\TooManyRequestsHttpException::class,
Tinywan\ExceptionHandler\Exception\ServerErrorHttpException::class,
// Tinywan\Validate\Exception\ValidateException::class,
Tinywan\Jwt\Exception\JwtTokenException::class
],
// 自定义HTTP状态码
'status' => [
'validate' => 400, // 验证器异常
'jwt_token' => 401, // 认证失败
'jwt_token_expired' => 401, // 访问令牌过期
'jwt_refresh_token_expired' => 402, // 刷新令牌过期
'server_error' => 500, // 服务器内部错误
],
// 自定义响应消息
'body' => [
'code' => 0,
'msg' => '服务器内部异常',
'data' => null
],
// 事件,event 与 webman/event 存在冲突,event 重命名为 event_trigger
'event_trigger' => [
'enable' => true,
// 钉钉机器人
'dingtalk' => [
'accessToken' => '85b834cadc3905ee57ff6807f3b65eee1cac7a1f29296635d81c24f9850aa5ad',
'secret' => 'SEC0309ed80d09e43c772bc336e72df0a6c27b13d79afd71bb0e91341e866261b9b',
'title' => '钉钉机器人异常通知',
]
],
],
];

@ -1,4 +1,4 @@
export const ACCESS_TOKEN = 'Access-Token'
export const ACCESS_TOKEN = 'authorization'
export const SIDEBAR_TYPE = 'sidebar_type'
export const TOGGLE_MOBILE_TYPE = 'is_mobile'

Loading…
Cancel
Save