-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add authentication middleware and exception
Added new files AuthMiddleware.php and AuthException.php to implement JWT-based authentication middleware and custom AuthException. Class AuthException will handle unauthorized access. Authentication was integrated with the AuthMiddleware into the signOut method in UserController.php. Also updated 'no_check_route' and 'login_type' configurations in jwt.php to support the changes. This commit ensures secure access to sensitive routes by validating JWT token.
- Loading branch information
1 parent
c98f8aa
commit 712badd
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Cloud-Admin project. | ||
* | ||
* @link https://www.cloud-admin.jayjay.cn | ||
* @document https://wiki.cloud-admin.jayjay.cn | ||
* @license https://github.com/swow-cloud/swow-admin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace App\Exception; | ||
|
||
use Hyperf\HttpMessage\Exception\HttpException; | ||
use Swow\Http\Status; | ||
use Throwable; | ||
|
||
class AuthException extends HttpException | ||
{ | ||
public function __construct(int $code = Status::UNAUTHORIZED, string $message = null, ?Throwable $previous = null) | ||
{ | ||
parent::__construct($code, $message, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Cloud-Admin project. | ||
* | ||
* @link https://www.cloud-admin.jayjay.cn | ||
* @document https://wiki.cloud-admin.jayjay.cn | ||
* @license https://github.com/swow-cloud/swow-admin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace App\Middleware\Auth; | ||
|
||
use App\Exception\AuthException; | ||
use App\Kernel\Http\Response; | ||
use Hyperf\Di\Annotation\Inject; | ||
use Phper666\JWTAuth\JWT; | ||
use Phper666\JWTAuth\Util\JWTUtil; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Swow\Http\Status; | ||
|
||
class AuthMiddleware implements MiddlewareInterface | ||
{ | ||
#[Inject] | ||
protected Response $response; | ||
|
||
#[Inject] | ||
protected JWT $jwt; | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
// 判断是否为noCheckRoute | ||
$path = $request->getUri()->getPath(); | ||
$method = $request->getMethod(); | ||
if ($this->jwt->matchRoute(null, $method, $path)) { | ||
return $handler->handle($request); | ||
} | ||
|
||
$token = $request->getHeaderLine('Authorization') ?? ''; | ||
|
||
if ($token === '') { | ||
return $this->response->handleException(new AuthException(Status::BAD_REQUEST)); | ||
} | ||
|
||
$token = JWTUtil::handleToken($token); | ||
|
||
if ($token !== false && $this->jwt->verifyToken($token)) { | ||
return $handler->handle($request); | ||
} | ||
|
||
return $this->response->handleException(new AuthException(Status::UNAUTHORIZED, 'Token authentication does not pass')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters