首页 > 解决方案 > 未经身份验证的调用返回 200 状态

问题描述

我有一个弹簧启动应用程序。我添加了一个安全层,如下所示:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${allowed.paths}")
    private List<String> allowedPaths;

    @Autowired
    private TestCenterAuthProvider authProvider;

    @Override
    public void configure(AuthenticationManagerBuilder auth){
        auth.authenticationProvider(authProvider);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .cors().disable()
                .authorizeRequests()
                .antMatchers(allowedPaths.toArray(new String[0]))
                .permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .permitAll();
    }
}

这默认为POST localhost:8080/login端点提供了默认的 HTML 表示。

现在,我所做的任何未经身份验证的调用都会返回 200 OK,并带有登录页面的 HTML 响应。我需要这个来返回 403。我无法弄清楚这一点。

标签: spring-bootspring-security

解决方案


插入

.exceptionHandling()
                .defaultAuthenticationEntryPointFor(
                        new Http403ForbiddenEntryPoint(),
                        new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"))
            .and()
            .authorizeRequests()

启用 403


推荐阅读