Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring security AuthenticationServiceException已转换为InsufficientAuthenticationException_Spring_Exception_Spring Security - Fatal编程技术网

Spring security AuthenticationServiceException已转换为InsufficientAuthenticationException

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

我在使用Spring Security进行身份验证期间遇到异常处理问题

这是我的
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时,使用的是未经身份验证的,因此拒绝访问。在我的案例中,上述问题中使用了基于肯定的实现来解决问题