使用JWT Auth发送API密钥 - 是否可能?

时间:2015-11-26 21:30:00

标签: php api authentication multi-tenant jwt

我创建了一个使用JWT授权用户的API,当然这很有用。

我的API的某些部分需要在用户登录时访问。我会称他们为匿名用户或API的公共访问权限。

但是,必须有一些基于令牌的身份验证,否则恶意用户可以简单地访问我的api并构建自己的前端。我不希望这样。

如何使用JWT授权令牌以防止用户建模以及具有用户身份验证的其他内容?

如果这些公共页面没有表单提交,我将如何发送所述令牌?

以下是我现有的JWT auth脚本的示例,供我参考,正如我所说,我不想改变这一点,我想添加一些功能来验证我的索引页面,而不是验证用户。

AuthenticateController.php:

    ...

        public function authenticate(Request $request)
{
    $credentials = $request->only('email', 'password');

    try {
        // verify the credentials and create a token for the user
        if (! $token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // if no errors are encountered we can return a JWT
    return response()->json(compact('token'));
}
    ...

jwt.php(仅使用用户模型)

        |--------------------------------------------------------------------------
| User Model namespace
|--------------------------------------------------------------------------
|
| Specify the full namespace to your User model.
| e.g. 'Acme\Entities\User'
|
*/

'user' => 'App\User',

/*
|--------------------------------------------------------------------------
| User identifier
|--------------------------------------------------------------------------
|
| Specify a unique property of the user that will be added as the 'sub'
| claim of the token payload.
|
*/

'identifier' => 'user_id',

任何有关创意的建议都将受到赞赏。对不起,我没有进一步的这个,但我不确定在概念层面如何做到这一点!这是我第一次尝试使用基于API的应用。谢谢。

  编辑:经过一番谷歌搜索,似乎我想使用JWT生成并发送API密钥......但是怎么样?

0 个答案:

没有答案
相关问题