代码之家  ›  专栏  ›  技术社区  ›  ozgur

当在Spring Security中禁用CSRF时,Get请求未被授权

  •  -1
  • ozgur  · 技术社区  · 8 年前

    如果不添加以下内容,springsecurity基本身份验证就可以工作

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
        }
    }
    

    在我添加这个代码段之后,一个没有授权头的GET请求得到响应,而我期望一个响应说Unauthorized。在添加此配置之前,GET response GET 401

    唯一的变化是上面的类;其他一切都没有改变。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Royts    8 年前

    尝试添加 .anyRequest().authenticated() ,这意味着,所有请求都必须经过身份验证。如果你想增加一个免责条款 .antMatchers(HttpMethod.GET, "/", "/js/**", "/css/*", "/images/**").permitAll() 不会对其进行身份验证。

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable().authorizeRequests()
                .antMatchers(HttpMethod.GET, "/", "/js/**", "/css/*", "/images/**").permitAll()
                .anyRequest().authenticated();
        }
    }