Skip to content

Commit

Permalink
✨ (security) AuthenticationEntryPoint 不再默认注册,交由业务方自己控制
Browse files Browse the repository at this point in the history
link gh-318
  • Loading branch information
Hccake committed Oct 1, 2024
1 parent 60ceb39 commit c9846ff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,25 +66,17 @@ public BearerTokenResolver bearerTokenResolver() {
return defaultBearerTokenResolver;
}

/**
* 自定义异常处理
* @return AuthenticationEntryPoint
*/
@Bean
@ConditionalOnMissingBean
public AuthenticationEntryPoint authenticationEntryPoint() {
return new CustomAuthenticationEntryPoint();
}

/**
* 资源服务器的定制器
*/
@Bean
@ConditionalOnMissingBean(
name = BasicOauth2ResourceServerConfigurerCustomizer.BASIC_OAUTH2_RESOURCE_SERVER_CONFIGURER_CUSTOMIZER_BEAN_NAME)
public BasicOauth2ResourceServerConfigurerCustomizer basicOauth2ResourceServerConfigurerCustomizer(
AuthenticationEntryPoint authenticationEntryPoint, BearerTokenResolver bearerTokenResolver) {
return new BasicOauth2ResourceServerConfigurerCustomizer(authenticationEntryPoint, bearerTokenResolver);
ObjectProvider<AuthenticationEntryPoint> authenticationEntryPointObjectProvider,
BearerTokenResolver bearerTokenResolver) {
return new BasicOauth2ResourceServerConfigurerCustomizer(authenticationEntryPointObjectProvider,
bearerTokenResolver);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,17 +36,25 @@ public class BasicOauth2ResourceServerConfigurerCustomizer implements SpringSecu

private final BearerTokenResolver bearerTokenResolver;

public BasicOauth2ResourceServerConfigurerCustomizer(AuthenticationEntryPoint authenticationEntryPoint,
public BasicOauth2ResourceServerConfigurerCustomizer(
ObjectProvider<AuthenticationEntryPoint> 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<HttpSecurity> httpSecurityOAuth2ResourceServerConfigurer = httpSecurity
.oauth2ResourceServer();

// 认证错误处理
if (this.authenticationEntryPoint != null) {
httpSecurity.exceptionHandling().authenticationEntryPoint(this.authenticationEntryPoint);
}

httpSecurityOAuth2ResourceServerConfigurer
// bearToken 解析器
.bearerTokenResolver(this.bearerTokenResolver)
// 不透明令牌,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
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;
import org.springframework.context.annotation.Configuration;
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c9846ff

Please sign in to comment.