Spring security用户URL权限之FilterSecurityInterceptor
总:
/** 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 implementsFilter {// ~ 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 checkingfi.getChain().doFilter(fi.getRequest(), fi.getResponse());}else {// first time this request being called, so perform security checkingif (fi.getRequest() != null) {fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE);}//这里做主要URL比对,将当前URL与securityMetadataSource(我们自己配置)中的URL过滤条件进行比对//首先判断当前URL是permit的还是需要验证的//若需要验证,尝试加载保存在SecurityContextHolder.getContext()中的已登录信息//调用AbstractSecurityInterceptor中的AccessDecisionManager对象的decide方法//如果对于配置中需要登录才可访问的URL,已经查找到登录信息,则执行下一个FilterInterceptorStatusToken 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;}}
Spring security用户URL权限之FilterSecurityInterceptor的更多相关文章
- Spring Security 动态url权限控制(三)
一.前言 本篇文章将讲述Spring Security 动态分配url权限,未登录权限控制,登录过后根据登录用户角色授予访问url权限 基本环境 spring-boot 2.1.8 mybatis-p ...
- Spring Security实现RBAC权限管理
Spring Security实现RBAC权限管理 一.简介 在企业应用中,认证和授权是非常重要的一部分内容,业界最出名的两个框架就是大名鼎鼎的 Shiro和Spring Security.由于Spr ...
- 登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架
登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架
- Spring Security 基于URL的权限判断
1. FilterSecurityInterceptor 源码阅读 org.springframework.security.web.access.intercept.FilterSecurityI ...
- Spring security 用户,角色,权限,资源
转自:http://blog.csdn.net/wybqq/article/details/52940194 关于Spring security对用户请求的处理过程 体现在这两个过程的体现. 关于用户 ...
- spring security 登录、权限管理配置
登录流程 1)容器启动(MySecurityMetadataSource:loadResourceDefine加载系统资源与权限列表) 2)用户发出请求 3)过滤器拦截(MySecurityFil ...
- 别再让你的微服务裸奔了,基于 Spring Session & Spring Security 微服务权限控制
微服务架构 网关:路由用户请求到指定服务,转发前端 Cookie 中包含的 Session 信息: 用户服务:用户登录认证(Authentication),用户授权(Authority),用户管理(R ...
- Spring Security 自定义 登陆 权限验证
转载于:https://www.jianshu.com/p/6b8fb59b614b 项目简介 基于Spring Cloud 的项目,Spring Cloud是在Spring Boot上搭建的所以按照 ...
- 获取spring security用户相关信息
在JSP中获得 使用spring security的标签库 在页面中引入标签 <%@ taglib prefix="sec" uri="http://www.spr ...
随机推荐
- Python多线程在爬虫中的应用
题记:作为测试工程师经常需要解决测试数据来源的问题,解决思路无非是三种:(1)直接从生产环境拷贝真实数据 (2)从互联网上爬取数据 (3)自己用脚本或者工具造数据.前段时间,为了获取更多的测试数据,笔 ...
- 从Docker容器内部,如何连接到本机的本地主机?
原文 从Docker容器内部,如何连接到本机的本地主机? 编辑:如果您使用的是Docker-for-mac或Docker-for-Windows 18.03+,只需使用主机连接到您的mysql服务即可 ...
- [Vue CLI 3] public 目录没用吗
在 vue-cli 3 初始化的项目根目录下面:和 src 同级有一个 public 目录 官网的说明如下:https://cli.vuejs.org/zh/guid... 在下列情况下使用: 你需要 ...
- ConcurrentModificationException解决办法
package test.my.chap0302; import java.util.ArrayList; import java.util.Iterator; import java.util.Li ...
- C++ int与string的互转
int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀,告诉编译器按照不同进制去解释.8进制 ...
- 在Eclipse中添加Tomcat
在Eclipse中开发web或开启web服务需要Tomcat的支持,在添加Tomcat之前要清楚你的Eclipse版本,如果你的Eclipse是javvEE版的就可以直接安装Tomcat,如果不是就需 ...
- Xcode10 import导入文件的坑
更新了10.0的Xcode,踩了两个坑,记录一下. #import "" 双引号内输入任何字符 都会导致Xcode崩溃 解决方案: target - buildSettings - ...
- 直击 KubeCon 2019 现场,阿里云 Hands-on Workshop 亮点回顾
2019 年 6 月 24 日,KubeCon + CloudNativeCon 第二次在中国举办.此次大会阿里共有 26 个技术演讲入选,并有两场沙龙活动,阿里云专家也与技术极客们也再次相聚.Kub ...
- ta-lib 里的蜡烛图形态函数源码
ta-lib 里的蜡烛图形态函数源码 以CDL2CROWS为例, 看一看c语言的源码: 有关的源码文件包括 d:\Documents\Pictures\ta-lib\c\src\ta_func\ta_ ...
- 关于编码的发展演变:ASCII、GB2312、GBK、gb18030、Unicode、UTF-8
[1]ASCII 每个字符占据1bytes(字节),第一次以规范标准发表是在1967年,最后一次修订是在1986年.用二进制表示的话最高位必须为0(扩展的ASCII不在考虑范围内),因此ASCII只能 ...