diff --git a/security/ballcat-spring-security-oauth2-resource-server/src/main/java/org/ballcat/springsecurity/oauth2/server/resource/OAuth2ResourceServerAutoConfiguration.java b/security/ballcat-spring-security-oauth2-resource-server/src/main/java/org/ballcat/springsecurity/oauth2/server/resource/OAuth2ResourceServerAutoConfiguration.java index 833f475f5..a9e6250ba 100644 --- a/security/ballcat-spring-security-oauth2-resource-server/src/main/java/org/ballcat/springsecurity/oauth2/server/resource/OAuth2ResourceServerAutoConfiguration.java +++ b/security/ballcat-spring-security-oauth2-resource-server/src/main/java/org/ballcat/springsecurity/oauth2/server/resource/OAuth2ResourceServerAutoConfiguration.java @@ -24,7 +24,7 @@ import org.ballcat.springsecurity.oauth2.server.resource.introspection.SpringAuthorizationServerSharedStoredOpaqueTokenIntrospector; import org.ballcat.springsecurity.oauth2.server.resource.properties.OAuth2ResourceServerProperties; import org.ballcat.springsecurity.properties.SpringSecurityProperties; -import org.ballcat.springsecurity.web.CustomAuthenticationEntryPoint; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -66,16 +66,6 @@ public BearerTokenResolver bearerTokenResolver() { return defaultBearerTokenResolver; } - /** - * 自定义异常处理 - * @return AuthenticationEntryPoint - */ - @Bean - @ConditionalOnMissingBean - public AuthenticationEntryPoint authenticationEntryPoint() { - return new CustomAuthenticationEntryPoint(); - } - /** * 资源服务器的定制器 */ @@ -83,8 +73,10 @@ public AuthenticationEntryPoint authenticationEntryPoint() { @ConditionalOnMissingBean( name = BasicOauth2ResourceServerConfigurerCustomizer.BASIC_OAUTH2_RESOURCE_SERVER_CONFIGURER_CUSTOMIZER_BEAN_NAME) public BasicOauth2ResourceServerConfigurerCustomizer basicOauth2ResourceServerConfigurerCustomizer( - AuthenticationEntryPoint authenticationEntryPoint, BearerTokenResolver bearerTokenResolver) { - return new BasicOauth2ResourceServerConfigurerCustomizer(authenticationEntryPoint, bearerTokenResolver); + ObjectProvider authenticationEntryPointObjectProvider, + BearerTokenResolver bearerTokenResolver) { + return new BasicOauth2ResourceServerConfigurerCustomizer(authenticationEntryPointObjectProvider, + bearerTokenResolver); } /** diff --git a/security/ballcat-spring-security-oauth2-resource-server/src/main/java/org/ballcat/springsecurity/oauth2/server/resource/configurer/BasicOauth2ResourceServerConfigurerCustomizer.java b/security/ballcat-spring-security-oauth2-resource-server/src/main/java/org/ballcat/springsecurity/oauth2/server/resource/configurer/BasicOauth2ResourceServerConfigurerCustomizer.java index c23cf758a..25025648d 100644 --- a/security/ballcat-spring-security-oauth2-resource-server/src/main/java/org/ballcat/springsecurity/oauth2/server/resource/configurer/BasicOauth2ResourceServerConfigurerCustomizer.java +++ b/security/ballcat-spring-security-oauth2-resource-server/src/main/java/org/ballcat/springsecurity/oauth2/server/resource/configurer/BasicOauth2ResourceServerConfigurerCustomizer.java @@ -17,7 +17,9 @@ package org.ballcat.springsecurity.oauth2.server.resource.configurer; import org.ballcat.springsecurity.configuer.SpringSecurityConfigurerCustomizer; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer; import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver; import org.springframework.security.web.AuthenticationEntryPoint; @@ -34,17 +36,25 @@ public class BasicOauth2ResourceServerConfigurerCustomizer implements SpringSecu private final BearerTokenResolver bearerTokenResolver; - public BasicOauth2ResourceServerConfigurerCustomizer(AuthenticationEntryPoint authenticationEntryPoint, + public BasicOauth2ResourceServerConfigurerCustomizer( + ObjectProvider authenticationEntryPointObjectProvider, BearerTokenResolver bearerTokenResolver) { - this.authenticationEntryPoint = authenticationEntryPoint; + this.authenticationEntryPoint = authenticationEntryPointObjectProvider.getIfAvailable(); this.bearerTokenResolver = bearerTokenResolver; } @Override public void customize(HttpSecurity httpSecurity) throws Exception { // 开启 OAuth2 资源服务 - httpSecurity.oauth2ResourceServer() - .authenticationEntryPoint(this.authenticationEntryPoint) + OAuth2ResourceServerConfigurer httpSecurityOAuth2ResourceServerConfigurer = httpSecurity + .oauth2ResourceServer(); + + // 认证错误处理 + if (this.authenticationEntryPoint != null) { + httpSecurity.exceptionHandling().authenticationEntryPoint(this.authenticationEntryPoint); + } + + httpSecurityOAuth2ResourceServerConfigurer // bearToken 解析器 .bearerTokenResolver(this.bearerTokenResolver) // 不透明令牌, diff --git a/security/ballcat-spring-security/src/main/java/org/ballcat/springsecurity/configuration/SpringSecurityComponentConfiguration.java b/security/ballcat-spring-security/src/main/java/org/ballcat/springsecurity/configuration/SpringSecurityComponentConfiguration.java index 706919440..89199bdfd 100644 --- a/security/ballcat-spring-security/src/main/java/org/ballcat/springsecurity/configuration/SpringSecurityComponentConfiguration.java +++ b/security/ballcat-spring-security/src/main/java/org/ballcat/springsecurity/configuration/SpringSecurityComponentConfiguration.java @@ -20,7 +20,6 @@ import org.ballcat.springsecurity.authorization.SpringSecurityChecker; import org.ballcat.springsecurity.component.CustomPermissionEvaluator; import org.ballcat.springsecurity.util.PasswordUtils; -import org.ballcat.springsecurity.web.CustomAuthenticationEntryPoint; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; @@ -28,7 +27,6 @@ import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.web.AuthenticationEntryPoint; /** * @author Hccake @@ -65,16 +63,6 @@ public DaoAuthenticationProvider daoAuthenticationProvider(UserDetailsService us return daoAuthenticationProvider; } - /** - * 自定义异常处理 - * @return AuthenticationEntryPoint - */ - @Bean - @ConditionalOnMissingBean - public AuthenticationEntryPoint authenticationEntryPoint() { - return new CustomAuthenticationEntryPoint(); - } - /** * 基于 spring security 的权限判断组件 * @return SpringSecurityChecker