日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Spring Security常用過濾器實例解析

瀏覽:130日期:2023-09-19 08:22:50

Spring Security常見的15個攔截器

1 . org.springframework.security.web.context.SecurityContextPersistenceFilter

首當其沖的一個過濾器,作用之重要,自不必多言。

SecurityContextPersistenceFilter主要是使用SecurityContextRepository在session中保存或更新一個 SecurityContext,并將SecurityContext給以后的過濾器使用,來為后續filter建立所需的上下文。 SecurityContext中存儲了當前用戶的認證以及權限信息。

2 . org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter

此過濾器用于集成SecurityContext到Spring異步執行機制中的WebAsyncManager

3 . org.springframework.security.web.header.HeaderWriterFilter

向請求的Header中添加相應的信息,可在http標簽內部使用security:headers來控制

4 . org.springframework.security.web.csrf.CsrfFilter

csrf又稱跨域請求偽造,SpringSecurity會對所有post請求驗證是否包含系統生成的csrf的token信息,

如果不包含,則報錯。起到防止csrf攻擊的效果。

5. org.springframework.security.web.authentication.logout.LogoutFilter

匹配 URL為/logout的請求,實現用戶退出,清除認證信息。

6 . org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

認證操作全靠這個過濾器,默認匹配URL為/login且必須為POST請求。

7 . org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter

如果沒有在配置文件中指定認證頁面,則由該過濾器生成一個默認認證頁面。

8 . org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter

由此過濾器可以生產一個默認的退出登錄頁面

9 . org.springframework.security.web.authentication.www.BasicAuthenticationFilter

此過濾器會自動解析HTTP請求中頭部名字為Authentication,且以Basic開頭的頭信息。

10 . org.springframework.security.web.savedrequest.RequestCacheAwareFilter

通過HttpSessionRequestCache內部維護了一個RequestCache,用于緩存HttpServletRequest

11 . org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter

針對ServletRequest進行了一次包裝,使得request具有更加豐富的API

12 . org.springframework.security.web.authentication.AnonymousAuthenticationFilter

當SecurityContextHolder中認證信息為空,則會創建一個匿名用戶存入到SecurityContextHolder中。

spring security為了兼容未登錄的訪問,也走了一套認證流程,只不過是一個匿名的身份。

13 . org.springframework.security.web.session.SessionManagementFilter

SecurityContextRepository限制同一用戶開啟多個會話的數量

14 . org.springframework.security.web.access.ExceptionTranslationFilter

異常轉換過濾器位于整個springSecurityFilterChain的后方,用來轉換整個鏈路中出現的異常

15 . org.springframework.security.web.access.intercept.FilterSecurityInterceptor

獲取所配置資源訪問的授權信息,根據SecurityContextHolder中存儲的用戶信息來決定其是否有權限。

那么,是不是spring security一共就這么多過濾器呢?答案是否定的!隨著spring-security.xml配置的添加,還會出現新的過濾器。

那么,是不是spring security每次都會加載這些過濾器呢?答案也是否定的!隨著spring-security.xml配置的修改,有些過濾器可能會被去掉。

spring security 過濾器鏈加載原理

