提交 a6e205dc 编写于 作者: zhouweidong's avatar zhouweidong

clean

上级 bd0af4d4
package cn.ibizlab.core.uaa.extensions.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
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.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
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 org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import java.util.Arrays;
/**
* oauth2认证服务器配置
*/
@Configuration
@EnableAuthorizationServer
@ConditionalOnProperty(name = "ibiz.oauth2.enable", havingValue = "true")
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Value("${ibiz.jwt.oauth2.signkey:oauth2}")
private String SignKey;
@Value("${ibiz.oauth.expiration:7200}")
private int expiration;
@Autowired
AuthenticationManager authenticationManager;
@Autowired
RedisConnectionFactory redisConnectionFactory;
@Autowired
OAuth2ClientDetailsService clientDetailsService;
/**
* 客户端配置
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetailsService);
}
/**
* 访问端点、Token存储策略
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.tokenStore(getTokenStore())
.accessTokenConverter(accessTokenConverter())
.authenticationManager(authenticationManager)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
}
/**
* 访问端点权限配置
* @param oauthServer
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
oauthServer.allowFormAuthenticationForClients().checkTokenAccess("permitAll()");
}
/**
* token存储策略
* @return
*/
@Bean
public TokenStore getTokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
/**
* 客户端秘钥
* @return
*/
@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
/**
* token存储策略(jwt增强)
* @return
*/
@Bean
public AuthorizationServerTokenServices tokenService() {
DefaultTokenServices tokenService = new DefaultTokenServices();
tokenService.setClientDetailsService(clientDetailsService);
tokenService.setSupportRefreshToken(true);
tokenService.setTokenStore(getTokenStore());
TokenEnhancerChain enhancer = new TokenEnhancerChain();
enhancer.setTokenEnhancers(Arrays.asList(accessTokenConverter()));
tokenService.setTokenEnhancer(enhancer);
tokenService.setAccessTokenValiditySeconds(expiration);
return tokenService;
}
/**
* jwt密签
* @return
*/
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(SignKey);
return converter;
}
}
package cn.ibizlab.core.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.authentication.TokenExtractor;
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.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
* oauth资源服务器配置
*/
@Configuration
@EnableResourceServer
@Slf4j
public class OAuth2SecurityConfig extends ResourceServerConfigurerAdapter {
@Value("${ibiz.oauth2.header:accesstoken}")
private String authorization;
@Value("${ibiz.oauth2.signkey:oauth2}")
private String SignKey;
/**
* token存储、解析,校验
* @param resources
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.tokenExtractor(new OAuth2TokenExtractor());
resources.tokenStore(getTokenStore()).stateless(true);
}
/**
* 校验策略,只校验oauth2颁发的token(特定请求头)
* @param http
* @throws Exception
*/
@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new OAuth2RequestMatcher());
http.authorizeRequests().anyRequest().authenticated();
}
/**
* token存储器
* @return
*/
@Bean
public TokenStore getTokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
/**
* jwt密签
* @return
*/
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(SignKey);
return converter;
}
/**
* 匹配特定oauth2请求头token,其余请求交由spring security处理
*/
public class OAuth2RequestMatcher implements RequestMatcher {
@Override
public boolean matches(HttpServletRequest request) {
String header = request.getHeader(authorization);
if (!StringUtils.isEmpty(header)) {
return true;
} else {
return false;
}
}
}
/**
* token请求头处理
*/
public class OAuth2TokenExtractor implements TokenExtractor {
@Override
public Authentication extract(HttpServletRequest request) {
String tokenValue = this.extractToken(request);
if (tokenValue != null) {
PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(tokenValue, "");
return authentication;
} else {
return null;
}
}
protected String extractToken(HttpServletRequest request) {
String token = this.extractHeaderToken(request);
if (token == null) {
log.debug("Token not found in headers. Trying request parameters.");
token = request.getParameter("access_token");
if (token == null) {
log.debug("Token not found in request parameters. Not an OAuth2 request.");
} else {
request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE, "Bearer");
}
}
return token;
}
protected String extractHeaderToken(HttpServletRequest request) {
Enumeration headers = request.getHeaders(authorization);
String value;
do {
if (!headers.hasMoreElements()) {
return null;
}
value = (String) headers.nextElement();
} while (!value.toLowerCase().startsWith("Bearer".toLowerCase()));
String authHeaderValue = value.substring("Bearer".length()).trim();
request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE, value.substring(0, "Bearer".length()).trim());
int commaIndex = authHeaderValue.indexOf(44);
if (commaIndex > 0) {
authHeaderValue = authHeaderValue.substring(0, commaIndex);
}
return authHeaderValue;
}
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册