401请求时,其中指定了'permitall()'

cu6pst1q  于 2021-09-30  发布在  Java
关注(0)|答案(2)|浏览(246)

我有以下Web安全配置适配器配置:

@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,直到我通过了一些授权-这不适合于这个网站。
我想这和 .anyRequest().authenticated() . 我以前读过,这不应该影响 permitAll() 我有没有弄错什么?

wf82jlnq

wf82jlnq1#

您的请求可能被拒绝,因为您没有提供csrf令牌。默认情况下,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
uqdfh47h

uqdfh47h2#

如果您使用的是jwt筛选器,则即使添加了permitall(),它也无法工作。如果删除该筛选器,它将正常工作。

相关问题