Spring Boot + Spring Security permitAll()和addFilter()配置没有效果

时间:2018-03-29 17:00:38

标签: spring spring-security

  1. 使用/ login的URL模式应该通过LoginFilter验证用户ID和密码 - 工作正常
  2. 带有/ users / register的URL模式不应该通过任何文件管理器,但它总是通过JWTAuthentication过滤器 - 不能正常工作
  3. 所有其他网址格式应通过JWTAuthentication过滤器进行授权 - 正常工作
  4. 以下是我的安全配置代码。请帮助我解释此代码中缺少的内容。如何配置过滤器,以便对/ login和/ register

    以外的URL模式进行JWT身份验证

    Spring-security-core:4.2.3,spring-boot:1.5.4

     protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers(HttpMethod.POST, "/login").permitAll()
                .antMatchers(HttpMethod.POST, "/users/register").permitAll()
                .anyRequest().authenticated()
                .and()
                // We filter the api/login requests
                .addFilterBefore(new LoginFilter("/login", authenticationManager()),
                        UsernamePasswordAuthenticationFilter.class)
                // And filter other requests to check the presence of JWT in header
                .addFilterBefore(new NoLoginAuthenticationFilter("/users/register"), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(new JWTAuthenticationFilter("/**", authenticationManager()),
                        UsernamePasswordAuthenticationFilter.class);
    
    }
    

1 个答案:

答案 0 :(得分:2)

您想要忽略某些网址。 为此,覆盖采用WebSecurity对象并忽略模式的configure方法。

尝试在配置类中添加以下方法覆盖。

@Override
public void configure(WebSecurity web) throws Exception {
    web
    .ignoring()
    .antMatchers("/users/register/**");
}