91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring security用戶URL權限FilterSecurityInterceptor使用解析

發布時間:2020-10-11 00:01:03 來源:腳本之家 閱讀:454 作者:二刀 欄目:編程語言

這篇文章主要介紹了Spring security用戶URL權限FilterSecurityInterceptor使用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

用戶通過瀏覽器發送URL地址,由FilterSecurityInterceptor判斷是否具有相應的訪問權限。

對于用戶請求的方法權限,例如注解@PreAuthorize("hasRole('ADMIN')"),由MethodSecurityInterceptor判斷

兩個攔截器都繼承了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.
 * 通過篩選器實現對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 中包含了一個HashMap,map中保存了用戶請求的Http.Method和相應的URL地址
	*例如在Spring boot中,可能是如下的配置,參考圖1
	*securityMetadataSource中的內容,參考圖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
	 *
	 *
	 *通過責任鏈式調用,執行doFilter方法
	 *FilterInvocation中保存了filter相關的信息,比如request,response,chain
	 *通過invoke方法處理具體的url過濾
	 */
	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 {
		//獲取當前http請求的地址,比如說“/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比對,將當前URL與securityMetadataSource(我們自己配置)中的URL過濾條件進行比對
			//首先判斷當前URL是permit的還是需要驗證的
			//若需要驗證,嘗試加載保存在SecurityContextHolder.getContext()中的已登錄信息
			//調用AbstractSecurityInterceptor中的AccessDecisionManager對象的decide方法
			//如果對于配置中需要登錄才可訪問的URL,已經查找到登錄信息,則執行下一個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;
	}
}

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

吐鲁番市| 泾川县| 金堂县| 扎鲁特旗| 玉溪市| 温泉县| 黑水县| 中山市| 南华县| 景洪市| 博爱县| 淮滨县| 来安县| 天台县| 香港| 平泉县| 墨江| 应城市| 阿拉善左旗| 肇庆市| 高安市| 榆中县| 金沙县| 那曲县| 沾益县| 白河县| 平利县| 乡城县| 上饶市| 阿城市| 泸西县| 浏阳市| 伊通| 新化县| 菏泽市| 奉化市| 兴海县| 景德镇市| 唐山市| 苍溪县| 奉贤区|