HttpSecurity类中定义了初始化过滤器的集合
public final class HttpSecurity extends AbstractConfiguredSecurityBuilderimplements SecurityBuilder , HttpSecurityBuilder { private final RequestMatcherConfigurer requestMatcherConfigurer; private List filters = new ArrayList<>(); private RequestMatcher requestMatcher = AnyRequestMatcher.INSTANCE; private FilterComparator comparator = new FilterComparator(); ...
自己实现的AbstractAuthenticationProcessingFilter 类
public class WebUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username";
public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password";
private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
private boolean postOnly = true;
public WebUsernamePasswordAuthenticationFilter() {
super(new AntPathRequestMatcher("/user/login", "POST"));
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
if (this.postOnly && !request.getMethod().equals("POST")) {
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
}
...
在继承了WebSecurityConfigurerAdapter的类中注册Bean对象
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
AuthenticationManager manager = super.authenticationManagerBean();
return manager;
}
WebUsernamePasswordAuthenticationFilter webUsernamePasswordAuthenticationFilter() throws Exception {
WebUsernamePasswordAuthenticationFilter filter = new WebUsernamePasswordAuthenticationFilter();
filter.setAuthenticationManager(authenticationManagerBean());
filter.setAuthenticationSuccessHandler(new WebAuthenticationSuccessHandler());
filter.setAuthenticationFailureHandler(new WebAuthenticationFailureHandler());
return filter;
}
直接通过Bean注解的方式注册过滤器,由于AbstractAuthenticationProcessingFilter实现了spring的ApplicationEventPublisherAware→Filter接口,所以当spring容器启动后会将WebUsernamePasswordAuthenticationFilter注入到Spring容器中,变成Spring的Servlet过滤器,而不是springsecurity过滤器,并且最终会被加到ApplicationFilterChain类的filters属性上,该属性代表了应用程序过滤器数组
另外在FilterComparator类中,已默认添加了UsernamePasswordAuthenticationFilter用户名认证过滤器
final class FilterComparator implements Comparator, Serializable { private static final int INITIAL_ORDER = 100; private static final int ORDER_STEP = 100; private final Map filterToOrder = new HashMap<>(); FilterComparator() { Step order = new Step(INITIAL_ORDER, ORDER_STEP); put(ChannelProcessingFilter.class, order.next()); put(ConcurrentSessionFilter.class, order.next()); put(WebAsyncManagerIntegrationFilter.class, order.next()); put(SecurityContextPersistenceFilter.class, order.next()); put(HeaderWriterFilter.class, order.next()); put(CorsFilter.class, order.next()); put(CsrfFilter.class, order.next()); put(LogoutFilter.class, order.next()); filterToOrder.put( "org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter", order.next()); filterToOrder.put( "org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationRequestFilter", order.next()); put(X509AuthenticationFilter.class, order.next()); put(AbstractPreAuthenticatedProcessingFilter.class, order.next()); filterToOrder.put("org.springframework.security.cas.web.CasAuthenticationFilter", order.next()); filterToOrder.put( "org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter", order.next()); filterToOrder.put( "org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationFilter", order.next()); put(UsernamePasswordAuthenticationFilter.class, order.next()); put(ConcurrentSessionFilter.class, order.next()); ...
最终springsecurity注册的Filter过滤器都会被添加到DefaultSecurityFilterChain类的filters属性上,然后等待客户端请求到来,被方法获取值后循环执行过滤链
public final class DefaultSecurityFilterChain implements SecurityFilterChain {
private static final Log logger = LogFactory.getLog(DefaultSecurityFilterChain.class);
private final RequestMatcher requestMatcher;
private final List filters;
...



