Spring Security 基于数据库的权限管理配置

目前在做一个原型系统,其中涉及到权限管理部分,研究了一下 Spring Security,由于网上资料都是在配置文件里面定义url权限的,基本上没有存在数据库中的。在这个过程中我在网上找了很多资料,但是没有一个是完全能够解决问题的,acegi的例子springside倒是有一个。 而下面这段是一位网上朋友提供的,还不错,解析的清楚,大家可以参考
applicationContext-security.xml文件如下:
Java代码 复制代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:security="http://www.springframework.org/schema/security"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-2.0.2.xsd">
  7. <!--
  8. FilterChainProxy会按顺序来调用这些filter,使这些filter能享用SpringIoc的功能,
  9. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON定义url比较前先转为小写
  10. PATTERN_TYPE_APACHE_ANT定义使用Apacheant的匹配模式
  11. -->
  12. <beanid="springSecurityFilterChain"
  13. class="org.springframework.security.util.FilterChainProxy">
  14. <propertyname="filterInvocationDefinitionSource">
  15. <value><![CDATA[
  16. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  17. PATTERN_TYPE_APACHE_ANT
  18. /**=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,securityContextHolderAwareRequestFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterSecurityInterceptor
  19. ]]></value>
  20. </property>
  21. </bean>
  22. <!--
  23. 集成过滤器(HttpSessionContextIntegrationFilter是集成过滤器的一个实现)
  24. 每次request前HttpSessionContextIntegrationFilter从Session中获取Authentication对象,在request完后
  25. 又把Authentication对象保存到Session中供下次request使用,此filter必须在其他Acegifilter前使用
  26. -->
  27. <beanid="httpSessionContextIntegrationFilter"
  28. class="org.springframework.security.context.HttpSessionContextIntegrationFilter"/>
  29. <!--
  30. 退出(Logout)过滤器退出登录操作
  31. -->
  32. <beanid="logoutFilter"
  33. class="org.springframework.security.ui.logout.LogoutFilter">
  34. <!--退出系统后系统跳转到此URL-->
  35. <constructor-argvalue="/login.action"/>
  36. <!--退出系统后的操作(调用logout方法)-->
  37. <constructor-arg>
  38. <list>
  39. <!--实现了LogoutHandler接口(logout方法)-->
  40. <refbean="rememberMeServices"/>
  41. <beanclass="org.springframework.security.ui.logout.SecurityContextLogoutHandler"/>
  42. </list>
  43. </constructor-arg>
  44. </bean>
  45. <!--
  46. 处理表单认证filter:
  47. 1.authenticationManager认证管理器
  48. 2.authenticationFailureUrl定义登录失败时转向的页面
  49. 3.defaultTargetUrl定义登录成功时转向的页面
  50. 4.filterProcessesUrl定义登录请求的地址
  51. 5.rememberMeServices在验证成功后添加cookie信息
  52. -->
  53. <beanid="authenticationProcessingFilter"
  54. class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter"
  55. p:authenticationManager-ref="authenticationManager"
  56. p:authenticationFailureUrl="/login.action?login_error=1"
  57. p:defaultTargetUrl="/user.action"
  58. p:filterProcessesUrl="/j_spring_security_check"
  59. p:rememberMeServices-ref="rememberMeServices"/>
  60. <!--
  61. 认证管理器(org.springframework.security.AuthenticationManager接口)
  62. org.springframework.security.providers.ProviderManager是认证管理器的一个实现,
  63. ProviderManager通过遍历一个提供者的集合来实现身份验证,
  64. 直到某一个认证提供者能够成功地验证该用户的身份
  65. -->
  66. <!--
  67. 通过Providers提供认证者列表,如果一个认证提供者失败可以尝试另外一个认证提供者,以保证获取不同来源的身份认证,如
  68. DaoAuthenticationProvider从数据库中读取用户信息验证身份
  69. AnonymousAuthenticationProvider匿名用户身份认证
  70. RememberMeAuthenticationProvider已存cookie中的用户信息身份认证
  71. 其它的还有
  72. AuthByAdapterProvider使用容器的适配器验证身份
  73. CasAuthenticationProvider根据Yale中心认证服务验证身份,用于实现单点登陆
  74. JaasAuthenticationProvider从JASS登陆配置中获取用户信息验证身份
  75. RemoteAuthenticationProvider根据远程服务验证用户身份
  76. RunAsImplAuthenticationProvider对身份已被管理器替换的用户进行验证
  77. X509AuthenticationProvider从X509认证中获取用户信息验证身份
  78. TestingAuthenticationProvider单元测试时使用
  79. 每个认证者会对自己指定的证明信息进行认证,如DaoAuthenticationProvider仅对UsernamePasswordAuthenticationToken这个证明信息进行认证。
  80. -->
  81. <beanid="authenticationManager"
  82. class="org.springframework.security.providers.ProviderManager"
  83. p:sessionController-ref="concurrentSessionController">
  84. <propertyname="providers">
  85. <list>
  86. <refbean="daoAuthenticationProvider"/>
  87. <bean
  88. class="org.springframework.security.providers.anonymous.AnonymousAuthenticationProvider"
  89. p:key="springsecurity"/>
  90. <bean
  91. class="org.springframework.security.providers.rememberme.RememberMeAuthenticationProvider"
  92. p:key="springsecurity"/>
  93. </list>
  94. </property>
  95. </bean>
  96. <!--阻止用户在成功登录之后再进行一次成功登录-->
  97. <beanid="concurrentSessionController"
  98. class="org.springframework.security.concurrent.ConcurrentSessionControllerImpl"
  99. p:maximumSessions="1"
  100. p:exceptionIfMaximumExceeded="true"
  101. p:sessionRegistry-ref="sessionRegistry"
  102. p:messageSource-ref="messageSource"/>
  103. <beanid="sessionRegistry"
  104. class="org.springframework.security.concurrent.SessionRegistryImpl"/>
  105. <beanid="messageSource"
  106. class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
  107. p:basename="/WEB-INF/classes/messages_zh_CN"/>
  108. <beanid="securityContextHolderAwareRequestFilter"
  109. class="org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter"/>
  110. <!--
  111. 利用cookie自动登陆filter
  112. 当SecurityContextHolder中不存在Authentication.用户授权信息,
  113. rememberMeProcessingFilter就会调用autoLogin()方法从cookie中获取用户信息,在验证filter之前使用
  114. -->
  115. <beanid="rememberMeProcessingFilter"
  116. class="org.springframework.security.ui.rememberme.RememberMeProcessingFilter"
  117. p:authenticationManager-ref="authenticationManager"
  118. p:rememberMeServices-ref="rememberMeServices"/>
  119. <!--
  120. 如果不存在任何授权信息时,自动添加匿名用户身份至SecurityContextHolder中
  121. -->
  122. <beanid="anonymousProcessingFilter"
  123. class="org.springframework.security.providers.anonymous.AnonymousProcessingFilter"
  124. p:key="springsecurity"
  125. p:userAttribute="anonymousUser,ROLE_ANONYMOUS"/>
  126. <!--
  127. 异常处理filter(异常转换过滤器),主要是处理AccessDeniedException和AuthenticationException,
  128. 将给每个异常找到合适的"去向"
  129. -->
  130. <beanid="exceptionTranslationFilter"
  131. class="org.springframework.security.ui.ExceptionTranslationFilter"
  132. p:accessDeniedHandler-ref="accessDeniedHandler"
  133. p:authenticationEntryPoint-ref="authenticationEntryPoint"/>
  134. <!--处理AccessDeniedException-->
  135. <beanid="accessDeniedHandler"
  136. class="org.springframework.security.ui.AccessDeniedHandlerImpl"
  137. p:errorPage="/accessDenied.jsp"/>
  138. <!---->
  139. <beanid="authenticationEntryPoint"
  140. class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint"
  141. p:loginFormUrl="/login.action"
  142. p:forceHttps="false"/>
  143. <!--
  144. 使用过滤器安全拦截器保护资源
  145. filterSecurityInterceptor在执行转向目标url前检查objectDefinitionSource中设定的用户权限信息,
  146. 安全强制过滤器负责拦截请求,判断请求是否安全,并且给予认证和访问决策管理器一个机会来验证用户的身份和权限
  147. 过程:
  148. 首先,过滤器安全拦截器使用authenticationManager调用自己的provider来对用户的认证信息进行验证并获取用户已有的权限。
  149. 然后,使用访问决策管理器来判断用户是否拥用合适的授权来访问受保护的资源。
  150. (objectDefinitionSource属性定义了访问URL需要的权限信息)
  151. 最后,有投票者根据用户持有认证和访问url需要的属性,调用自己的voter来投票,决定是否允许访问。
  152. -->
  153. <beanid="filterSecurityInterceptor"
  154. class="org.springframework.security.intercept.web.FilterSecurityInterceptor"
  155. p:authenticationManager-ref="authenticationManager"
  156. p:accessDecisionManager-ref="accessDecisionManager"
  157. p:objectDefinitionSource-ref="objectDefinitionSource">
  158. </bean>
  159. <beanid="objectDefinitionSource"
  160. class="com.shopin.modules.security.intercept.web.DataBaseFilterInvocationDefinitionSource"
  161. p:convertUrlToLowercaseBeforeComprison="true"
  162. p:useAntPath="true"
  163. p:cacheManager-ref="securityCacheManager"/>
  164. <!--
  165. 访问决策管理器
  166. 验证用户是否有权限访问相应的资源(filterSecurityInterceptor中objectDefinitionSource属性定义的访问URL需要的属性信息)
  167. -->
  168. <beanid="accessDecisionManager"
  169. class="org.springframework.security.vote.AffirmativeBased"
  170. p:allowIfAllAbstainDecisions="false">
  171. <propertyname="decisionVoters">
  172. <list>
  173. <beanclass="org.springframework.security.vote.RoleVoter"/>
  174. <beanclass="org.springframework.security.vote.AuthenticatedVoter"/>
  175. </list>
  176. </property>
  177. </bean>
  178. <beanid="rememberMeServices"
  179. class="org.springframework.security.ui.rememberme.TokenBasedRememberMeServices"
  180. p:key="springsecurity"
  181. p:userDetailsService-ref="userDetailsService"/>
  182. <beanid="daoAuthenticationProvider"
  183. class="org.springframework.security.providers.dao.DaoAuthenticationProvider"
  184. p:userCache-ref="userCache"
  185. p:passwordEncoder-ref="passwordEncoder"
  186. p:userDetailsService-ref="userDetailsService"/>
  187. <beanid="passwordEncoder"
  188. class="org.springframework.security.providers.encoding.Md5PasswordEncoder"/>
  189. <!--缓存配置-->
  190. <beanid="resourceCache"
  191. class="com.shopin.modules.security.resourcedetails.EhCacheResourceCache">
  192. <propertyname="cache">
  193. <beanclass="org.springframework.cache.ehcache.EhCacheFactoryBean"
  194. p:cacheManager-ref="cacheManager"
  195. p:cacheName="resourceCache"/>
  196. </property>
  197. </bean>
  198. <beanid="userCache"
  199. class="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache">
  200. <propertyname="cache">
  201. <beanclass="org.springframework.cache.ehcache.EhCacheFactoryBean"
  202. p:cacheManager-ref="cacheManager"
  203. p:cacheName="userCache"/>
  204. </property>
  205. </bean>
  206. <beanid="cacheManager"
  207. class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  208. p:configLocation="classpath:ehcache-hibernate.xml">
  209. </bean>
  210. <beanid="userDetailsService"class="cn.shopin.miniweb.service.security.UserDetailServiceImpl"/>
  211. <beanid="securityCacheManager"
  212. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  213. p:transactionManager-ref="transactionManager"
  214. p:proxyTargetClass="true">
  215. <propertyname="target">
  216. <beanclass="com.shopin.modules.security.cache.SecurityCacheManagerImpl"
  217. p:sessionFactory-ref="sessionFactory"
  218. p:resourcCache-ref="resourceCache"/>
  219. </property>
  220. <propertyname="transactionAttributes">
  221. <props>
  222. <propkey="init*">PROPAGATION_REQUIRED,readOnly</prop>
  223. <propkey="get*">PROPAGATION_REQUIRED,readOnly</prop>
  224. </props>
  225. </property>
  226. </bean>
  227. <beanid="loggerListener"
  228. class="org.springframework.security.event.authentication.LoggerListener"/>
  229. </beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值