有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 401 on请求,其中指定了'permitAll()'

我有以下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()的-我有没有误解了什么


共 (2) 个答案

  1. # 1 楼答案

    如果使用jwt过滤器,即使添加permitAll(),它也不会工作。如果你拆下过滤器,它会工作得很好

  2. # 2 楼答案

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