Java Spring安全筛选器链不忽略指定的路径

Java Spring安全筛选器链不忽略指定的路径,java,spring-mvc,spring-security,Java,Spring Mvc,Spring Security,我有一个SpringBootRESTAPI,它通过JWT进行身份验证。我的问题是,我已将Spring Security配置为允许对用于身份验证的路径进行不受限制的访问/auth/token,但它仍然会在不应该的情况下命中我的安全过滤器。我不知道我哪里做错了任何建议都是非常恰当的 安全配置 public class JwtWebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private JwtA

我有一个SpringBootRESTAPI,它通过JWT进行身份验证。我的问题是,我已将Spring Security配置为允许对用于身份验证的路径进行不受限制的访问
/auth/token
,但它仍然会在不应该的情况下命中我的安全过滤器。我不知道我哪里做错了任何建议都是非常恰当的

安全配置

public class JwtWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) 
        throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.userDetailsService)
                .passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public JwtAuthenticationFilter authenticationTokenFilter() throws Exception {
        return new JwtAuthenticationFilter();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .authorizeRequests()
            .antMatchers("/auth/token").permitAll() // do not authenticate
            .anyRequest().authenticated()

            // TODO: configure
            .cors()
            .and()

            // TODO enable and configure
            .csrf().disable()

            // Unauthorized request handler
            .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()

            // Keep application security stateless
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        // JWT security filter
        httpSecurity.addFilterBefore(authenticationTokenFilter(), 
            UsernamePasswordAuthenticationFilter.class);

        // Disable page caching to prevent cached REST responses
        httpSecurity.headers().cacheControl();
    }

    @Override
    public void configure(WebSecurity webSecurity) throws Exception {
        webSecurity.ignoring().antMatchers(HttpMethod.POST, "/auth/token");
    }
}
过滤器

public class JwtAuthenticationFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain chain) throws ServletException, IOException {
        // this runs
    }
}
控制器

@RestController
public class AuthenticationController {

    // Authenticate user
    @RequestMapping(value = "/auth/token", method = RequestMethod.POST)
    public ResponseEntity<?> createAuthenticationToken(HttpServletResponse response,
        @RequestBody JwtAuthenticationRequest authenticationRequest, Device device) throws AuthenticationException {
        // never gets to run
    }
}
@RestController
公共类身份验证控制器{
//验证用户
@RequestMapping(value=“/auth/token”,method=RequestMethod.POST)
public ResponseEntity createAuthenticationToken(HttpServletResponse,
@RequestBody JwtAuthenticationRequest authenticationRequest,设备设备)引发AuthenticationException{
//永远也跑不动
}
}
只是一个建议

有没有可能,您所使用的不是身份验证,而是cors

身份验证-HTTP状态401

CORS-HTTP状态403

如果启用cors而不进行配置,则不允许任何CRO源请求。 从CorsConfiguration javadoc:

默认情况下,新创建的CorsConfiguration不允许任何跨源请求,必须明确配置以指示应允许的内容


感谢@dur询问我是否在使用Spring Boot,这让我找到了解决方案

这让我开始思考Spring Boot是如何在飞行中自动为我们创建bean的,这最终成为了罪魁祸首。原来我的
JwtAuthenticationFilter
类被Spring Boot自动放入过滤器链中,但当我在安全配置中显式声明它时,它也被包含在安全过滤器链中。因此,尽管我在security config中的
忽略()
方法中排除了
/auth/token
是正确的,但这还不足以阻止过滤器在Spring引导本身的上下文中发生。解决方案是配置一个bean,显式地阻止它被springboot添加

@Bean
public RegistrationBean jwtAuthFilterRegister(JwtAuthenticationFilter filter) {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
    registrationBean.setEnabled(false);
    return registrationBean;
}

很抱歉,我实际上在另一个类中有一个cors配置bean,现在它允许从所有映射进行访问。应该提供这一点。