SpringSecurity3的核心类有三种

1.URL过滤器或方法拦截器:用来拦截URL或者方法资源对其进行验证,其抽象基类为AbstractSecurityInterceptor 
2.资源权限获取器:用来取得访问某个URL或者方法所需要的权限,接口为SecurityMetadataSource 
3.访问决策器:用来决定用户是否拥有访问权限的关键类,其接口为AccessDecisionManager。

调用顺序为:AbstractSecurityInterceptor调用SecurityMetadataSource取得资源的所有可访问权限,然后再调用AccessDecisionManager来实现决策,确定用户是否有权限访问该资源。

SecurityMetadataSource包括MethodSecurityMetadataSource和FilterInvocationSecurityMetadataSource,分别对应方法和URL资源。

你也可以完全自定义自己的过滤器、资源权限获取器、访问决策器,下面给出完整的springsecurity3的配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/security"
  3. xmlns:beans="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/security
  8. http://www.springframework.org/schema/security/spring-security-3.0.xsd">
  9. <beans:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener" />
  10. <http auto-config="true" access-denied-page="/403.jsp">
  11. <intercept-url pattern="/css/**" filters="none" />
  12. <intercept-url pattern="/images/**" filters="none" />
  13. <intercept-url pattern="/js/**" filters="none" />
  14. <intercept-url pattern="/403.jsp" filters="none" />
  15. <intercept-url pattern="/" filters="none" />
  16. <intercept-url pattern="/login.jsp" filters="none" />
  17. <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" default-target-url="/finance/index.do?listId=CONSUMPTION&page.rowCount=10" />
  18. <logout logout-success-url="/login.jsp"/>
  19. <!-- 防止同一用户多次登录,使第二次登录失败  -->
  20. <session-management>
  21. <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
  22. </session-management>
  23. <!-- 增加一个filter,这点与Acegi是不一样的,不能修改默认的filter了,这个filter位于FILTER_SECURITY_INTERCEPTOR之前 -->
  24. <custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="urlSecurityFilter" />
  25. </http>
  26. <!-- 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性,
  27. 我们的所有控制将在这三个类中实现,解释详见具体配置 -->
  28. <beans:bean id="urlSecurityFilter" class="com.maxjay.main.system.web.filter.UrlSecurityInterceptorFilter">
  29. <beans:property name="authenticationManager" ref="authenticationManager" />
  30. <beans:property name="accessDecisionManager" ref="securityAccessDecisionManager" />
  31. <beans:property name="securityMetadataSource" ref="urlSecurityMetadataSource" />
  32. </beans:bean>
  33. <!-- 认证管理器,实现用户认证的入口,主要实现UserDetailsService接口即可 -->
  34. <authentication-manager alias="authenticationManager">
  35. <authentication-provider user-service-ref="userService">
  36. <!--   如果用户的密码采用加密的话,可以加点“盐”
  37. <password-encoder hash="md5" />
  38. -->
  39. </authentication-provider>
  40. </authentication-manager>
  41. </beans:beans>

其中userService实现了UserDetailsService用来与自己的用户表进行适配,UrlSecurityInterceptorFilter继承了AbstractSecurityInterceptor并实现了Filter接口,urlSecurityMetadataSource实现了FilterInvocationSecurityMetadataSource接口,securityAccessDecisionManager实现了AccessDecisionManager接口,它们都通过spring的注解声明为容器的一个对象,所以在配置文件中才能直接引用。

springsecurity3有提供了几个已经实现好的访问决策器,其抽象类为AbstractAccessDecisionManager,它使用投票机制(AccessDecisionVoter)来确定用户有没有权限访问某资源,其实现类有三个: 
AffirmativeBased:只要有一个投票器通过即审核通过 
ConsensusBased:只有当赞成票>反对票时 审核才会通过 
UnanimousBased:只要有一个投票器反对,审核就不通过 
你可以直接使用该抽象类的实现类,其配置如下:

  1. <beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
  2. <!-- false意味着当配置的投票器只投了弃权票时,不允许继续执行 -->
  3. <beans:property name="allowIfAllAbstainDecisions" value="false"/>
  4. <!-- 配置该决策器所需要的投票器 -->
  5. <beans:property name="decisionVoters">
  6. <beans:list>
  7. <beans:bean class="org.springframework.security.access.vote.RoleVoter"/>
  8. <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
  9. </beans:list>
  10. </beans:property>
  11. </beans:bean>

还是不懂的话,看看这篇文章应该容易理解一点

