使用jwt替换默认的令牌

This commit is contained in:
winLsy
2018-01-21 19:09:16 +08:00
parent 176825f1d3
commit f21a6a156f
6 changed files with 112 additions and 7 deletions
@@ -1,10 +1,17 @@
package com.micro.fast.security.app;
import com.micro.fast.security.app.jwt.MsJwtEnhancer;
import com.micro.fast.security.core.master.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
/**
@@ -18,7 +25,41 @@ public class TokenStoreConfig {
private RedisConnectionFactory redisConnectionFactory;
@Bean
@ConditionalOnProperty(prefix = "ms.security.oauth2",name = "storeType",havingValue = "redis",matchIfMissing = false)
public TokenStore redisTokenStore(){
return new RedisTokenStore(redisConnectionFactory);
}
/**
* 如果存在ms.security.oauth2.storeType的配置存在且值是jwt这个配置就生效
* 如果不存在这个ms.security.oauth2.storeType属性就默认生效
*/
@Configuration
@ConditionalOnProperty(prefix = "ms.security.oauth2",name = "storeType",havingValue = "jwt",matchIfMissing = true)
public static class JwtTokenConfig{
@Autowired
private SecurityProperties securityProperties;
@Bean
public TokenStore jwtTokenStore(){
return new JwtTokenStore(jwtAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter(){
JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
//根据秘钥生成token
jwtAccessTokenConverter.setSigningKey(securityProperties.getOauth2().getJwtSigningKey());
return jwtAccessTokenConverter;
}
@Bean
@ConditionalOnMissingBean
public TokenEnhancer tokenEnhancer(){
return new MsJwtEnhancer();
}
}
}
@@ -1,8 +1,8 @@
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 com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -14,7 +14,13 @@ import org.springframework.security.oauth2.config.annotation.configurers.ClientD
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.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import java.util.ArrayList;
import java.util.List;
/**
* OAuth2协议标准服务商实现配置类,配置token的生成存储策略
@@ -27,18 +33,26 @@ public class AuthorizationProviderConfig extends AuthorizationServerConfigurerAd
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
@Autowired(required = true)
private UserDetailsService userDetailsService;
@Autowired
private SecurityProperties securityProperties;
/**
* 只有在jwt环境下才生效
*/
@Autowired(required = false)
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Autowired
private TokenEnhancer jwtTokenEnhancer;
@Autowired
@Qualifier("redisTokenStore")
private TokenStore tokenStore;
/**
* 配置token的存取策略
* 配置token的存取策略,以及增强策略
* @param endpoints
* @throws Exception
*/
@@ -48,6 +62,18 @@ public class AuthorizationProviderConfig extends AuthorizationServerConfigurerAd
.tokenStore(tokenStore)
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
if (jwtAccessTokenConverter!=null && jwtTokenEnhancer!=null){
//声明token增强链
TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
//将增强逻辑填入增强链
List<TokenEnhancer> enhancers = new ArrayList<>();
enhancers.add(jwtTokenEnhancer);
enhancers.add(jwtAccessTokenConverter);
enhancerChain.setTokenEnhancers(enhancers);
//将增强链放入扩展点中
endpoints.tokenEnhancer(enhancerChain)
.accessTokenConverter(jwtAccessTokenConverter);
}
super.configure(endpoints);
}
@@ -24,7 +24,9 @@ import org.springframework.social.security.SpringSocialConfigurer;
@EnableResourceServer
public class AuthorizationResourceConfig extends ResourceServerConfigurerAdapter {
//todo 待优化,不唯一,应从启动参数中获取
/**
* todo 待优化,不唯一,应从启动参数中获取
*/
public static final String DRUID = "/druid/*";
@Autowired
@@ -0,0 +1,25 @@
package com.micro.fast.security.app.jwt;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 对jwt中的信息进行自定义
* @author lsy
*/
public class MsJwtEnhancer implements TokenEnhancer{
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
//添加自定义的信息
Map<String,Object> info = new HashMap<>();
info.put("project","ms");
((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(info);
return oAuth2AccessToken;
}
}
@@ -1,13 +1,25 @@
package com.micro.fast.security.core.master;
/**
*
* aouth2属性配置
* @author lsy
*/
public class OAuth2Properties {
private OAuth2ClientProperties[] clients = new OAuth2ClientProperties[]{};
private String jwtSigningKey = "msjwt";
private String storeType = "jwt";
public String getJwtSigningKey() {
return jwtSigningKey;
}
public void setJwtSigningKey(String jwtSigningKey) {
this.jwtSigningKey = jwtSigningKey;
}
public OAuth2ClientProperties[] getClients() {
return clients;
}
@@ -49,7 +49,6 @@ public class RestfulController {
System.out.println(s);
});
}
System.out.println(user.getName());
System.out.println(user.getPassword());
System.out.println(user.getBirthday());