总:

用户通过浏览器发送URL地址,由FilterSecurityInterceptor判断是否具有相应的访问权限。
对于用户请求的方法权限,例如注解@PreAuthorize("hasRole('ADMIN')"),由MethodSecurityInterceptor判断
两个拦截器都继承了AbstractSecurityInterceptor

  1. /*
  2. * Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.security.web.access.intercept;
  17. import java.io.IOException;
  18. import javax.servlet.Filter;
  19. import javax.servlet.FilterChain;
  20. import javax.servlet.FilterConfig;
  21. import javax.servlet.ServletException;
  22. import javax.servlet.ServletRequest;
  23. import javax.servlet.ServletResponse;
  24. import org.springframework.security.access.SecurityMetadataSource;
  25. import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
  26. import org.springframework.security.access.intercept.InterceptorStatusToken;
  27. import org.springframework.security.web.FilterInvocation;
  28. /**
  29. * Performs security handling of HTTP resources via a filter implementation.
  30. * 通过筛选器实现对HTTP资源的安全处理。
  31. * <p>
  32. * The <code>SecurityMetadataSource</code> required by this security interceptor is of
  33. * type {@link FilterInvocationSecurityMetadataSource}.
  34. * <p>
  35. *安全拦截器所需的SecurityMetadataSource类型是FilterInvocationSecurityMetadataSource
  36. *
  37. * Refer to {@link AbstractSecurityInterceptor} for details on the workflow.
  38. * </p>
  39. *
  40. * @author Ben Alex
  41. * @author Rob Winch
  42. */
  43. public class FilterSecurityInterceptor extends AbstractSecurityInterceptor implements
  44. Filter {
  45. // ~ Static fields/initializers
  46. // =====================================================================================
  47. private static final String FILTER_APPLIED = "__spring_security_filterSecurityInterceptor_filterApplied";
  48. // ~ Instance fields
  49. // ================================================================================================
  50. /**
  51. *securityMetadataSource 中包含了一个HashMap,map中保存了用户请求的Http.Method和相应的URL地址
  52. *例如在Spring boot中,可能是如下的配置,参考图1
  53. *securityMetadataSource中的内容,参考图2
  54. */
  55. private FilterInvocationSecurityMetadataSource securityMetadataSource;
  56. private boolean observeOncePerRequest = true;
  57. // ~ Methods
  58. // ========================================================================================================
  59. /**
  60. * Not used (we rely on IoC container lifecycle services instead)
  61. *
  62. * @param arg0 ignored
  63. *
  64. * @throws ServletException never thrown
  65. */
  66. public void init(FilterConfig arg0) throws ServletException {
  67. }
  68. /**
  69. * Not used (we rely on IoC container lifecycle services instead)
  70. */
  71. public void destroy() {
  72. }
  73. /**
  74. * Method that is actually called by the filter chain. Simply delegates to the
  75. * {@link #invoke(FilterInvocation)} method.
  76. *
  77. * @param request the servlet request
  78. * @param response the servlet response
  79. * @param chain the filter chain
  80. *
  81. * @throws IOException if the filter chain fails
  82. * @throws ServletException if the filter chain fails
  83. *
  84. *
  85. *通过责任链式调用,执行doFilter方法
  86. *FilterInvocation中保存了filter相关的信息,比如request,response,chain
  87. *通过invoke方法处理具体的url过滤
  88. */
  89. public void doFilter(ServletRequest request, ServletResponse response,
  90. FilterChain chain) throws IOException, ServletException {
  91. FilterInvocation fi = new FilterInvocation(request, response, chain);
  92. invoke(fi);
  93. }
  94. public FilterInvocationSecurityMetadataSource getSecurityMetadataSource() {
  95. return this.securityMetadataSource;
  96. }
  97. public SecurityMetadataSource obtainSecurityMetadataSource() {
  98. return this.securityMetadataSource;
  99. }
  100. public void setSecurityMetadataSource(FilterInvocationSecurityMetadataSource newSource) {
  101. this.securityMetadataSource = newSource;
  102. }
  103. public Class<?> getSecureObjectClass() {
  104. return FilterInvocation.class;
  105. }
  106. public void invoke(FilterInvocation fi) throws IOException, ServletException {
  107. //获取当前http请求的地址,比如说“/login”
  108. if ((fi.getRequest() != null)
  109. && (fi.getRequest().getAttribute(FILTER_APPLIED) != null)
  110. && observeOncePerRequest) {
  111. // filter already applied to this request and user wants us to observe
  112. // once-per-request handling, so don't re-do security checking
  113. fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
  114. }
  115. else {
  116. // first time this request being called, so perform security checking
  117. if (fi.getRequest() != null) {
  118. fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE);
  119. }
  120. //这里做主要URL比对,将当前URL与securityMetadataSource(我们自己配置)中的URL过滤条件进行比对
  121. //首先判断当前URL是permit的还是需要验证的
  122. //若需要验证,尝试加载保存在SecurityContextHolder.getContext()中的已登录信息
  123. //调用AbstractSecurityInterceptor中的AccessDecisionManager对象的decide方法
  124. //如果对于配置中需要登录才可访问的URL,已经查找到登录信息,则执行下一个Filter
  125. InterceptorStatusToken token = super.beforeInvocation(fi);
  126. try {
  127. fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
  128. }
  129. finally {
  130. super.finallyInvocation(token);
  131. }
  132. super.afterInvocation(token, null);
  133. }
  134. }
  135. /**
  136. * Indicates whether once-per-request handling will be observed. By default this is
  137. * <code>true</code>, meaning the <code>FilterSecurityInterceptor</code> will only
  138. * execute once-per-request. Sometimes users may wish it to execute more than once per
  139. * request, such as when JSP forwards are being used and filter security is desired on
  140. * each included fragment of the HTTP request.
  141. *
  142. * @return <code>true</code> (the default) if once-per-request is honoured, otherwise
  143. * <code>false</code> if <code>FilterSecurityInterceptor</code> will enforce
  144. * authorizations for each and every fragment of the HTTP request.
  145. */
  146. public boolean isObserveOncePerRequest() {
  147. return observeOncePerRequest;
  148. }
  149. public void setObserveOncePerRequest(boolean observeOncePerRequest) {
  150. this.observeOncePerRequest = observeOncePerRequest;
  151. }
  152. }

