欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring security用戶URL權(quán)限FilterSecurityInterceptor使用解析

 更新時(shí)間:2019年12月12日 09:03:43   作者:二刀  
這篇文章主要介紹了Spring security用戶URL權(quán)限FilterSecurityInterceptor使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Spring security用戶URL權(quán)限FilterSecurityInterceptor使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

用戶通過(guò)瀏覽器發(fā)送URL地址,由FilterSecurityInterceptor判斷是否具有相應(yīng)的訪問(wèn)權(quán)限。

對(duì)于用戶請(qǐng)求的方法權(quán)限,例如注解@PreAuthorize("hasRole('ADMIN')"),由MethodSecurityInterceptor判斷

兩個(gè)攔截器都繼承了AbstractSecurityInterceptor

代碼如下

/*
 * Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 *
 * 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.web.access.intercept;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
import org.springframework.security.access.intercept.InterceptorStatusToken;
import org.springframework.security.web.FilterInvocation;
/**
 * Performs security handling of HTTP resources via a filter implementation.
 * 通過(guò)篩選器實(shí)現(xiàn)對(duì)HTTP資源的安全處理。
 * <p>
 * The <code>SecurityMetadataSource</code> required by this security interceptor is of
 * type {@link FilterInvocationSecurityMetadataSource}.
 * <p>
 *安全攔截器所需的SecurityMetadataSource類型是FilterInvocationSecurityMetadataSource
 *
 * Refer to {@link AbstractSecurityInterceptor} for details on the workflow.
 * </p>
 *
 * @author Ben Alex
 * @author Rob Winch
 */
public class FilterSecurityInterceptor extends AbstractSecurityInterceptor implements
		Filter {
	// ~ Static fields/initializers
	// =====================================================================================
	private static final String FILTER_APPLIED = "__spring_security_filterSecurityInterceptor_filterApplied";
	// ~ Instance fields
	// ================================================================================================
	/**
	*securityMetadataSource 中包含了一個(gè)HashMap,map中保存了用戶請(qǐng)求的Http.Method和相應(yīng)的URL地址
	*例如在Spring boot中,可能是如下的配置,參考圖1
	*securityMetadataSource中的內(nèi)容,參考圖2
	*/
	private FilterInvocationSecurityMetadataSource securityMetadataSource;
	private Boolean observeOncePerRequest = true;
	// ~ Methods
	// ========================================================================================================
	/**
	 * Not used (we rely on IoC container lifecycle services instead)
	 *
	 * @param arg0 ignored
	 *
	 * @throws ServletException never thrown
	 */
	public void init(FilterConfig arg0) throws ServletException {
	}
	/**
	 * Not used (we rely on IoC container lifecycle services instead)
	 */
	public void destroy() {
	}
	/**
	 * Method that is actually called by the filter chain. Simply delegates to the
	 * {@link #invoke(FilterInvocation)} method.
	 *
	 * @param request the servlet request
	 * @param response the servlet response
	 * @param chain the filter chain
	 *
	 * @throws IOException if the filter chain fails
	 * @throws ServletException if the filter chain fails
	 *
	 *
	 *通過(guò)責(zé)任鏈?zhǔn)秸{(diào)用,執(zhí)行doFilter方法
	 *FilterInvocation中保存了filter相關(guān)的信息,比如request,response,chain
	 *通過(guò)invoke方法處理具體的url過(guò)濾
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
				FilterChain chain) throws IOException, ServletException {
		FilterInvocation fi = new FilterInvocation(request, response, chain);
		invoke(fi);
	}
	public FilterInvocationSecurityMetadataSource getSecurityMetadataSource() {
		return this.securityMetadataSource;
	}
	public SecurityMetadataSource obtainSecurityMetadataSource() {
		return this.securityMetadataSource;
	}
	public void setSecurityMetadataSource(FilterInvocationSecurityMetadataSource newSource) {
		this.securityMetadataSource = newSource;
	}
	public Class<?> getSecureObjectClass() {
		return FilterInvocation.class;
	}
	public void invoke(FilterInvocation fi) throws IOException, ServletException {
		//獲取當(dāng)前http請(qǐng)求的地址,比如說(shuō)“/login”
		if ((fi.getRequest() != null)
						&& (fi.getRequest().getAttribute(FILTER_APPLIED) != null)
						&& observeOncePerRequest) {
			// filter already applied to this request and user wants us to observe
			// once-per-request handling, so don't re-do security checking
			fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
		} else {
			// first time this request being called, so perform security checking
			if (fi.getRequest() != null) {
				fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE);
			}
			//這里做主要URL比對(duì),將當(dāng)前URL與securityMetadataSource(我們自己配置)中的URL過(guò)濾條件進(jìn)行比對(duì)
			//首先判斷當(dāng)前URL是permit的還是需要驗(yàn)證的
			//若需要驗(yàn)證,嘗試加載保存在SecurityContextHolder.getContext()中的已登錄信息
			//調(diào)用AbstractSecurityInterceptor中的AccessDecisionManager對(duì)象的decide方法
			//如果對(duì)于配置中需要登錄才可訪問(wèn)的URL,已經(jīng)查找到登錄信息,則執(zhí)行下一個(gè)Filter
			InterceptorStatusToken token = super.beforeInvocation(fi);
			try {
				fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
			}
			finally {
				super.finallyInvocation(token);
			}
			super.afterInvocation(token, null);
		}
	}
	/**
	 * Indicates whether once-per-request handling will be observed. By default this is
	 * <code>true</code>, meaning the <code>FilterSecurityInterceptor</code> will only
	 * execute once-per-request. Sometimes users may wish it to execute more than once per
	 * request, such as when JSP forwards are being used and filter security is desired on
	 * each included fragment of the HTTP request.
	 *
	 * @return <code>true</code> (the default) if once-per-request is honoured, otherwise
	 * <code>false</code> if <code>FilterSecurityInterceptor</code> will enforce
	 * authorizations for each and every fragment of the HTTP request.
	 */
	public Boolean isObserveOncePerRequest() {
		return observeOncePerRequest;
	}
	public void setObserveOncePerRequest(Boolean observeOncePerRequest) {
		this.observeOncePerRequest = observeOncePerRequest;
	}
}

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

