目的:将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中