Slim으로 REST API를 작성하고 있습니다.Slim PHP : 미들웨어로 유효한 경로 만 잡으십시오.
<?php
class SecurityMiddleware extends \Slim\Middleware
{
protected $resource;
public function __construct($resource)
{
$this->resource = $resource;
}
public function call()
{
//get a reference to application
$app = $this->app;
//skip routes that are exceptionally allowed without an access token:
$publicRoutes = ["/","/login","/about"];
if (in_array($app->request()->getPathInfo(),publicRoutes)){
$this->next->call(); //let go
} else {
//Validate:
if ($this->resource->isValid()){
$this->next->call(); //validation passed, let go
} else {
$app->response->setStatus('403'); //validation failed
$app->response->body(json_encode(array("Error"=>"Access token problem")));
return;
}
}
}
}
이 작동하지만, 바람직하지 않은 부작용이 기존 경로와 비 사이에 구별을하지 않습니다 미들웨어 : 나는 인증 된 사용자 만이 그들을 액세스 할 수 있도록 자원을 보호하기 위해 작은 미들웨어를 작성했습니다 기존 경로. 예를 들어, 사용자가 존재하지 않는 /dfghdfgh
과 같은 경로를 요청하려고 시도하면 404라는 HTTP 상태 코드를받는 대신 액세스 토큰이 없다는 403이라는 메시지가 표시됩니다. 미들웨어 클래스에 대해 다음과 유사한 구현을 추가하고자합니다.
if ($app->hasRoute($app->request->getPathInfo()){
$this->next->call(); //let go so user gets 404 from the app.
}
어떻게 구현할 수 있습니까?
감사합니다. 따라서 기본적으로 반대 방향을 취하고 있습니다. 공개적으로 사용 가능한 경로를 버리는 대신 인증이 필요한 경로를 잡는 것입니다. 필자의 경우 거의 모든 리소스가 인증을 필요로하므로이 접근 방식이 내 유스 케이스에 맞지 않는다고 생각합니다. – user1555863