【问题标题】:401 on request where `permitAll()` specified401 on request where `permitAll()` 指定
【发布时间】:2021-09-02 16:03:55
【问题描述】:

我有这个 WebSecurityConfigurerAdapter 配置:

@EnableWebSecurity
public class HttpSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.cors()
                .and()
                .authorizeRequests()

                .mvcMatchers("/auth/**").permitAll()

                .anyRequest().authenticated()

                .and()
                .oauth2ResourceServer().jwt()
        ;
    }
}

当我向auth 发出请求时,我得到一个 401,直到我通过了一些授权 - 这不适合这个 endopint。

我认为这与.anyRequest().authenticated() 有关。我之前读过这不应该影响permitAll()s - 我是否配置错误?

【问题讨论】:

  • 有没有返回401的路径示例
  • 您是否使用 @EnableWebSecurity 注释了您的配置类?
  • @AkifHadziabdic "POST /auth/test"
  • @Nemanja 是的 org.springframework.security.config.annotation.web.configuration.EnableWebSecurity

标签: java spring spring-boot security spring-security


【解决方案1】:

您的请求可能被拒绝,因为您没有提供CSRF token。默认情况下,Spring Security 会为每个 POST 请求启用它,您需要显式禁用它。

@EnableWebSecurity
public class HttpSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.cors()
                .and()
                .csrf().disable()
                .authorizeRequests()
                .mvcMatchers("/auth/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .oauth2ResourceServer().jwt();
    }
}

您可以将以下属性添加到您的 application.yml 文件中,这样您就可以看到如果 CSRF 不是这种情况,您的请求被拒绝的原因:

logging:
  level:
    org.springframework.security: TRACE

【讨论】:

  • 这实际上是问题
【解决方案2】:

如果您使用的是 jwt 过滤器,即使您添加了 permitAll() 也无法正常工作。如果您删除过滤器,它将正常工作。

【讨论】:

猜你喜欢
  • 2022-12-05
  • 2020-07-24
  • 1970-01-01
  • 2021-05-25
  • 2019-03-25
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
相关资源
最近更新 更多