|
@@ -17,18 +17,16 @@
|
|
|
|
|
|
package com.pig4cloud.pigx.common.security.component;
|
|
package com.pig4cloud.pigx.common.security.component;
|
|
|
|
|
|
-import lombok.Setter;
|
|
|
|
import lombok.SneakyThrows;
|
|
import lombok.SneakyThrows;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
|
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.ResourceServerConfigurerAdapter;
|
|
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.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
|
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
|
|
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.security.oauth2.provider.token.RemoteTokenServices;
|
|
|
|
+import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -45,13 +43,9 @@ public class PigxResourceServerConfigurerAdapter extends ResourceServerConfigure
|
|
@Autowired
|
|
@Autowired
|
|
protected RemoteTokenServices remoteTokenServices;
|
|
protected RemoteTokenServices remoteTokenServices;
|
|
@Autowired
|
|
@Autowired
|
|
- protected UserDetailsService userDetailsService;
|
|
|
|
- @Autowired
|
|
|
|
private PermitAllUrlProperties permitAllUrlProperties;
|
|
private PermitAllUrlProperties permitAllUrlProperties;
|
|
@Autowired
|
|
@Autowired
|
|
private RestTemplate lbRestTemplate;
|
|
private RestTemplate lbRestTemplate;
|
|
- @Setter
|
|
|
|
- private boolean details;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 默认的配置,对外暴露
|
|
* 默认的配置,对外暴露
|
|
@@ -72,58 +66,15 @@ public class PigxResourceServerConfigurerAdapter extends ResourceServerConfigure
|
|
.and().csrf().disable();
|
|
.and().csrf().disable();
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 提供子类重写
|
|
|
|
- * <p>
|
|
|
|
- * 1. 不重写,默认支持获取用户名
|
|
|
|
- * 2. 重写notGetUser,提供性能
|
|
|
|
- * <p>
|
|
|
|
- * see codegen ResourceServerConfigurer
|
|
|
|
- *
|
|
|
|
- * @param resources
|
|
|
|
- */
|
|
|
|
@Override
|
|
@Override
|
|
public void configure(ResourceServerSecurityConfigurer resources) {
|
|
public void configure(ResourceServerSecurityConfigurer resources) {
|
|
- if (details) {
|
|
|
|
- canGetUser(resources);
|
|
|
|
- } else {
|
|
|
|
- notGetUser(resources);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 不获取用户详细 只有用户名
|
|
|
|
- *
|
|
|
|
- * @param resources
|
|
|
|
- */
|
|
|
|
- protected void notGetUser(ResourceServerSecurityConfigurer resources) {
|
|
|
|
DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
|
|
DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
|
|
- DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
|
|
|
|
|
|
+ UserAuthenticationConverter userTokenConverter = new PigxUserAuthenticationConverter();
|
|
accessTokenConverter.setUserTokenConverter(userTokenConverter);
|
|
accessTokenConverter.setUserTokenConverter(userTokenConverter);
|
|
|
|
|
|
remoteTokenServices.setRestTemplate(lbRestTemplate);
|
|
remoteTokenServices.setRestTemplate(lbRestTemplate);
|
|
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
|
|
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
|
|
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
|
|
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
|
|
- .tokenServices(remoteTokenServices);
|
|
|
|
|
|
+ .tokenServices(remoteTokenServices);
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 上下文中获取用户全部信息,两次调用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)
|
|
|
|
- .tokenServices(remoteTokenServices);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|