springcloud集成Oauth2权限项目-token存入redis(九) - Go语言中文社区

springcloud集成Oauth2权限项目-token存入redis(九)


目的:将token存入redis是为了将token失效,防止以前的token还可以继续使用

 

oauth pom加入redis 包

<!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

application.yml

redis:
    database: 0
    host: 127.0.0.1
    port: 6379

在OAuth2ServerConfig.java配置redis

加入下面这段代码

/**
         * tokenstore 定制化处理
         * @return TokenStore
         */
        @Bean
        public TokenStore redisTokenStore() {
            RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
            //redis key 前缀
            tokenStore.setPrefix(DEMO_RESOURCE_ID+"_");
            return tokenStore;
        }

一定要将redis注入进去

@Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
            tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));
            endpoints
                    .tokenEnhancer(tokenEnhancerChain)
                    .tokenStore(redisTokenStore())
                    .accessTokenConverter(accessTokenConverter())
                    .authenticationManager(authenticationManager)
                    .userDetailsService(userDetailsService)
                    // 2018-4-3 增加配置,允许 GET、POST 请求获取 token,即访问端点:oauth/token
                    .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
            endpoints.reuseRefreshTokens(true);
            //oauth2登录异常处理
        }

关于redis安装百度很多教程

然后启动项目

获取token

127.0.0.1:9999/oauth/oauth/token?username=hello&password=hello&grant_type=password&scope=scope&client_id=client_id&client_secret=client_secret

获取成功,查看redis中是否存在

生成了这么多的东西,说明已经存入redis中

项目地址:https://github.com/James-Pan0525/vcloud.git

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_38157516/article/details/100524339
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:06:54
  • 阅读 ( 1485 )
  • 分类:Redis

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