qq_42437588 2021-09-10 15:56 采纳率: 0%
浏览 725

springsecurity集成oath2和jwt token设置过期时间


    @Bean
    public TokenStore tokenStore() {
        RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
        tokenStore.setPrefix(SecurityConstants.abc);
        tokenStore.setAuthenticationKeyGenerator(new DefaultAuthenticationKeyGenerator() {
            @Override
            public String extractKey(OAuth2Authentication authentication) {
                return super.extractKey(authentication) + StrUtil.COLON + TenantContextHolder.getTenantId();
            }
        });
        return tokenStore;

默认时常为12个小时,怎样改这个时常啊

  • 写回答

1条回答 默认 最新

  • qbanxiaoli 2021-09-10 19:46
    关注

    public class JwtAccessToken extends JwtAccessTokenConverter {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        DefaultOAuth2AccessToken defaultOAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken);
        //设置过期时间
        defaultOAuth2AccessToken.setExpiration(new Date(System.currentTimeMillis() + 1000 * 3600 * 24));
        // 设置额外用户信息
        User user = ((User) authentication.getPrincipal());
        List<String> authorities = new ArrayList<>();
        user.getAuthorityList().forEach(authority -> authorities.add(authority.getAuthority()));
        // 将用户信息添加到token额外信息中
        defaultOAuth2AccessToken.getAdditionalInformation().put("uuid", user.getUuid());
        defaultOAuth2AccessToken.getAdditionalInformation().put("authorities", authorities);
        return super.enhance(defaultOAuth2AccessToken, authentication);
    }
    

    }

    评论

报告相同问题?

问题事件

  • 创建了问题 9月10日