访问令牌过期时如何在Nuxt身份验证模块(oauth2)中使用带有密钥斗篷的刷新令牌

时间:2020-07-02 16:38:09

标签: javascript nuxt.js keycloak keycloak-services

访问令牌(Bearer JWT令牌)在登录后工作成功,但是在1-2分钟后访问令牌过期,并且axios请求返回401未经授权,并且nuxt应用程序具有刷新令牌,但是如何在nuxt中正确使用它来更新从keycloak服务访问令牌。这是我的nuxt.config.js

<?php
ob_start();  
$sql = "Select COUNT(b.name)as cnt, s.name,s.sid, b.zid from subcategory s left JOIN business b on s.sid = b.sid where s.cid = '10' and b.zid is null or b.zid ='1'and s.cid = '10' group by s.sid ORDER BY s.name ";

$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
                                 
if ($queryResults > 0) { while ($row = mysqli_fetch_array($result)) 
{echo 
"<div class='pcat_list'><a href='subcategory.php?ID={$row['sid']}&ZONE=1' style='color:#444'>".$row['name']."(".$row['cnt'].")</a></div>";
}
}

这是我在管理界面中的密钥斗篷配置 enter image description here

2 个答案:

答案 0 :(得分:0)

我已经使用JS适配器与keycloak进行了合作,如果您从UI方面提供了有关如何配置keycloak的更多信息,我会为我提供angular服务。

答案 1 :(得分:0)

上周,我们面对着同样的问题,即v4.9.1。

在深入研究源代码之后,很明显,尽管此模块即将发布的v5可能引入了对使用刷新令牌自动刷新访问令牌的支持,但v4似乎没有,尽管存储了刷新令牌。

为了在v5发行稳定之前解决此问题,我们创建了module plugin。将其添加到您的项目目录中,并提供给Nuxt Auth plugins

nuxt.config.js中随附的策略配置使用:

...
  grant_type: 'authorization_code',
  response_type: 'code',
  access_type: 'online'
...

(其中grant_typedocumented都不明显。)

此外,在Keycloak本身的客户端配置中,我们必须:

  • 禁用implicit flow
  • access type设置为“公开”
  • 设置web origins以允许来自Nuxt应用程序的部署URL的CORS请求;如果已将它们配置为“有效重定向URI”,则设置为+

这对我们来说很好,当访问令牌过期时从Keycloak捕获401,使用刷新令牌获取新的访问令牌,然后重试原始失败的请求。

(从我对an issue reporting the same on the Nuxt Auth repo的回答中交叉发布。)