【发布时间】:2019-04-02 13:49:01
【问题描述】:
我目前正在开发一个具有 ReactJS 前端和 Lumen 后端的应用程序。我正在尝试使用 Auth0 使 API 只能由特定用户访问。
目前,我在 Lumen 中设置了 JWT 身份验证。使用邮递员,我可以在设置Auth0提供的测试承载令牌时成功请求数据。
以下是我用于 JWT 验证的设置:
$verifier = new JWTVerifier([
'supported_algs' => ['RS256'],
'valid_audiences' => ['http://example.com'],
'authorized_iss' => ['https://example.eu.auth0.com/'],
]);
我从 ReactJS 开始,到目前为止,我可以使用 Auth0 完美登录。 以下是 auth0 连接的设置:
auth0 = new auth0.WebAuth({
domain: AUTH_CONFIG.domain,
clientID: AUTH_CONFIG.clientId,
redirectUri: AUTH_CONFIG.callbackUrl,
responseType: 'token id_token',
scope: 'openid profile'
});
登录成功返回的是access_token和id_token。
现在是令人沮丧的部分.. 我对来自我的 API 的数据执行 GET 请求,如下所示:
axios({
url:'http://example.com/public/api/enquiries',
headers: {
'Content-Type': 'application/json',
'Authorization':'Bearer ' + localStorage.getItem('id_token'),
}
}).then(function(response) {
console.log(response);
});
然后这会从我的 Lumen API 返回以下错误:
Invalid audience Wo6olMsBdA0U0azY4q09iZ7bjXPbYSvd; expected http://example.com
我在 Lumen 中对解码数据进行了 var_vump,所有数据都是正确的,除了显示为:Wo6olMsBdA0U0azY4q09iZ7bjXPbYSvd 的“aud”项。
我将上述字符串添加为接受的受众,然后成功发出请求,但这肯定不正确。谁能指出我正确的方向?
非常感谢。
【问题讨论】: