Spring security AuthenticationServiceException已转换为InsufficientAuthenticationException
我在使用Spring Security进行身份验证期间遇到异常处理问题 这是我的Spring security AuthenticationServiceException已转换为InsufficientAuthenticationException,spring,exception,spring-security,Spring,Exception,Spring Security,我在使用Spring Security进行身份验证期间遇到异常处理问题 这是我的AuthenticationProvider,其中每个异常都会引发AuthenticationServiceException @Component public class MyAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authent
AuthenticationProvider
,其中每个异常都会引发AuthenticationServiceException
@Component
public class MyAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
try {
// ...
} catch (Exception e) {
throw new AuthenticationServiceException(e.getMessage(), e);
}
}
}
下面是我的自定义身份验证提供程序
@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
log.debug(e.toString());
}
}
这是安全配置
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationProvider authenticationProvider;
@Autowired
private AuthenticationEntryPoint authenticationEntryPoint;
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.authenticationProvider(authenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint);
}
}
一切按预期触发。问题在于,在AuthenticationProvider
中,AuthenticationException
是UnficientAuthenticationException
的一个实例,而不是AuthenticationServiceException
引发的AuthenticationServiceException
。
在MyAuthenticationEntryPoint
中,我想要的是引发原因集的异常,这是一个自定义异常
我怎样才能解决这个问题?
为什么Spring会将一个AuthenticationServiceException
替换为一个不充分的AuthenticationException
提前谢谢
解决方案
我找到了解决办法!问题出在SecurityConfig
类中.authenticationEntryPoint(authenticationEntryPoint)
必须位于.httpBasic()
下,并且不能全局设置
正确的配置如下所示:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.csrf().disable();
}
只是一个健全性检查:还有
AuthorizationServiceException
-拼写很接近,因此您能否确认确实抛出了AuthenticationServiceException
(编译器不会捕捉到不匹配,因为两者都是运行时异常)您的意思是,在MyAuthenticationEntryPoint中,AuthenticationException是UnficientAuthenticationException的一个实例。改变它,它是混乱的。顺便说一句,我有同样的问题,如果它是一个问题。我能想到的唯一一件事就是尝试通过调试跟踪流程,看看Spring在哪里进行转换,以及是否有关闭转换的设置。我知道Spring有时会这样做,因为我遵循了“usernamenotfoundexception”的抛出,并且每次都会转换为“badcredentialsexception”。如果你找到了解决办法,就把它贴出来。Thx@jzheaux如上所示,使用的异常是AuthenticationServiceException。我也尝试过BadCredentialsException,它是org.springframework.security.core.AuthenticationException的另一个实现。@Merv我遵循流程,问题是当涉及AccessDecisionManager时,使用的是未经身份验证的,因此拒绝访问。在我的案例中,上述问题中使用了基于肯定的实现来解决问题