Spring security用户URL权限FilterSecurityInterceptor使用解析

 更新时间:2019年12月12日 09:03:43   作者:二刀  
这篇文章主要介绍了Spring security用户URL权限FilterSecurityInterceptor使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了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;
	}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java通过正则表达式捕获组中的文本

    Java通过正则表达式捕获组中的文本

    这篇文章主要给大家介绍了关于利用Java如何通过正则表达式捕获组中文本的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧下
    2019-09-09
  • VSCode 配置 Spring Boot 项目开发环境的全过程

    VSCode 配置 Spring Boot 项目开发环境的全过程

    两三年前曾经试过配置Java环境, 存在不少问题作罢. 最近搜了下相关的文章, 感觉VSCode对Java项目的支持比三年前完善了不少. 今天实际配置了一下环境, 把自己常用的功能过了一遍, 基本能跑通开发流程, 做个笔记,需要的朋友可以参考下
    2024-03-03
  • MyBatis复杂Sql查询实现示例介绍

    MyBatis复杂Sql查询实现示例介绍

    在利用mybatis做查询的时候,一般返回结果用resulttype,这种情况必须是查询的结果在对应 的pojo类中有对应的,一般都是单表查询,但是对于一些复杂的情况,比如需要用到多表查询的时候,resultType不再适用,此时一般用resultMap来表示返回的结果
    2022-12-12
  • Mybatis配置之<environments>配置元素详解

    Mybatis配置之<environments>配置元素详解

    这篇文章主要介绍了Mybatis配置之<environments>配置元素,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Struts2 文件上传进度条的实现实例代码

    Struts2 文件上传进度条的实现实例代码

    本篇文章主要介绍了Struts2 文件上传进度条的实现实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 浅析mybatis和spring整合的实现过程

    浅析mybatis和spring整合的实现过程

    据官方的说法,在Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持。因此由Mybatis社区自己开发了一个Mybatis-Spring用来满足Mybatis用户整合Spring的需求,下面通过Mybatis-Spring来整合Mybatis跟Spring的用法做介绍
    2015-10-10
  • 详解Spring Boot 异步执行方法

    详解Spring Boot 异步执行方法

    这篇文章主要介绍了Spring Boot 异步执行方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • SpringMVC+EasyUI实现页面左侧导航菜单功能

    SpringMVC+EasyUI实现页面左侧导航菜单功能

    这篇文章主要介绍了SpringMVC+EasyUI实现页面左侧导航菜单功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • 怎样通过反射获取非静态内部类实例

    怎样通过反射获取非静态内部类实例

    这篇文章主要介绍了怎样通过反射获取非静态内部类实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • SpringBoot使用 druid 连接池来优化分页语句

    SpringBoot使用 druid 连接池来优化分页语句

    这篇文章主要介绍了SpringBoot使用 druid 连接池来优化分页语句,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论