Spring security用户URL权限之FilterSecurityInterceptor的更多相关文章

  1. Spring Security 动态url权限控制(三)

    一.前言 本篇文章将讲述Spring Security 动态分配url权限,未登录权限控制,登录过后根据登录用户角色授予访问url权限 基本环境 spring-boot 2.1.8 mybatis-p ...

  2. Spring Security实现RBAC权限管理

    Spring Security实现RBAC权限管理 一.简介 在企业应用中,认证和授权是非常重要的一部分内容,业界最出名的两个框架就是大名鼎鼎的 Shiro和Spring Security.由于Spr ...

  3. 登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架

    登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架

  4. Spring Security 基于URL的权限判断

    1.  FilterSecurityInterceptor 源码阅读 org.springframework.security.web.access.intercept.FilterSecurityI ...

  5. Spring security 用户,角色,权限,资源

    转自:http://blog.csdn.net/wybqq/article/details/52940194 关于Spring security对用户请求的处理过程 体现在这两个过程的体现. 关于用户 ...

  6. spring security 登录、权限管理配置

    登录流程 1)容器启动(MySecurityMetadataSource:loadResourceDefine加载系统资源与权限列表)  2)用户发出请求  3)过滤器拦截(MySecurityFil ...

  7. 别再让你的微服务裸奔了,基于 Spring Session & Spring Security 微服务权限控制

    微服务架构 网关:路由用户请求到指定服务,转发前端 Cookie 中包含的 Session 信息: 用户服务:用户登录认证(Authentication),用户授权(Authority),用户管理(R ...

  8. Spring Security 自定义 登陆 权限验证

    转载于:https://www.jianshu.com/p/6b8fb59b614b 项目简介 基于Spring Cloud 的项目,Spring Cloud是在Spring Boot上搭建的所以按照 ...

  9. 获取spring security用户相关信息

    在JSP中获得 使用spring security的标签库 在页面中引入标签 <%@ taglib prefix="sec" uri="http://www.spr ...

随机推荐

  1. unity如何查找某个脚本挂在了哪些物体上

    在开发中往往会遇到一个问题:不知道整个场景中究竟有哪些物体挂载了某一个脚本.如果挨个查找太麻烦了,下面有一种方法可以快速找到解决这个问题. 在unity的Window里有一项Editor tests ...

  2. sqlserver 一些基本的语法

    1. STR()函数    这是将数字转换为字符串的快捷函数,这个函数有3个参数:数值,总长度,和小数位   如: select str(123.4,8,4) = 123.4000  如长度不够,则左 ...

  3. Vue项目根据不同运行环境打包项目

    前提 项目是直接通过 vue-cli脚手架 生成的: 假设在项目开发中,分为三个环境 -- · 测试环境· 预生产环境· 生产环境 每个环境的接口地址都是不同的,而 vue-cli 给出的环境只有 d ...

  4. oracle国家字符集

    国家字符集是一个备用字符集,利用此字符集可以在没有Unicode 数据库字符集的数据库中存储 Unicode 字符. 选择国家字符集的其它原因如下: 对于频繁的字符处理操作,不同的字符编码方案可能更为 ...

  5. 几种常见的flex布局

    1,水平等距排列.俩端对齐.垂直方向居顶对齐 html: <div class="container flex"> <div class="div1&q ...

  6. Adapter小练习

    Aapter的继承关系图: Android中Adapter的是数据和视图之间的桥梁,数据在adapter中做处理,然后显示到视图上面. 一.ArrayAdapter适配器 java代码: import ...

  7. asp.net ajax客户端框架如何调用Web Service

    asp.net ajax客户端框架如何调用Web Service 1:Web Service类添加 [System.Web.Script.Services.ScriptService]特性2:需要异步 ...

  8. hdu 2844 混合背包【背包dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意:有n种纸币面额(a1,a2,...an),每种面额对应有(c1,c2,...cn)张.问这些钱能拼成 ...

  9. 服务网关zuul----zuul中的动态刷新路由配置

    Spring Cloud实战小贴士:Zuul处理Cookie和重定向 所以解决该问题的思路也很简单,我们只需要通过设置sensitiveHeaders即可,设置方法分为两种: 全局设置: zuul.s ...

  10. [Linux]环境配置之jdk的安装 标签: jdk服务器linux 2016-08-07 22:18 502人阅读 评论(21)

    这两天服务器崩了,所以需要重新配置环境,然后从头到尾配置了一遍,现在记录总结一下自己这两天的工作,首先是jdk的配置! 很多软件,需要jdk为基础,所以第一个装的就是jdk. 第一步,拷贝文件 首先将 ...