http://www.blogjava.net/youxia/archive/2008/12/07/244883.html

spring-security配置和原理简介的更多相关文章

  1. spring boot 之 spring security 配置

    Spring Security简介 之前项目都是用shiro,但是时过境迁,spring security变得越来越流行.spring security的前身是Acegi, acegi 我也玩过,那都 ...

  2. 深入理解Spring Security授权机制原理

    原创/朱季谦 在Spring Security权限框架里,若要对后端http接口实现权限授权控制,有两种实现方式. 一.一种是基于注解方法级的鉴权,其中,注解方式又有@Secured和@PreAuth ...

  3. spring security 配置多个AuthenticationProvider

    前言 发现很少关于spring security的文章,基本都是入门级的,配个UserServiceDetails或者配个路由控制就完事了,而且很多还是xml配置,国内通病...so,本文里的配置都是 ...

  4. Spring学习日志之Spring Security配置

    依赖引入 <dependency> <groupId>org.springframework.security</groupId> <artifactId&g ...

  5. Spring Security入门(2-3)Spring Security 的运行原理 4 - 自定义登录方法和页面

    参考链接,多谢作者: http://blog.csdn.net/lee353086/article/details/52586916 http元素下的form-login元素是用来定义表单登录信息的. ...

  6. Spring Security配置

    更加优雅地配置Spring Securiy(使用Java配置和注解):https://www.cnblogs.com/xxzhuang/p/5960001.html 采用注解方式实现security: ...

  7. Spring Security配置个过滤器也这么卷

    以前胖哥带大家用Spring Security过滤器实现了验证码认证,今天我们来改良一下验证码认证的配置方式,更符合Spring Security的设计风格,也更加内卷. CaptchaAuthent ...

  8. spring mvc 和spring security配置 web.xml设置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  9. 通过 Spring Security配置 解决X-Frame-Options deny 造成的页面空白 iframe调用问题

    spring Security下,X-Frame-Options默认为DENY,非Spring Security环境下,X-Frame-Options的默认大多也是DENY,这种情况下,浏览器拒绝当前 ...

  10. spring mvc 和spring security配置 spring-servlet.xml和spring-security.xml设置

    spring-servlet.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

随机推荐

  1. tp5.1 where 时间查询

    $where_time = []; if ($_GET['s_time'] && !isset($_GET['e_time'])){ $where_time = ['add_time' ...

  2. redis未设置idle超时时间导致连接过多

    今天ELK收集日志的时候,发现收集失败,查找各方面原因,最后在redis日志里面发现报错:[2489] 02 Jun 10:43:42 # Error allocating resoures for ...

  3. RocketMQ共包含9个模块

    rocketmq-common:通用的枚举.基类方法.或者数据结构,包名有admin.consumer.filter.hook.message rocketmq-remoting:使用netty的客户 ...

  4. jQuery BlockUI Plugin Demo 3(Page Blocking Examples)

    This page demonstrates several ways to block the page. Each button below activates blockUI and then ...

  5. 第四章 INI配置——《跟我学Shiro》

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2020820 第四章 INI配置——<跟我学Shiro> 博客分类: 跟我学Shir ...

  6. excel自学笔记 from av50264533

    1.函数公式 MINUTE(serial_number)  函数解读 Serial_number 表示一个时间值,其中包含要查找的分钟  函数公式 NOW()  函数解读 显示出现在的时间 计算通话时 ...

  7. 【C/C++开发】C++实现简单的线程池

    C++实现简单的线程池 线程池编程简介: 在我们的服务端的程序中运用了大量关于池的概念,线程池.连接池.内存池.对象池等等.使用池的概念后可以高效利用服务器端的资源,比如没有大量的线程在系统中进行上下 ...

  8. PL/SQL链接Oracle数据库 导出表结构和表数据

    打开pl/sql客户端(导出数据表结构) 在左侧 点击tabales 2 Tools-->Export User Objects,导出sql格式的文件 3 红色1 是你要选择导出的表,红色2 是 ...

  9. springboot没有webapp目录——手动添加

    src\main\webapp\ 参考文章:https://blog.csdn.net/fakerswe/article/details/80922536

  10. 20175316 盛茂淞 2018-2019-2 《Java程序设计》实验五 《网络安全与编程》 实验报告

    20175316 盛茂淞 2018-2019-2 <Java程序设计>实验五 <网络安全与编程> 实验报告 一.实验报告封面 课程:Java程序设计 班级:1753班 姓名:盛 ...