最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Security常用過(guò)濾器實(shí)例解析

 更新時(shí)間:2020年03月04日 08:51:57   作者:天宇軒-王  
這篇文章主要介紹了Spring Security常用過(guò)濾器實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Spring Security常見(jiàn)的15個(gè)攔截器

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

首當(dāng)其沖的一個(gè)過(guò)濾器,作用之重要,自不必多言。

  • SecurityContextPersistenceFilter主要是使用SecurityContextRepository在session中保存或更新一個(gè)
  • SecurityContext,并將SecurityContext給以后的過(guò)濾器使用,來(lái)為后續(xù)filter建立所需的上下文。
  • SecurityContext中存儲(chǔ)了當(dāng)前用戶(hù)的認(rèn)證以及權(quán)限信息。

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

此過(guò)濾器用于集成SecurityContext到Spring異步執(zhí)行機(jī)制中的WebAsyncManager

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

向請(qǐng)求的Header中添加相應(yīng)的信息,可在http標(biāo)簽內(nèi)部使用security:headers來(lái)控制

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

csrf又稱(chēng)跨域請(qǐng)求偽造,SpringSecurity會(huì)對(duì)所有post請(qǐng)求驗(yàn)證是否包含系統(tǒng)生成的csrf的token信息,

如果不包含,則報(bào)錯(cuò)。起到防止csrf攻擊的效果。

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

匹配 URL為/logout的請(qǐng)求,實(shí)現(xiàn)用戶(hù)退出,清除認(rèn)證信息。

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

認(rèn)證操作全靠這個(gè)過(guò)濾器,默認(rèn)匹配URL為/login且必須為POST請(qǐng)求。

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

如果沒(méi)有在配置文件中指定認(rèn)證頁(yè)面,則由該過(guò)濾器生成一個(gè)默認(rèn)認(rèn)證頁(yè)面。

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

由此過(guò)濾器可以生產(chǎn)一個(gè)默認(rèn)的退出登錄頁(yè)面

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

此過(guò)濾器會(huì)自動(dòng)解析HTTP請(qǐng)求中頭部名字為Authentication,且以Basic開(kāi)頭的頭信息。

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

通過(guò)HttpSessionRequestCache內(nèi)部維護(hù)了一個(gè)RequestCache,用于緩存HttpServletRequest

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

針對(duì)ServletRequest進(jìn)行了一次包裝,使得request具有更加豐富的API

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

當(dāng)SecurityContextHolder中認(rèn)證信息為空,則會(huì)創(chuàng)建一個(gè)匿名用戶(hù)存入到SecurityContextHolder中。

spring security為了兼容未登錄的訪(fǎng)問(wèn),也走了一套認(rèn)證流程,只不過(guò)是一個(gè)匿名的身份。

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

SecurityContextRepository限制同一用戶(hù)開(kāi)啟多個(gè)會(huì)話(huà)的數(shù)量

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

異常轉(zhuǎn)換過(guò)濾器位于整個(gè)springSecurityFilterChain的后方,用來(lái)轉(zhuǎn)換整個(gè)鏈路中出現(xiàn)的異常

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

獲取所配置資源訪(fǎng)問(wèn)的授權(quán)信息,根據(jù)SecurityContextHolder中存儲(chǔ)的用戶(hù)信息來(lái)決定其是否有權(quán)限。

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

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

spring security 過(guò)濾器鏈加載原理

