请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

JwtTokenStore的jwt存在哪里

老师,您好!
JdbcTokenStore的token 是存放在数据库的。
1、JwtTokenStore的jwt是存放在哪里的呢?学生想把jwt存到redis里,又要怎么做?
2、使用JwtTokenStore的话,每次调用/token/oauth/token都会拿到一个新的jwt,并且上一个jwt还不会过期。这种情况要怎么实现成JdbcTokenStore一样,第二次获取jwt的话,上一次的jwt不过期就会返回上一个jwt。
图片描述

正在回答 回答被采纳积分+3

2回答

月光下的初恋 2019-11-26 11:49:34

回复 朱小悬:我是将jwt存在redis里的,登出的时候从redis删除就行了。


这是代码:

@Configuration

public class TokenStoreConfig {


    @Configuration

    @ConditionalOnProperty(prefix = "auth.token.store", name = "type", havingValue = "jdbc")

    @Import(AuthJdbcTokenStore.class)

    public static class JdbcTokenConfig {

    }


    @Configuration

    @ConditionalOnProperty(prefix = "auth.token.store", name = "type", havingValue = "redis")

    @Import(AuthRedisTokenStore.class)

    public static class RedisTokenConfig {

    }


    @Configuration

    @ConditionalOnProperty(prefix = "auth.token.store", name = "type", havingValue = "jwt")

    @Import(AuthJwtTokenStore.class)

    public static class AuthJwtTokenConfig {

    }


    @Configuration

    @ConditionalOnProperty(prefix = "auth.token.store", name = "type", havingValue = "redisJwt", matchIfMissing = true)

    @Import(RedisJwtTokenStore.class)

    public static class ResJwtTokenConfig {

    }


    @Bean

    public TokenEnhancerChain tokenEnhancerChain() {

        return new TokenEnhancerChain();

    }

}


RedisJwtTokenStore配置如下:

public class RedisJwtTokenStore {


    @Resource

    private TokenEnhancerChain tokenEnhancerChain;


  @Resource

    private RedisConnectionFactory redisConnectionFactory;


    @Bean

    public TokenStore redisTokenStore() {

        return new RedisTokenStore(redisConnectionFactory);

    }

    @Bean

    public JwtAccessTokenConverter jwtAccessTokenConverter() {

        final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();

        converter.setSigningKey("test");

        return converter;

    }


    @PostConstruct

    public void init(){

        List<TokenEnhancer> enhancers = new ArrayList<>(2);


        enhancers.add(jwtTokenEnhancer());

        enhancers.add(jwtAccessTokenConverter());


        tokenEnhancerChain.setTokenEnhancers(enhancers);

    }

    /**

     * jwt 生成token 定制化处理

     * 添加一些额外的用户信息到token里面

     *

     * @return TokenEnhancer

     */

    @Bean

    @ConditionalOnMissingBean(DefaultJwtTokenEnhancer.class)

    public TokenEnhancer jwtTokenEnhancer() {

        return new DefaultJwtTokenEnhancer();

    }

}


0 回复 有任何疑惑可以回复我~
JoJo 2019-11-25 09:03:53
  1. JwtTokenStore不存jwt,因为jwt是自包含的。不建议你存jwt,如果你一定要存token,就别用jwt。

  2. 因为JwtTokenStore不存jwt,所以没法返回上一个没过期的jwt,同理,如果你想要这个效果,就别用jwt。

最后,如果你一定要,就得自己写一个JwtTokenStore。

0 回复 有任何疑惑可以回复我~
  • 提问者 朱小悬 #1
    老师,如果要登出的情况,是需要删除jwt的,不然我登出了,但jwt的过期时间还没到,那登出就不通了,那种情况需要怎么处理呢
    回复 有任何疑惑可以回复我~ 2019-11-25 11:36:41

相似问题

登录后可查看更多问答,登录/注册

问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号