---
title: "spring secutity 2.05的配置"
author: "dou_shini8820"
date: "2012-07-24T10:50:40Z"
tags: []
categories: ["编程语言"]
slug: "spring-secutity-2-05"
draft: false
lastmod: "2012-11-01T23:43:02Z"
id: 1605126
url: "https://doushini.iteye.com/blog/1605126"
---

spring secutity 2.05的配置  
参考：[http://blog.csdn.net/superboo/article/details/5025435](http://blog.csdn.net/superboo/article/details/5025435)  
xml配置中多次出现“/”，如login-page="/userLoginAction\_init"，是为了区分是一个引用的名字还是跳转到一个方法中  
1.导入jar  
2.在web.xml中引入spring secutity3.0  
 &lt;filter&gt;  
  &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;  
  &lt;filter-class&gt;  
   org.springframework.web.filter.DelegatingFilterProxy  
  &lt;/filter-class&gt;  
 &lt;/filter&gt;  
 &lt;filter-mapping&gt;  
  &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;  
  &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
 &lt;/filter-mapping&gt;  
3.在classpath下建一个applicationContext-security.xml  
3.1.导入  
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)" xmlns:security="[http://www.springframework.org/schema/security](http://www.springframework.org/schema/security)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
  xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd](http://www.springframework.org/schema/beans/spring-beans-3.0.xsd)  
                        [http://www.springframework.org/schema/security](http://www.springframework.org/schema/security) [http://www.springframework.org/schema/security/spring-security-3.0.xsd](http://www.springframework.org/schema/security/spring-security-3.0.xsd)"&gt;  
3.2哪些资源需要过滤  
&lt;http auto-config="true" access-denied-page="/jsp/accessDenied.jsp"&gt;   
        &lt;intercept-url pattern="/css/\*\*" filters="none" /&gt;   
        &lt;intercept-url pattern="/images/\*\*" filters="none" /&gt;   
        &lt;intercept-url pattern="/js/\*\*" filters="none" /&gt;   
        &lt;!-- 增加一个filter，这点与Acegi是不一样的，不能修改默认的filter了。这个filter位于FILTER\_SECURITY\_INTERCEPTOR之前  --&gt;   
        &lt;custom-filter ref="myFilter" before="FILTER\_SECURITY\_INTERCEPTOR" /&gt;   
配置需要特定角色访问的资源  
&lt;security:intercept-url pattern="/admin/\*\*" access="ROLE\_ADMIN" /&gt;  
&lt;security:intercept-url pattern="/space/\*\*" access="ROLE\_USER" /&gt;  
3.2.1登录（注销）页面设置  
&lt;form-login login-page="/login.jsp"  authentication-failure-url="/common/403.jsp"  default-target-url="/admin.jsp" /&gt;   
&lt;logout logout-success-url="/login.jsp"/&gt;  
&lt;/http&gt;

3.3认证管理方面（如何自定义一个filter，必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性，然后写三个类分别实现相应的接口）  
&lt;bean id="myFilter" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor"&gt;   
        &lt;!-- 认证管理器，实现用户认证的入口 --&gt;   
        &lt;property name="authenticationManager" ref="authenticationManager" /&gt;   
        &lt;!-- 访问决策器，决定某个用户具有的角色，是否有足够的权限去访问某个资源 --&gt;   
        &lt;property name="accessDecisionManager" ref="accessDecisionManager" /&gt;   
        &lt;!-- 资源源数据定义，即定义某一资源可以被哪些角色访问 --&gt;   
        &lt;property name="securityMetadataSource" ref="secureResourceFilterInvocationDefinitionSource" /&gt;   
    &lt;/bean&gt;  
3.3.1认证管理器  
&lt;security:authentication-manager alias="authenticationManager"&gt;   
        &lt;!-- 认证管理器提供者\[user-service-ref]引用的服务组件，通过securityManager进行对用户信息的认证--&gt;   
        &lt;security:authentication-provider ref="authenticationProvider"&gt;        
        &lt;/security:authentication-provider&gt;  
&lt;/security:authentication-manager&gt;   
3.3.1.1认证管理器提供者  
    &lt;bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"&gt;   
        &lt;property name="userDetailsService" ref="userDetailsService"/&gt;   
        &lt;!-- value设置为false,为了能在myAuthenticationfailureHandler（认证失败）中接受到该异常，通过异常响应不同的页面 --&gt;  
        &lt;property name="hideUserNotFoundExceptions" value="false"/&gt;  
        &lt;!-- 自定义密码加密校验机制 --&gt;  
        &lt;property name="passwordEncoder" ref="md5ShaPasswordEncoder"/&gt;  
    &lt;/bean&gt;  
3.3.2访问决策器  
&lt;bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased"&gt;   
        &lt;property name="allowIfAllAbstainDecisions" value="false" /&gt;   
        &lt;property name="decisionVoters"&gt;   
            &lt;list&gt;   
                &lt;bean class="org.springframework.security.access.vote.RoleVoter" /&gt;   
                &lt;bean class="org.springframework.security.access.vote.AuthenticatedVoter" /&gt;   
            &lt;/list&gt;   
        &lt;/property&gt;  
    &lt;/bean&gt;  
3.3.3资源源数据定义（将所有的资源和权限对应关系建立起来，即定义某一资源可以被哪些角色访问）  
&lt;beans:bean id="MySecurityMetadataSource" init-method="loadResourceDefine"  class="com.softvan.spring.security.InvocationSecurityMetadataSourceService"&gt;   
        &lt;beans:property name="roleService" ref="RoleService" /&gt;   
        &lt;beans:property name="actionService" ref="ActionService" /&gt;   
&lt;/beans:bean&gt;

 

 

高级//////////////////////////////////////////////////////////////////////////////////////  
3.2的另外一种配置  
建立一个资源表和一个资源角色中间表用于存放角色所能访问的url。  
然后再写一个自定义过滤器，用于读取表中角色所能访问的url。  
这样需要先导入这个自定义过滤器，  
&lt;beans:bean id="filterSecurityInterceptor"  
        class="org.springframework.security.intercept.web.FilterSecurityInterceptor" autowire="byType"&gt;  
        &lt;custom-filter before="FILTER\_SECURITY\_INTERCEPTOR"/&gt;  
        &lt;beans:property name="objectDefinitionSource" ref="filterInvocationDefinitionSource" /&gt;  
&lt;/beans:bean&gt;  
&lt;beans:bean id="filterInvocationDefinitionSource" class="com.lovo.JdbcFilterInvocationDefinitionSourceFactoryBean"&gt;  
        &lt;beans:property name="dataSource" ref="dataSource"/&gt;  
        &lt;beans:property name="resourceQuery" value="select m.address,r.descn from t\_module\_role mr join t\_module m on mr.m\_id=m.id join t\_role r on mr.r\_id=r.id; "/&gt;  
&lt;/beans:bean&gt;  
再写对应的com.lovo.JdbcFilterInvocationDefinitionSourceFactoryBean类

3.2.1的高级配置  
&lt;security:form-login login-page="/userLoginAction\_init" authentication-failure-handler-ref="myAuthenticationfailureHandler"  authentication-success-handler-ref="myAuthenticationSuccessHandler"  always-use-default-target="true" /&gt;  
&lt;security:access-denied-handler error-page="/accessDenied.jsp" /&gt;  
&lt;security:remember-me  user-service-ref="userDetailsService"  token-validity-seconds="123456789" /&gt;    
&lt;security:logout invalidate-session="true" logout-success-url="/" logout-url="/j\_spring\_security\_logout" /&gt;  
然后再配置两个filter  
&lt;!--自定义认证成功--&gt;   
    &lt;bean id="myAuthenticationSuccessHandler" class="com.miaopu.core.security.MyAuthenticationSuccessHandler"&gt;  
     &lt;property name="defaultTargetUrl" value="/userLoginAction\_entry" /&gt;   
        &lt;property name="alwaysUseDefaultTargetUrl" value="false" /&gt;  
        &lt;property name="userLoginService" ref="userLoginService"/&gt;   
    &lt;/bean&gt;  
&lt;!-- 认证失败 --&gt;   
    &lt;bean id="myAuthenticationfailureHandler" class="com.miaopu.core.security.MyAuthenticationFailureHandler"&gt;   
        &lt;property name="defaultFailureUrl" value="/userLoginAction\_loginFailure" /&gt;   
        &lt;property name="allowSessionCreation" value="false"/&gt;  
    &lt;/bean&gt;