🔀 合并分支。合并pigx 更改的新特性

This commit is contained in:
冷冷 committed 2018-10-30 19:47:30 +08:00
commit d31535f0f2
33 files changed
+137 -96

No files matched your search

@@ -18,6 +18,8 @@
package com.pig4cloud.pigx.common.security.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
@@ -25,10 +27,14 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Res
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.web.client.RestTemplate;
/**
* @author lengleng
* @date 2018/6/22
* <p>
* 1. 支持remoteTokenServices 负载均衡
* 2. 支持 获取用户全部信息
*/
public abstract class BaseResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
@Autowired
@@ -40,21 +46,49 @@ public abstract class BaseResourceServerConfigurerAdapter extends ResourceServer
@Autowired
protected UserDetailsService userDetailsService;
/**
* 默认的配置,对外暴露
*
* @param http
* @throws Exception
*/
@Override
public abstract void configure(HttpSecurity http) throws Exception;
/**
* why add resourceId
* https://stackoverflow.com/questions/28703847/how-do-you-set-a-resource-id-for-a-token
* 提供子类重写
* <p>
* 1. 不重写,默认支持获取雍熙
* 2. 重写notGetUser,提供性能
* <p>
* see codegen ResourceServerConfigurer
*
* @param resources
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
canGetUser(resources);
}
@Bean
@LoadBalanced
public RestTemplate lbRestTemplate() {
return new RestTemplate();
}
/**
* 不获取用户详细 只有用户名
*
* @param resources
*/
protected void notGetUser(ResourceServerSecurityConfigurer resources) {
DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
userTokenConverter.setUserDetailsService(userDetailsService);
accessTokenConverter.setUserTokenConverter(userTokenConverter);
remoteTokenServices.setRestTemplate(lbRestTemplate());
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler)
@@ -62,4 +96,22 @@ public abstract class BaseResourceServerConfigurerAdapter extends ResourceServer
}
/**
* 上下文中获取用户全部信息,两次调用userDetailsService,影响性能
*
* @param resources
*/
private void canGetUser(ResourceServerSecurityConfigurer resources) {
DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
userTokenConverter.setUserDetailsService(userDetailsService);
accessTokenConverter.setUserTokenConverter(userTokenConverter);
remoteTokenServices.setRestTemplate(lbRestTemplate());
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler)
.tokenServices(remoteTokenServices);
}
}
@@ -1,23 +1,19 @@
package com.pig4cloud.pigx.common.security.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.*;
/**
@@ -21,7 +21,6 @@ package com.pig4cloud.pigx.common.security.feign;
import feign.RequestInterceptor;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
import org.springframework.context.annotation.Bean;
@@ -24,7 +24,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationEventPublisher;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;