栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springsecurity中配置自定义过滤器后不通过http.addFilterBefore注册但是却能访问

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springsecurity中配置自定义过滤器后不通过http.addFilterBefore注册但是却能访问

HttpSecurity类中定义了初始化过滤器的集合

public final class HttpSecurity extends
		AbstractConfiguredSecurityBuilder
		implements 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;
	...
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/919107.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号