首页 > 解决方案 > 基本身份验证在邮递员中返回 403,但在浏览器中工作正常

问题描述

我已将 securityConfig 定义如下:

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .passwordEncoder(passwordEncoder())
                .usersByUsernameQuery(\\some code\\)
                .authoritiesByUsernameQuery(\\some code\\)
                .getUserDetailsService();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .httpBasic().authenticationEntryPoint(entryPoint);
    }

当我在浏览器的表单中输入凭据时,它可以正常工作,

在我的浏览器中形成

但是当我尝试使用基本身份验证表单在邮递员中发送请求时邮差

我收到 403 错误。我究竟做错了什么?

标签: spring-boottomcatbasic-authentication

解决方案


这很可能是由CSRF 保护引起的。

文档

我们的建议是对普通用户可以通过浏览器处理的任何请求使用 CSRF 保护。如果您只创建非浏览器客户端使用的服务,您可能希望禁用 CSRF 保护。

CSRF 保护可以按如下方式禁用:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .httpBasic().authenticationEntryPoint(entryPoint)
            .and()
            .csrf().csrf().disable()
}

推荐阅读