public class DelegatingFilterProxy extends GenericFilterBean {
@Nullable
private String contextAttribute;
@Nullable
private WebApplicationContext webApplicationContext;
@Nullable
private String targetBeanName;
private boolean targetFilterLifecycle;
@Nullable
private volatile Filter delegate;//注:這個(gè)過(guò)濾器才是真正加載的過(guò)濾器
private final Object delegateMonitor;
//注:doFilter才是過(guò)濾器的入口,直接從這看!
public void doFilter(ServletRequest request, ServletResponse response, FilterChain
filterChain) 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: no
ContextLoaderListener or DispatcherServlet registered?");
}
//第一步:doFilter中最重要的一步,初始化上面私有過(guò)濾器屬性delegate
delegateToUse = this.initDelegate(wac);
}
this.delegate = delegateToUse;
}
}
//第三步:執(zhí)行FilterChainProxy過(guò)濾器
this.invokeDelegate(delegateToUse, request, response, filterChain);
}
//第二步:直接看最終加載的過(guò)濾器到底是誰(shuí)
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
//debug得知targetBeanName為:springSecurityFilterChain
String targetBeanName = this.getTargetBeanName();
Assert.state(targetBeanName != null, "No target bean name set");
//debug得知delegate對(duì)象為:FilterChainProxy
Filter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);
if (this.isTargetFilterLifecycle()) {
delegate.init(this.getFilterConfig());
}
return delegate;
}
protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse
response, FilterChain filterChain) throws ServletException, IOException {
delegate.doFilter(request, response, filterChain);
}
}

第二步debug結(jié)果如下:

