Spring Security 之 编写自定义Filter的意外
通过写个Filter会采用@Component进行注解,然后由环境进行自动注入到过滤链上
@Component
public class MyFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
}
}
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyFilter myFilter;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter.class);
}
}
以上逻辑是写一个自定义过滤器,通过配置添加到SpringSecurity过滤链中。在Spring Boot环境运行中,就会发现Spring MVC主过滤链上也存在一个MyFilter的相同实例,即请求在符合要求的情况下,会经过MyFilter过滤器两次