修改token的存储策略

This commit is contained in:
winLsy
2018-01-21 17:46:29 +08:00
parent 9df28d563f
commit 176825f1d3
5 changed files with 168 additions and 2 deletions
@@ -0,0 +1,24 @@
package com.micro.fast.security.app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
/**
* 配置令牌的存取策略
* @author lsy
*/
@Configuration
public class TokenStoreConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public TokenStore redisTokenStore(){
return new RedisTokenStore(redisConnectionFactory);
}
}
@@ -1,14 +1,72 @@
package com.micro.fast.security.app.config.authorization2;
import com.micro.fast.security.app.TokenStoreConfig;
import com.micro.fast.security.core.master.OAuth2ClientProperties;
import com.micro.fast.security.core.master.SecurityProperties;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder;
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.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
/**
* OAuth2协议标准服务商实现配置类
* OAuth2协议标准服务商实现配置类,配置token的生成存储策略
* @author lsy
*/
@Configuration
@EnableAuthorizationServer
public class AuthorizationProviderConfig {
public class AuthorizationProviderConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private SecurityProperties securityProperties;
@Autowired
@Qualifier("redisTokenStore")
private TokenStore tokenStore;
/**
* 配置token的存取策略
* @param endpoints
* @throws Exception
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.tokenStore(tokenStore)
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
super.configure(endpoints);
}
/**
* 配置哪些应用可以获取token
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
InMemoryClientDetailsServiceBuilder builder = clients.inMemory();
if (ArrayUtils.isNotEmpty(securityProperties.getOauth2().getClients())){
for (OAuth2ClientProperties client: securityProperties.getOauth2().getClients()){
builder.withClient(client.getClientId()).secret(client.getClientSecret())
.accessTokenValiditySeconds(client.getAccessTokenValiditySeconds())
.authorizedGrantTypes("refresh_token","password")
.scopes("all","read","write")
.and();
}
}
}
}
@@ -0,0 +1,56 @@
package com.micro.fast.security.core.master;
/**
* @author lsy
*/
public class OAuth2ClientProperties {
private String clientId;
private String clientSecret;
private int accessTokenValiditySeconds;
private String authorizedGrantTypes;
private String scopes;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public int getAccessTokenValiditySeconds() {
return accessTokenValiditySeconds;
}
public void setAccessTokenValiditySeconds(int accessTokenValiditySeconds) {
this.accessTokenValiditySeconds = accessTokenValiditySeconds;
}
public String getAuthorizedGrantTypes() {
return authorizedGrantTypes;
}
public void setAuthorizedGrantTypes(String authorizedGrantTypes) {
this.authorizedGrantTypes = authorizedGrantTypes;
}
public String getScopes() {
return scopes;
}
public void setScopes(String scopes) {
this.scopes = scopes;
}
}
@@ -0,0 +1,18 @@
package com.micro.fast.security.core.master;
/**
*
* @author lsy
*/
public class OAuth2Properties {
private OAuth2ClientProperties[] clients = new OAuth2ClientProperties[]{};
public OAuth2ClientProperties[] getClients() {
return clients;
}
public void setClients(OAuth2ClientProperties[] clients) {
this.clients = clients;
}
}
@@ -30,6 +30,8 @@ public class SecurityProperties {
private SessionProperties session = new SessionProperties();
private OAuth2Properties oauth2 = new OAuth2Properties();
public SessionProperties getSession() {
return session;
}
@@ -69,4 +71,12 @@ public class SecurityProperties {
public void setSocial(SocialProperties social) {
this.social = social;
}
public OAuth2Properties getOauth2() {
return oauth2;
}
public void setOauth2(OAuth2Properties oauth2) {
this.oauth2 = oauth2;
}
}