用户禁用token更新问题
用户被禁用的后,如果刷新token信息
token信息是存redis里面的,更新用户信息的时候重存下信息就可以了,可以参考下controller下面的updateCacheUserInfo方法。
token信息是存redis里面的,更新用户信息的时候重存下信息就可以了,可以参考下controller下面的updateCacheUserInfo方法。 那得是用户主动调用的吧,得用户本人使用access_token 请求updateCacheUserInfo
我测试了下,校验token的时候它只会校验token的合法性,不会去校验用户信息的,虽然不能校验用户信息,不过可以移除token,致使token过期,重新登录就会提示禁用。
@GetMapping("/update2")
public @ResponseBody String updateUserInfo() {
Collection<OAuth2AccessToken> tokensByClientIdAndUserName =
tokenStore.findTokensByClientIdAndUserName("yaohw", "yaohw");
if (tokensByClientIdAndUserName != null) {
tokensByClientIdAndUserName.forEach(t -> {
consumerTokenServices.revokeToken(t.getValue());
});
}
return "ok";
}
如果一定要在校验token的同时也校验用户信息可以自定义写一个过滤器实现。
还有个问题多种模式下,username字段得保持唯一吧,不然在存储token的时候同样的username,后面的登录的人始终使用第一个相同username登录生成的accesstoken导致获取用户信息也是第一个人
默认RedisTokenStore.java生成token的key是username + clientId+非空scope经过MD5加密后的结果,也就是说默认情况下username、clientId、scope相同,那么他们使用同一个token,第一次登录成功,第二次登录根据三者组成的key去redis取,发现已存在,则直接返回已存在的token,不再重新生成。
上面说的是默认情况下是通过 RedisTokenStore.java下DefaultAuthenticationKeyGenerator.java实现的,如果需要自定义,可以在配置 RedisTokenStore的时候设置一个自定义的AuthenticationKeyGenerator。
/**
* 配置token存储,这个配置token存到redis中
* @return
*/
@Bean
public TokenStore tokenStore() {
RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
redisTokenStore.setAuthenticationKeyGenerator(“自定义AuthenticationKeyGenerator”);
return redisTokenStore;
}
private AuthenticationKeyGenerator keyGenerator() {
return new AuthenticationKeyGenerator() {
/**
* @param authentication an OAuth2Authentication
* @return a unique key identifying the authentication
*/
@Override
public String extractKey(OAuth2Authentication authentication) {
OAuth2Request oAuth2Request = authentication.getOAuth2Request();
String clientId = oAuth2Request.getClientId();
UserDetailImpl principal = (UserDetailImpl)authentication.getPrincipal();
log.info("用户信息:{}", principal);
return principal.getId() + clientId;
}
};
}