相關(guān)文章

  • Java?Guava的使用技巧整理

    Java?Guava的使用技巧整理

    Guava是Google發(fā)布的一個(gè)開源庫(kù),主要提供了一些在Java開發(fā)中非常有用的工具類和API,不管是工作還是學(xué)習(xí)都是非常值得我們?nèi)ナ煜さ?,一起?lái)看看吧
    2023-03-03
  • Java多維數(shù)組詳解

    Java多維數(shù)組詳解

    大家好,本篇文章主要講的是Java多維數(shù)組詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Java中Servlet的生命周期詳解

    Java中Servlet的生命周期詳解

    這篇文章主要介紹了Java中Servlet的生命周期詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • 微服務(wù)之間如何通過(guò)feign調(diào)用接口上傳文件

    微服務(wù)之間如何通過(guò)feign調(diào)用接口上傳文件

    這篇文章主要介紹了微服務(wù)之間如何通過(guò)feign調(diào)用接口上傳文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java基數(shù)排序radix sort原理及用法解析

    Java基數(shù)排序radix sort原理及用法解析

    這篇文章主要介紹了Java基數(shù)排序radix sort原理及用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java面向?qū)ο蠡A(chǔ)知識(shí)之封裝,繼承,多態(tài)和抽象

    Java面向?qū)ο蠡A(chǔ)知識(shí)之封裝,繼承,多態(tài)和抽象

    這篇文章主要介紹了Java面向?qū)ο蟮姆庋b,繼承,多態(tài)和抽象,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-11-11
  • 詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案

    詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案

    本篇文章主要介紹了詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • spring boot整合Shiro實(shí)現(xiàn)單點(diǎn)登錄的示例代碼

    spring boot整合Shiro實(shí)現(xiàn)單點(diǎn)登錄的示例代碼

    本篇文章主要介紹了spring boot整合Shiro實(shí)現(xiàn)單點(diǎn)登錄的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Java匿名類和匿名函數(shù)的概念和寫法

    Java匿名類和匿名函數(shù)的概念和寫法

    匿名函數(shù)寫法和匿名類寫法的前提必須基于函數(shù)式接口匿名函數(shù)寫法和匿名類寫法其本質(zhì)是同一個(gè)東西,只是簡(jiǎn)化寫法不同使用Lambda表達(dá)式簡(jiǎn)寫匿名函數(shù)時(shí),可以同時(shí)省略實(shí)現(xiàn)類名、函數(shù)名,這篇文章主要介紹了Java匿名類和匿名函數(shù)的概念和寫法,需要的朋友可以參考下
    2023-06-06
  • Java后端之俯瞰數(shù)據(jù)接收的三種方式

    Java后端之俯瞰數(shù)據(jù)接收的三種方式

    在前后端分離的開發(fā)項(xiàng)目中,前后端聯(lián)調(diào)的時(shí)候會(huì)出現(xiàn)這樣那樣的問(wèn)題,尤其是在調(diào)取數(shù)據(jù)的程序上面,有時(shí)候前端給的前端給到后端的明明是正確的但就是無(wú)法拿到正確的數(shù)據(jù),下面小千就來(lái)給大家詳解一下常見的三種數(shù)據(jù)傳輸方式
    2021-10-10

最新評(píng)論