Spring Security + Basic Auth + API返回404而不是401

时间:2017-04-20 21:14:58

标签: spring-mvc spring-security basic-authentication

我有一个使用基本身份验证运行spring security的spring启动应用程序。提供适当的基本身份验证凭据时,将调用控制器方法,但对于不正确的身份验证凭据,我将获得404 Not Found而不是401 Unauthorized。

这是我的Spring Security Config

@Configuration
public static class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .antMatcher("/useradmin/api")
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .httpBasic().authenticationEntryPoint(authenticationEntryPoint()).and().csrf().disable();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("default").password("password").roles("USER");
    }
}

我已审核了Spring Security with basic auth redirecting to /error for invalid credentials,如果我从自动配置类中排除ErrorMvcAutoConfiguration.class,我会得到401.但是,如果我将其保留为自动配置,我只会得到404,实际上它没有被重定向前面提到的/error问题说。

我已经创建了自己的BasicErrorController实现,我在getError方法上设置了一个断点并且它从未到达,所以我假设其他东西正在捕获响应并将其状态从401更改为404。

还有什么可能是这个问题的根源?

0 个答案:

没有答案