栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

oauth2 redis登录信息(工作中使用)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

oauth2 redis登录信息(工作中使用)

修改oauth2 redis登录信息

  private final AuthenticationKeyGenerator authenticationKeyGenerator = new
            DefaultAuthenticationKeyGenerator();
             RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);

    private final JdkSerializationStrategy serializationStrategy = new JdkSerializationStrategy();
    public void setRedis(Authentication authentication, Collection authorities, HopeUser hopeUser, RedisTokenStore tokenStore) {
        if (authentication instanceof OAuth2Authentication) {
            OAuth2Authentication originalOAuth2Authentication = (OAuth2Authentication) authentication;
            if (!originalOAuth2Authentication.isClientOnly()) {
                Authentication userAuthentication = originalOAuth2Authentication.getUserAuthentication();
                if (userAuthentication instanceof WeChatAuthenticationToken || userAuthentication instanceof SmsCodeAuthenticationToken || userAuthentication instanceof UsernamePasswordAuthenticationToken) {
                    //替换用户信息
                    UsernamePasswordAuthenticationToken usernamePasswordAuthentication = new UsernamePasswordAuthenticationToken(hopeUser, "N_A", authorities);
                    usernamePasswordAuthentication.setDetails(hopeUser);
                    OAuth2Authentication oauth2Authentication = new OAuth2Authentication(originalOAuth2Authentication.getOAuth2Request(), usernamePasswordAuthentication);
                    oauth2Authentication.setDetails(hopeUser);
                    //提取秘钥
                    String key = authenticationKeyGenerator.extractKey(originalOAuth2Authentication);
                    //获取auth_to_access auth_to_access:key
                    byte[] serializedKey = serializationStrategy.serialize("auth_to_access" + ":" + key);
                    byte[] bytes = null;
                    RedisConnection conn = redisConnectionFactory.getConnection();
                    try {
                        bytes = conn.get(serializedKey);
                    } finally {
                        conn.close();
                    }
                    //获取到token
                    OAuth2AccessToken accessToken = serializationStrategy.deserialize(bytes, OAuth2AccessToken.class);
                    //更新这个token权限
                    tokenStore.storeAccessToken(accessToken, oauth2Authentication);
                    tokenStore.storeRefreshToken(accessToken.getRefreshToken(), oauth2Authentication);
                }
            }
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/915282.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号