refresh_token仅工作一次

时间:2018-12-17 20:35:21

标签: spring oauth-2.0 spring-oauth2

我对此事挠头。

使用JdbcTokenStore

我能够登录并从access_token端点获取refresh_tokenoauth/token。 Web客户端存储两个令牌。 access_token过期并发送HTTP 401之后,客户端尝试通过显示access_token来获取新的refresh_token

新的access_tokenrefresh_token重新存储在客户端。

但是,第二次完成此操作时,我收到一个invalid_grant错误,告诉我refresh_token是错误的。

这是日志:

2018-12-17T20:24:42.193Z INFO [main.js:1033] Login success.

...

ngx-logger.js:250 2018-12-17T20:24:42.195Z INFO [main.js:1034] {
  "access_token": .72UGm5604uDyuY0eDSKE3s_Wr9GzhOFyYMsWoYKxdGs",
  "token_type": "bearer",
  "refresh_token": .5tucDhuMJciUufeosI-FG1lO5WaWQCq9_7G7kDPGmMc",
  "expires_in": 4,
  "scope": "read write",
  "jti": "495c03d9-c19d-4239-9d94-9e96c49844f5"
}
ngx-logger.js:256 2018-12-17T20:24:42.198Z DEBUG [main.js:748] User is logged in.!


...

ngx-logger.js:250 2018-12-17T20:24:54.438Z INFO [main.js:843] Handling 401 error
ngx-logger.js:250 2018-12-17T20:24:54.441Z INFO [main.js:845] Refreshing access token
ngx-logger.js:250 2018-12-17T20:24:54.443Z INFO [main.js:1044] Attempting to refresh access token
ngx-logger.js:250 2018-12-17T20:24:54.446Z INFO [main.js:1045] refresh_token: .5tucDhuMJciUufeosI-FG1lO5WaWQCq9_7G7kDPGmMc

...

ngx-logger.js:250 2018-12-17T20:24:54.643Z INFO [main.js:854] Access token refreshed.
ngx-logger.js:250 2018-12-17T20:24:54.651Z INFO [main.js:855] {
  "access_token": .gets297iCBDdNNK2C29PBTxRP1VdM9ok3ilo1g5Ow0A",
  "token_type": "bearer",
  "refresh_token": .NcJQOWDDo1q474LzvCeh37BjCn14I3E6e03JuWO208Y",
  "expires_in": 4,
  "scope": "read write",
  "jti": "0fa0c63e-2027-4780-9ce3-501608cdaee5"
}

... 

ngx-logger.js:247 2018-12-17T20:25:13.162Z ERROR [main.js:863] {
  "headers": {
    "normalizedNames": {},
    "lazyUpdate": null
  },
  "status": 400,
  "statusText": "OK",
  "url": "https://192.168.1.144:8443/oauth/token",
  "ok": false,
  "name": "HttpErrorResponse",
  "message": "Http failure response for https://192.168.1.144:8443/oauth/token: 400 OK",
  "error": {
    "error": "invalid_grant",
    "error_description": "Invalid refresh token: .NcJQOWDDo1q474LzvCeh37BjCn14I3E6e03JuWO208Y"
  }
}

我不知道这里可能出什么问题。我想念什么吗?

1 个答案:

答案 0 :(得分:1)

原来,我不得不将TokenEnhancerChain设置为重用刷新令牌。显然,默认情况下默认为true,因为这就是我们使用refresh_tokens的原因-它们的行为应与access_tokens一样(如果您现在感到困惑:这很讽刺-我不知道为什么为什么默认值为true,如果有人可以澄清这一点,我会感到非常高兴。

无论如何。在您的配置中要设置(类似):

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
    enhancerChain.setTokenEnhancers(Collections.singletonList(accessTokenConverter));
    endpoints.tokenStore(tokenStore)
            .accessTokenConverter(accessTokenConverter)
            .tokenEnhancer(enhancerChain)
            .reuseRefreshTokens(false)  // <-------------- Set to false
            .authenticationManager(authenticationManager);
}
  

github上的相关问题:#867