由此可知, DelegatingFilterProxy通過(guò)springSecurityFilterChain這個(gè)名稱(chēng),得到了一個(gè)FilterChainProxy過(guò)濾器,
最終在第三步執(zhí)行了這個(gè)過(guò)濾器。

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;
//咿!?可以通過(guò)一個(gè)叫SecurityFilterChain的對(duì)象實(shí)例化出一個(gè)FilterChainProxy對(duì)象
//這FilterChainProxy又是何方神圣?會(huì)不會(huì)是真正的過(guò)濾器鏈對(duì)象呢?先留著這個(gè)疑問(wèn)!
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 {
//第一步:具體操作調(diào)用下面的doFilterInternal方法了
this.doFilterInternal(request, response, chain);
}
}
private void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException, ServletException {
FirewalledRequest fwRequest =
this.firewall.getFirewalledRequest((HttpServletRequest)request);
HttpServletResponse fwResponse =
this.firewall.getFirewalledResponse((HttpServletResponse)response);
//第二步:封裝要執(zhí)行的過(guò)濾器鏈,那么多過(guò)濾器就在這里被封裝進(jìn)去了!
List<Filter> filters = this.getFilters((HttpServletRequest)fwRequest);
if (filters != null && filters.size() != 0) {
FilterChainProxy.VirtualFilterChain vfc = new
FilterChainProxy.VirtualFilterChain(fwRequest, chain, filters);
//第四步:加載過(guò)濾器鏈
vfc.doFilter(fwRequest, fwResponse);
} else {
if (logger.isDebugEnabled()) {
logger.debug(UrlUtils.buildRequestUrl(fwRequest) + (filters == null ? " has no
matching filters" : " has an empty filter list"));
}
fwRequest.reset();
chain.doFilter(fwRequest, fwResponse);
}
}
private List<Filter> getFilters(HttpServletRequest request) {
Iterator var2 = this.filterChains.iterator();
//第三步:封裝過(guò)濾器鏈到SecurityFilterChain中!
SecurityFilterChain chain;
do {
if (!var2.hasNext()) {
return null;
}
chain = (SecurityFilterChain)var2.next();
} while(!chain.matches(request));
return chain.getFilters();
}
}

SecurityFilterChain

最后看SecurityFilterChain,這是個(gè)接口,實(shí)現(xiàn)類(lèi)也只有一個(gè),這才是web.xml中配置的過(guò)濾器鏈對(duì)象!

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 + "]";
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Mapper層繼承BaseMapper<T>需要引入的pom依賴(lài)方式

    Mapper層繼承BaseMapper<T>需要引入的pom依賴(lài)方式

    這篇文章主要介紹了Mapper層繼承BaseMapper<T>需要引入的pom依賴(lài)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java中的Optional處理方法

    Java中的Optional處理方法

    在我們?nèi)粘5拈_(kāi)發(fā)中,我們經(jīng)常會(huì)遇到?NullPointerException,如何才能優(yōu)雅的處理NPE?這里告訴大家一個(gè)較為流行的方法,這篇文章主要介紹了Java中的Optional處理方法,需要的朋友可以參考下
    2022-09-09
  • Springboot使用SPI注冊(cè)bean到spring容器的示例代碼

    Springboot使用SPI注冊(cè)bean到spring容器的示例代碼

    這篇文章主要介紹了Springboot使用SPI注冊(cè)bean到spring容器,主要包括mydriver接口,mysqldriver實(shí)現(xiàn)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • Python安裝Jupyter Notebook配置使用教程詳解

    Python安裝Jupyter Notebook配置使用教程詳解

    這篇文章主要介紹了Python安裝Jupyter Notebook配置使用教程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java中l(wèi)ambda表達(dá)式實(shí)現(xiàn)aop切面功能

    Java中l(wèi)ambda表達(dá)式實(shí)現(xiàn)aop切面功能

    本文主要介紹了Java中l(wèi)ambda表達(dá)式實(shí)現(xiàn)aop切面功能,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 關(guān)于spring?aop兩種代理混用的問(wèn)題

    關(guān)于spring?aop兩種代理混用的問(wèn)題

    這篇文章主要介紹了關(guān)于spring?aop兩種代理混用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • springboot pojo對(duì)象日期屬性的問(wèn)題

    springboot pojo對(duì)象日期屬性的問(wèn)題

    這篇文章主要介紹了springboot pojo對(duì)象日期屬性的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java線(xiàn)程阻塞和喚醒的幾種方式詳解

    Java線(xiàn)程阻塞和喚醒的幾種方式詳解

    這篇文章主要介紹了Java線(xiàn)程阻塞和喚醒的幾種方式詳解,線(xiàn)程阻塞是指當(dāng)一個(gè)線(xiàn)程無(wú)法繼續(xù)執(zhí)行時(shí),它會(huì)進(jìn)入阻塞狀態(tài),直到某個(gè)條件滿(mǎn)足后才能繼續(xù)執(zhí)行,線(xiàn)程阻塞可以通過(guò)多種方式實(shí)現(xiàn),如等待鎖、等待IO操作、等待其他線(xiàn)程的完成等,需要的朋友可以參考下
    2023-10-10
  • 如何讓W(xué)in10實(shí)現(xiàn)Java文件的開(kāi)機(jī)自啟動(dòng)

    如何讓W(xué)in10實(shí)現(xiàn)Java文件的開(kāi)機(jī)自啟動(dòng)

    這篇文章主要介紹了如何讓W(xué)in10實(shí)現(xiàn)Java文件的開(kāi)機(jī)自啟動(dòng),對(duì)于一些想要一直運(yùn)行的Java文件,就會(huì)造成每次系統(tǒng)更新之后的重啟導(dǎo)致Java文件無(wú)法繼續(xù)運(yùn)行。,需要的朋友可以參考下
    2019-06-06
  • 20個(gè)非常實(shí)用的Java程序代碼片段

    20個(gè)非常實(shí)用的Java程序代碼片段

    這篇文章主要為大家分享了20個(gè)非常實(shí)用的Java程序片段,對(duì)java開(kāi)發(fā)項(xiàng)目有所幫助,感興趣的小伙伴們可以參考一下
    2016-02-02

最新評(píng)論

白沙| 彭水| 阿荣旗| 灵丘县| 炉霍县| 广平县| 安庆市| 顺昌县| 铁力市| 浦江县| 大石桥市| 洪江市| 兴山县| 即墨市| 壶关县| 呼图壁县| 七台河市| 平昌县| 鄂尔多斯市| 丘北县| 南澳县| 阿尔山市| 同心县| 庆云县| 册亨县| 扬州市| 云南省| 阿拉善盟| 理塘县| 贡嘎县| 米脂县| 新泰市| 合江县| 上林县| 慈利县| 阿拉尔市| 碌曲县| 永修县| 呈贡县| 芒康县| 德阳市|