【问题标题】:JWT cakephp auth not working in productionJWT cakephp auth 在生产中不起作用
【发布时间】:2016-02-16 04:19:28
【问题描述】:

您好,我在生产服务器上使用 Cakephp 3.x 应用程序进行 ADmad/JwtAuth.Jwt 身份验证时遇到问题。

我已经在几个开发环境中安装了我的 Cakephp 应用程序,并且按预期工作。当我尝试在网络托管服务上运行它时,我的问题就出现了。我已经尝试了两个,在它们(ehost和dreamhost)上都获得了相同的401(未经授权)错误

我按照本教程进行设置:http://www.bravo-kernel.com/2015/04/how-to-add-jwt-authentication-to-a-cakephp-3-rest-api/

就像我说的在本地和开发环境中完美运行。那么还有其他人认为虚拟主机需要使本教程正常工作吗?有什么建议吗?

【问题讨论】:

    标签: cakephp-3.0 production-environment jwt unauthorized


    【解决方案1】:

    通过添加“sub”属性来解决。

    https://www.rfc-editor.org/rfc/rfc7519#section-4.1.2

    public function token()
    {
        $user = $this->Auth->identify();
    
        if (!$user) {
            throw new UnauthorizedException('Invalid username or password');
        }
    
        $this->set([
            'success' => true,
            'data' => [
                'token' => $token = JWT::encode(
                    [
                        'id' => $user['id'],
                        'sub' => $user['id'],
                        'exp' => time() + 604800,
                        'iat' => time()
                    ],
                    Security::salt()
                )
            ],
            '_serialize' => ['success', 'data']
        ]);
    }
    

    【讨论】:

    • 我也有同样的问题,但我已经有 sub 了,你还需要做什么才能让它在生产中工作吗?本地一切正常。
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多