程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了401 请求,其中指定了 `permitAll()`大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决401 请求,其中指定了 `permitAll()`?

开发过程中遇到401 请求,其中指定了 `permitAll()`的问题如何解决?下面主要结合日常开发的经验,给出你关于401 请求,其中指定了 `permitAll()`的解决方法建议,希望对你解决401 请求,其中指定了 `permitAll()`有所启发或帮助;

我有这个 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,直到我通过了一些授权 - 这不适合此内点。

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

解决方法

您的请求可能被拒绝,因为您没有提供 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
,

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

大佬总结

以上是大佬教程为你收集整理的401 请求,其中指定了 `permitAll()`全部内容,希望文章能够帮你解决401 请求,其中指定了 `permitAll()`所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。