public class DelegatingFilterProxy extends GenericFilterBean {@Nullableprivate String contextAttribute;@Nullableprivate WebApplicationContext webApplicationContext;@Nullableprivate String targetBeanName;private boolean targetFilterLifecycle;@Nullableprivate volatile Filter delegate;//注:這個過濾器才是真正加載的過濾器private final Object delegateMonitor;//注:doFilter才是過濾器的入口,直接從這看!public void doFilter(ServletRequest request, ServletResponse response, FilterChainfilterChain) throws ServletException, IOException {Filter delegateToUse = this.delegate;if (delegateToUse == null) {synchronized(this.delegateMonitor) {delegateToUse = this.delegate;if (delegateToUse == null) {WebApplicationContext wac = this.findWebApplicationContext();if (wac == null) {throw new IllegalStateException('No WebApplicationContext found: noContextLoaderListener or DispatcherServlet registered?');}//第一步:doFilter中最重要的一步,初始化上面私有過濾器屬性delegatedelegateToUse = this.initDelegate(wac);}this.delegate = delegateToUse;}}//第三步:執行FilterChainProxy過濾器this.invokeDelegate(delegateToUse, request, response, filterChain);}//第二步:直接看最終加載的過濾器到底是誰protected Filter initDelegate(WebApplicationContext wac) throws ServletException {//debug得知targetBeanName為:springSecurityFilterChainString targetBeanName = this.getTargetBeanName();Assert.state(targetBeanName != null, 'No target bean name set');//debug得知delegate對象為:FilterChainProxyFilter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);if (this.isTargetFilterLifecycle()) {delegate.init(this.getFilterConfig());}return delegate;}protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponseresponse, FilterChain filterChain) throws ServletException, IOException {delegate.doFilter(request, response, filterChain);}}

第二步debug結果如下:

Spring Security常用過濾器實例解析

Spring Security常用過濾器實例解析

由此可知, DelegatingFilterProxy通過springSecurityFilterChain這個名稱,得到了一個FilterChainProxy過濾器,最終在第三步執行了這個過濾器。

FilterChainProxy

public class FilterChainProxy extends GenericFilterBean {private static final Log logger = LogFactory.getLog(FilterChainProxy.class);private static final String FILTER_APPLIED =FilterChainProxy.class.getName().concat('.APPLIED');private List<SecurityFilterChain> filterChains;private FilterChainProxy.FilterChainValidator filterChainValidator;private HttpFirewall firewall;//咿!?可以通過一個叫SecurityFilterChain的對象實例化出一個FilterChainProxy對象//這FilterChainProxy又是何方神圣?會不會是真正的過濾器鏈對象呢?先留著這個疑問!public FilterChainProxy(SecurityFilterChain chain) {this(Arrays.asList(chain));}//又是SecurityFilterChain這家伙!嫌疑更大了!public FilterChainProxy(List<SecurityFilterChain> filterChains) {this.filterChainValidator = new FilterChainProxy.NullFilterChainValidator();this.firewall = new StrictHttpFirewall();this.filterChains = filterChains;}//注:直接從doFilter看public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {boolean clearContext = request.getAttribute(FILTER_APPLIED) == null;if (clearContext) {try {request.setAttribute(FILTER_APPLIED, Boolean.TRUE);this.doFilterInternal(request, response, chain);} finally {SecurityContextHolder.clearContext();request.removeAttribute(FILTER_APPLIED);}} else {//第一步:具體操作調用下面的doFilterInternal方法了this.doFilterInternal(request, response, chain);}}private void doFilterInternal(ServletRequest request, ServletResponse response, FilterChainchain) throws IOException, ServletException {FirewalledRequest fwRequest =this.firewall.getFirewalledRequest((HttpServletRequest)request);HttpServletResponse fwResponse =this.firewall.getFirewalledResponse((HttpServletResponse)response);//第二步:封裝要執行的過濾器鏈,那么多過濾器就在這里被封裝進去了!List<Filter> filters = this.getFilters((HttpServletRequest)fwRequest);if (filters != null && filters.size() != 0) {FilterChainProxy.VirtualFilterChain vfc = newFilterChainProxy.VirtualFilterChain(fwRequest, chain, filters);//第四步:加載過濾器鏈vfc.doFilter(fwRequest, fwResponse);} else {if (logger.isDebugEnabled()) {logger.debug(UrlUtils.buildRequestUrl(fwRequest) + (filters == null ? ' has nomatching filters' : ' has an empty filter list'));}fwRequest.reset();chain.doFilter(fwRequest, fwResponse);}}private List<Filter> getFilters(HttpServletRequest request) {Iterator var2 = this.filterChains.iterator();//第三步:封裝過濾器鏈到SecurityFilterChain中!SecurityFilterChain chain;do {if (!var2.hasNext()) {return null;}chain = (SecurityFilterChain)var2.next();} while(!chain.matches(request));return chain.getFilters();}}

Spring Security常用過濾器實例解析

SecurityFilterChain

最后看SecurityFilterChain,這是個接口,實現類也只有一個,這才是web.xml中配置的過濾器鏈對象!

public interface SecurityFilterChain { boolean matches(HttpServletRequest request); List<Filter> getFilters();}

public final class DefaultSecurityFilterChain implements SecurityFilterChain { private static final Log logger = LogFactory.getLog(DefaultSecurityFilterChain.class); private final RequestMatcher requestMatcher; private final List<Filter> filters; public DefaultSecurityFilterChain(RequestMatcher requestMatcher, Filter... filters) { this(requestMatcher, Arrays.asList(filters)); } public DefaultSecurityFilterChain(RequestMatcher requestMatcher, List<Filter> filters) { logger.info('Creating filter chain: ' + requestMatcher + ', ' + filters); this.requestMatcher = requestMatcher; this.filters = new ArrayList<>(filters); } public RequestMatcher getRequestMatcher() { return requestMatcher; } public List<Filter> getFilters() { return filters; } public boolean matches(HttpServletRequest request) { return requestMatcher.matches(request); } @Override public String toString() { return '[ ' + requestMatcher + ', ' + filters + ']'; }}

Spring Security常用過濾器實例解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产精品日本一区二区不卡视频 | 久久国产电影| 精品一级视频| 精品香蕉视频| 国内自拍视频一区二区三区| 久久精品女人| 91青青国产在线观看精品| 国产精品久久久久蜜臀| 亚洲人成在线网站| 日本韩国欧美超级黄在线观看| 午夜精品久久久久久久久久蜜桃| 久久中文字幕一区二区三区| 国产一区二区精品久| 中文字幕在线看片| 国产高清一区二区| 免费观看在线综合| 日韩精品午夜视频| 免费亚洲婷婷| 色偷偷偷在线视频播放| 999国产精品永久免费视频app| 亚洲国产成人精品女人| 首页亚洲欧美制服丝腿| 日韩激情视频网站| 久久69成人| 久久精品高清| 亚洲精品在线国产| 久久gogo国模啪啪裸体| 91精品xxx在线观看| 亚洲激情久久| 欧美一区网站| 日韩精品永久网址| 爽好多水快深点欧美视频| 欧美私人啪啪vps| 超碰在线99| 丝瓜av网站精品一区二区| 国产福利资源一区| 久久久国产亚洲精品| 视频一区欧美精品| 老色鬼精品视频在线观看播放| 日本久久成人网| 亚洲日韩中文字幕一区| 欧美精品国产| 激情丁香综合| 欧美日韩1区2区3区| 高清一区二区| 美女日韩在线中文字幕| 欧美国产日本| 9色精品在线| 老鸭窝一区二区久久精品| 亚洲二区三区不卡| 欧美韩一区二区| 亚洲欧洲另类| 国产黄色一区| 老鸭窝亚洲一区二区三区| 麻豆91精品91久久久的内涵| 婷婷精品进入| 国产日产精品一区二区三区四区的观看方式 | 欧美中文字幕| 精品淫伦v久久水蜜桃| 99国产精品| 久久精品伊人| 亚洲精品一级| 99精品国产一区二区三区| 日韩**一区毛片| 成人羞羞视频播放网站| 国产欧美三级| 亚洲欧美激情诱惑| 国产成人精品一区二区三区免费 | 亚洲影视一区| 色婷婷久久久| 国产精品黄色| 在线观看亚洲精品福利片| 在线天堂中文资源最新版| 中文字幕乱码亚洲无线精品一区| 中文字幕在线高清| 国产精品免费不| 免费看黄色91| re久久精品视频| 福利视频一区| 国产日韩高清一区二区三区在线| 亚洲综合精品| 在线天堂中文资源最新版| 国产欧美一区二区三区米奇| 亚洲在线观看| 久久久一二三| 国产在线一区不卡| 欧美亚洲一区二区三区| 久久午夜视频| av一区二区高清| 精品亚洲美女网站| 国产一区不卡| 麻豆传媒一区二区三区| 欧美天堂一区| 日本不卡视频一二三区| 视频在线观看一区二区三区| 欧美二区视频| 中文字幕人成乱码在线观看| 国产精品地址| 日韩精品亚洲专区| 一级欧美视频| 丝袜亚洲另类欧美| 在线亚洲一区| 狠狠干成人综合网| 久久一区二区三区电影| 黑森林国产精品av| 精品国产a一区二区三区v免费| 国产乱子精品一区二区在线观看| 视频一区欧美日韩| 鲁大师成人一区二区三区| 91精品观看| 欧美日韩一区二区综合| 成午夜精品一区二区三区软件| 国产精品传媒麻豆hd| 国产亚洲电影| 国产欧美日韩精品高清二区综合区 | 奶水喷射视频一区| 欧美91福利在线观看| 久久要要av| 久久久久久美女精品| 蜜桃av.网站在线观看| 亚洲一区资源| 久久久777| 99热精品久久| 午夜国产一区二区| 黄色亚洲大片免费在线观看| 欧美日韩精品一本二本三本| 亚洲精品午夜av福利久久蜜桃| 国产99亚洲| 伊人影院久久| 国产精品嫩草99av在线| 六月天综合网| 亚久久调教视频| 欧美一区自拍| 你懂的国产精品永久在线| 九九久久国产| 日韩精品永久网址| 不卡视频在线| 亚洲三级在线| 国产欧美日韩免费观看| 国产一区二区三区91| 在线观看精品| 亚洲免费网址| 久久激五月天综合精品| 久久久亚洲欧洲日产| 在线看片国产福利你懂的| 国产一区清纯| 免费久久99精品国产| 日韩欧美2区| 免费在线观看一区| 天堂√8在线中文| 婷婷综合激情| 青草综合视频| 麻豆mv在线观看| 免费日韩av片| 国产精品日韩精品中文字幕| 日韩理论视频| 亚洲一区网站| 国产精品视频一区视频二区| 亚洲黄色中文字幕| 免费看的黄色欧美网站| 国产精品成人3p一区二区三区| 国产日韩电影| 亚洲一区二区av| 精品视频在线观看网站| 日韩精品电影| 蜜桃免费网站一区二区三区| 国产日韩欧美一区二区三区| 日本蜜桃在线观看视频| 六月丁香综合| 狠狠久久伊人| 香蕉视频成人在线观看| 欧美国产另类| 蜜臀91精品国产高清在线观看| 美国三级日本三级久久99| 精品久久久亚洲| 国产精品毛片在线| 国产精品一区二区av日韩在线| 麻豆视频在线观看免费网站黄| 日韩视频一区| 久久av导航| 蜜臀久久精品| 亚洲精品欧美| 日韩成人亚洲| 亚洲麻豆一区| 亚洲日本网址| 日韩高清电影免费| 久久精品国产www456c0m| 国产日产一区| 亚洲综合不卡| 久久久久免费| 日韩精品一区二区三区中文在线| 国产一区三区在线播放| 久色成人在线| av资源中文在线| 日本视频在线一区| 不卡中文字幕| 97精品国产一区二区三区| 日本a级不卡| 在线亚洲激情| 日本精品不卡|