Spring Security之 web.xml 源码分析(三)

Spring Security之 web.xml 源码分析(三)

本篇文章我们来分析一下在web.xmlDelegatingFilterProxy的名字<filter-name>为什么必须是springSecurityFilterChainspringSecurityFilterChain究竟做了什么


  1. web.xml配置了一个DelegatingFilterProxy的filter,而DelegatingFilterProxy的名字<filter-name>必须是springSecurityFilterChain,因为springSecurityFilterChain是Spring容器帮我们去初始化的一个filter


    在这里插入图片描述

  2. 打开执行类DelegatingFilterProxy的源码,发现DelegatingFilterProxy它并不是filter,它继承的类GenericFilterBean才实现了Filter接口


    在这里插入图片描述
    在这里插入图片描述

  3. 我们关注下DelegatingFilterProxy这个类中的doFilter()方法,而在doFilter()方法,doFilter()方法中具体使用的是delegate这个FilterDelegatingFilterProxy并不是真正的Filter,真正做事的是delegate


    在这里插入图片描述

  4. delegate这个属性在DelegatingFilterProxy这个类开头已经有了申明


    在这里插入图片描述

  5. 然后我们再来看到DelegatingFilterProxy类中的initFilterBean()方法中找到delegate的创建,里面有个targetBeanName通过getFilterName()获取到值


    在这里插入图片描述


  1. 再找到getFilterName()这个方法,而这个方法就是去web.xml中获取<filter-name>值给targetBeanName,这里就回答了第一个问题,因为getFilterName()这个方法是到web.xml中获取<filter-name>值给targetBeanName,所以我们web.xml中<filter-name>的值必须是springSecurityFilterChain

    在这里插入图片描述

  2. 我们再回到initFilterBean()方法中的delegatedelegate它是通过initDelegate(wac)去注入的


    在这里插入图片描述

8.所以我们再来看看initDelegate(wac)这个方法是怎么执行的,它里面再次出现了delegatetargetBeanName这两个属性,所以最终把targetBeanName的值赋值给了delegate


在这里插入图片描述

9.当initFilterBean()方法中的delegate获取到值后,delegate把值传到最初我们看到的doFilter(),再把值赋值给delegateToUse


在这里插入图片描述

  1. DelegatingFilterProxy类中的invokeDelegate()给我们描述了delegateToUse在做什么事


    在这里插入图片描述

  2. 查看invokeDelegate()方法调用了doFilter(request, response, filterChain)方法

在这里插入图片描述

  1. 再查看doFilter(request, response, filterChain)方法,发现调用了Filter接口


    在这里插入图片描述

  2. 此接口的doFilter()方法实际是找了具体实现类FilterChainProxydoFilter()


    在这里插入图片描述

  3. FilterChainProxy中不管什么情况都会执行doFilterInternal(request, response, chain)这个方法


在这里插入图片描述

  1. 查看doFilterInternal(request, response, chain)这个方法,里面会获取一个List集合,里面就是Spring Security在运行中所有默认filter,然乎一个个加载去执行

在这里插入图片描述

  1. Spring Security在运行了哪些默认filter,我们可以查看SecurityFilters这个类
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.security.config.http;

import org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter;

/**
 * Stores the default order numbers of all Spring Security filters for use in
 * configuration.
 *
 * @author Luke Taylor
 * @author Rob Winch
 */

enum SecurityFilters {
	FIRST(Integer.MIN_VALUE),
	CHANNEL_FILTER,
	SECURITY_CONTEXT_FILTER,
	CONCURRENT_SESSION_FILTER,
	WEB_ASYNC_MANAGER_FILTER /** {@link WebAsyncManagerIntegrationFilter} */,
	HEADERS_FILTER, CORS_FILTER,
	CSRF_FILTER,
	LOGOUT_FILTER,
	X509_FILTER,
	PRE_AUTH_FILTER,
	CAS_FILTER,
	FORM_LOGIN_FILTER,
	OPENID_FILTER,
	LOGIN_PAGE_FILTER,
	DIGEST_AUTH_FILTER,
	BASIC_AUTH_FILTER,
	REQUEST_CACHE_FILTER,
	SERVLET_API_SUPPORT_FILTER,
	JAAS_API_SUPPORT_FILTER,
	REMEMBER_ME_FILTER,
	ANONYMOUS_FILTER,
	SESSION_MANAGEMENT_FILTER,
	EXCEPTION_TRANSLATION_FILTER,
	FILTER_SECURITY_INTERCEPTOR,
	SWITCH_USER_FILTER,
	LAST(Integer.MAX_VALUE);

	private static final int INTERVAL = 100;
	private final int order;

	private SecurityFilters() {
		order = ordinal() * INTERVAL;
	}

	private SecurityFilters(int order) {
		this.order = order;
	}

	public int getOrder() {
		return order;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值