简体   繁体   English

Keyjloak令牌对于angularjs不起作用

[英]Keycloak token not active with angularjs

I have spring boot backend app with Angular js app. 我有Angular js应用程序的spring boot后端应用程序。 The login process and initial backend communication are successful. 登录过程和初始后端通信成功。 After some idle time, the front end will show 403 forbidden with token not active on the backend console. 在一些空闲时间之后,前端将显示403禁止,后端控制台上的令牌未激活。

The code below contains refresh token, But it seems not working. 下面的代码包含刷新令牌,但它似乎无法正常工作。

// use bearer token when calling backend
themesApp.config(['$httpProvider', function($httpProvider) {
  var isExpired = keycloak.isTokenExpired();
  var token = keycloak.token;

  if (isExpired) {
    keycloak.updateToken(5)
    .success(function() {
      $httpProvider.defaults.headers.common['Authorization'] = 'BEARER ' + token;
    })
    .error(function() {
      console.error('Failed to refresh token');
    });
  }

  $httpProvider.defaults.headers.common['Authorization'] = 'BEARER ' + token;
}]);

Error on the backend 后端出错

2017-05-29 10:08:23.715 ERROR 5072 --- [nio-8080-exec-3] o.k.a.BearerTokenRequestAuthenticator    : Failed to verify token

org.keycloak.common.VerificationException: Token is not active

Something must be wrong on the Keycloak Server, Token not active means token being is expired or is used before it gets valid. Keycloak Server上的某些内容必定是错误的, Token not active意味着令牌已过期或在其生效之前使用。 Could it be that the time/date is wrong on your KC server ? 可能是你的KC服务器上的时间/日期错了吗?

you can config the 'Session Idle Time' here: 你可以在这里配置'Session Idle Time':

在此输入图像描述

I had the same issue and handle it with an automatical logout. 我有同样的问题,并通过自动注销处理它。 So the user has to login again. 因此用户必须再次登录。

In your code: 在你的代码中:

var token = keycloak.token;

you define the value of token once. 你定义一次令牌的价值。 After the update you have to set it again: 更新后,您必须再次设置它:

// use bearer token when calling backend
themesApp.config(['$httpProvider', function($httpProvider) {
  var isExpired = keycloak.isTokenExpired();
  var token = keycloak.token;

  if (isExpired) {
    keycloak.updateToken(5)
    .success(function() {

     // UPDATE THE TOKEN
     token = keycloak.token;

     $httpProvider.defaults.headers.common['Authorization'] = 'BEARER ' +     token;
    })
.error(function() {
  console.error('Failed to refresh token');
});
}

 $httpProvider.defaults.headers.common['Authorization'] = 'BEARER ' + token;
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM