Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用户禁用token更新问题 #5

Open
peterzhangnull opened this issue Aug 10, 2020 · 7 comments
Open

用户禁用token更新问题 #5

peterzhangnull opened this issue Aug 10, 2020 · 7 comments

Comments

@peterzhangnull
Copy link

用户被禁用的后,如果刷新token信息

@copoile
Copy link
Owner

copoile commented Aug 10, 2020

token信息是存redis里面的,更新用户信息的时候重存下信息就可以了,可以参考下controller下面的updateCacheUserInfo方法。

@peterzhangnull
Copy link
Author

peterzhangnull commented Aug 14, 2020

token信息是存redis里面的,更新用户信息的时候重存下信息就可以了,可以参考下controller下面的updateCacheUserInfo方法。
那得是用户主动调用的吧,得用户本人使用access_token 请求updateCacheUserInfo

@copoile
Copy link
Owner

copoile commented Aug 14, 2020

我测试了下,校验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";
    }

@copoile
Copy link
Owner

copoile commented Aug 14, 2020

如果一定要在校验token的同时也校验用户信息可以自定义写一个过滤器实现。

@peterzhangnull
Copy link
Author

peterzhangnull commented Aug 14, 2020

还有个问题多种模式下,username字段得保持唯一吧,不然在存储token的时候同样的username,后面的登录的人始终使用第一个相同username登录生成的accesstoken导致获取用户信息也是第一个人

@copoile
Copy link
Owner

copoile commented Aug 15, 2020

默认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;
    }

@copoile
Copy link
Owner

copoile commented Aug 15, 2020

 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;
            }
        };
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants