首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JWT透传api的具体方法

JWT透传api的具体方法
EN

Stack Overflow用户
提问于 2017-01-20 21:02:28
回答 2查看 914关注 0票数 1

在上面的问题中,我得到了包含getpostputdelete方法的api/user。可以在特定的方法上使用passthrough吗?

例如,公共方法只有get,而rest需要令牌才能使用该方法?

谢谢你的回答。

代码语言:javascript
复制
$app->add(new \Slim\Middleware\JwtAuthentication([
"path" => ["/api", "/admin"],
"passthrough" => ["/api/login", "/admin/ping", "/api/user"],
"algorithm" => "HS256",
"secret" => getenv("JWT_SECRET"),
"callback" => function ($request, $response, $arguments) use ($container) {
    $container["jwt"] = $arguments["decoded"];
},
"error" => function ($request, $response, $arguments) {
    $data["status"] = "error";
    $data["message"] = $arguments["message"];
    return $response
        ->withHeader("Content-Type", "application/json")
        ->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}]));
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-22 22:37:18

默认情况下,JWT Authentication middleware不对OPTIONS请求进行身份验证。要允许未经身份验证的GET请求,您可以手动将其添加到RequestMethodRule。您的示例代码将如下所示。

代码语言:javascript
复制
require __DIR__ . "/vendor/autoload.php";

$app = new \Slim\App;
$container = $app->getContainer();

$app->add(new \Slim\Middleware\JwtAuthentication([
    "path" => ["/api"],
    "secret" => getenv("JWT_SECRET"),
    "callback" => function ($request, $response, $arguments) use ($container) {
        $container["jwt"] = $arguments["decoded"];
    },
    "rules" => [
        new \Slim\Middleware\JwtAuthentication\RequestMethodRule([
            "passthrough" => ["OPTIONS", "GET"]
        ])
    ],
    "error" => function ($request, $response, $arguments) {
        $data["status"] = "error";
        $data["message"] = $arguments["message"];
        return $response
            ->withHeader("Content-Type", "application/json")
            ->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
    }
]));

$app->get("/api/user", function ($request, $response) {
    print "Hello\n\n";
});

$app->post("/api/user", function ($request, $response) {
    print "Hello\n\n";
});

$app->run();

这将会产生影响。

代码语言:javascript
复制
$ curl --request GET --include http://127.0.0.1:8080/api/user
HTTP/1.1 200 OK
Host: 127.0.0.1:8080
Connection: close
X-Powered-By: PHP/7.0.12
Content-Type: text/html; charset=UTF-8
Content-Length: 7

Hello

$ curl --request POST --include http://127.0.0.1:8080/api/user
HTTP/1.1 401 Unauthorized
Host: 127.0.0.1:8080
Connection: close
X-Powered-By: PHP/7.0.12
Content-Type: application/json
Content-Length: 59

{
    "status": "error",
    "message": "Token not found"
}
票数 1
EN

Stack Overflow用户

发布于 2017-01-20 23:04:24

可以,您可以同时使用Slim Middleware和分组授权路由,并将中间件添加到分组中:

代码语言:javascript
复制
$validateUser = function($request,$response,$next) {
    $token = $_COOKIE['token'];
    $token = JWT::decode($token,$secret,['HS256']);

    if ($token->user->isAdmin) {
        return $next($request,$response);
    }
    return $response->withStatus(403)->withJson(array('message' => 'Forbidden'));
};


$app->get('/api/user',function($request,$response) {
    return $response->withJson(array('message' => 'Public route'));
});

$app->group('/api/user',function() {
    $this->delete('','');
    $this->post('','');
    $this->patch('','');
})->add($validateUser);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41764365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档