⚡ 优化代码,区别重写和默认情况
This commit is contained in:
1 parent
45ee93c0eb
commit
547ae5cd90
15 files changed
+81
-39
No files matched your search
+4
@@ -19,6 +19,8 @@ package com.pig4cloud.pigx.common.security.annotation;
|
||||
|
||||
import com.pig4cloud.pigx.common.security.component.PigxResourceServerAutoConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@@ -32,6 +34,8 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@EnableResourceServer
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
@Import(PigxResourceServerAutoConfiguration.class)
|
||||
public @interface EnablePigxResourceServer {
|
||||
}
|
||||
+4
-16
@@ -19,14 +19,9 @@ package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
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.token.DefaultAccessTokenConverter;
|
||||
@@ -42,9 +37,6 @@ import org.springframework.web.client.RestTemplate;
|
||||
* 2. 支持 获取用户全部信息
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableResourceServer
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
|
||||
@Autowired
|
||||
protected ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
|
||||
@@ -56,6 +48,8 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
|
||||
protected UserDetailsService userDetailsService;
|
||||
@Autowired
|
||||
private PermitAllUrlProperties permitAllUrlProperties;
|
||||
@Autowired
|
||||
private RestTemplate lbRestTemplate;
|
||||
|
||||
|
||||
/**
|
||||
@@ -92,12 +86,6 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
|
||||
canGetUser(resources);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public RestTemplate lbRestTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 不获取用户详细 只有用户名
|
||||
@@ -109,7 +97,7 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
|
||||
DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
|
||||
accessTokenConverter.setUserTokenConverter(userTokenConverter);
|
||||
|
||||
remoteTokenServices.setRestTemplate(lbRestTemplate());
|
||||
remoteTokenServices.setRestTemplate(lbRestTemplate);
|
||||
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
|
||||
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
|
||||
.accessDeniedHandler(pigAccessDeniedHandler)
|
||||
@@ -128,7 +116,7 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
|
||||
userTokenConverter.setUserDetailsService(userDetailsService);
|
||||
accessTokenConverter.setUserTokenConverter(userTokenConverter);
|
||||
|
||||
remoteTokenServices.setRestTemplate(lbRestTemplate());
|
||||
remoteTokenServices.setRestTemplate(lbRestTemplate);
|
||||
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
|
||||
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
|
||||
.accessDeniedHandler(pigAccessDeniedHandler)
|
||||
|
||||
+18
@@ -17,7 +17,13 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
@@ -25,4 +31,16 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
*/
|
||||
@ComponentScan("com.pig4cloud.pigx.common.security")
|
||||
public class PigxResourceServerAutoConfiguration {
|
||||
@Bean
|
||||
@Primary
|
||||
@LoadBalanced
|
||||
public RestTemplate lbRestTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ResourceServerConfigurerAdapter.class)
|
||||
public ResourceServerConfigurerAdapter resourceServerConfigurerAdapter() {
|
||||
return new BaseResourceServerConfigurerAdapter();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user