【问题标题】:Basic auth returns 403 in postman but works fine in browser基本身份验证在邮递员中返回 403,但在浏览器中工作正常
【发布时间】:2021-12-04 22:01:44
【问题描述】:

我已将 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-boot tomcat basic-authentication


    【解决方案1】:

    这很可能是由CSRF protection 引起的。

    来自docs

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 2013-10-08
      • 2021-07-12
      • 2015-03-20
      相关资源
      最近更新 更多