提交 61cfbc54 编写于 作者: M ManongJu

使用redis存储Token信息,更为接近生产环境

上级 b036c99c
...@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
...@@ -15,9 +16,9 @@ import org.springframework.security.oauth2.provider.ClientDetailsService; ...@@ -15,9 +16,9 @@ import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.concurrent.TimeUnit;
/** /**
* Created by Mr.Yangxiufeng on 2017/12/28. * Created by Mr.Yangxiufeng on 2017/12/28.
...@@ -36,11 +37,20 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap ...@@ -36,11 +37,20 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
@Autowired @Autowired
private UserDetailsServiceImpl userDetailsService; private UserDetailsServiceImpl userDetailsService;
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean @Bean
public JdbcTokenStore jdbcTokenStore(){ RedisTokenStore redisTokenStore(){
return new JdbcTokenStore(dataSource); return new RedisTokenStore(redisConnectionFactory);
} }
//token存储数据库
// @Bean
// public JdbcTokenStore jdbcTokenStore(){
// return new JdbcTokenStore(dataSource);
// }
@Override @Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception { public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetails()); clients.withClientDetails(clientDetails());
...@@ -51,7 +61,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap ...@@ -51,7 +61,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
} }
@Override @Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(jdbcTokenStore()) endpoints.tokenStore(redisTokenStore())
.userDetailsService(userDetailsService) .userDetailsService(userDetailsService)
.authenticationManager(authenticationManager); .authenticationManager(authenticationManager);
endpoints.tokenServices(defaultTokenServices()); endpoints.tokenServices(defaultTokenServices());
...@@ -65,10 +75,11 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap ...@@ -65,10 +75,11 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
@Bean @Bean
public DefaultTokenServices defaultTokenServices(){ public DefaultTokenServices defaultTokenServices(){
DefaultTokenServices tokenServices = new DefaultTokenServices(); DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setTokenStore(jdbcTokenStore()); tokenServices.setTokenStore(redisTokenStore());
tokenServices.setSupportRefreshToken(true); tokenServices.setSupportRefreshToken(true);
tokenServices.setClientDetailsService(clientDetails()); tokenServices.setClientDetailsService(clientDetails());
// tokenServices.setAccessTokenValiditySeconds( (int) TimeUnit.DAYS.toSeconds(30)); // token有效期自定义设置,默认12小时 tokenServices.setAccessTokenValiditySeconds(60); // token有效期自定义设置,默认12小时
tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 30);//默认30天,这里修改
return tokenServices; return tokenServices;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册