---
title: "spring security2配置文件学习小结"
author: "huttoncs"
date: "2015-09-02T15:13:14Z"
tags: []
categories: ["开源软件"]
slug: "spring-security2"
draft: false
lastmod: "2015-09-02T15:13:14Z"
id: 2240296
url: "https://huttoncs.iteye.com/blog/2240296"
---

内容 转自 [spring security2配置文件学习小结](http://mengyang.iteye.com/blog/383606)

 

**1.applicationContext-security的配置**   
使用命名空间,主要分为3个部分:   
a. &lt;http&gt;注册过滤器链,配置表单登陆,注销等   
b. 注册自定义的安全认证管理器   
c. 注册自定义的授权过滤器 

**2.&lt;http&gt;**   
a. &lt;http&gt;元素会创建一个FilterChainProxy和filter使用的bean。以前常常出现的因为filter顺序不正确产生的问题不会再出现了，现在这些过滤器的位置都是预定义好的。   
b. auto-config = true(默认为false)   
\==   
&lt;http&gt;   
    &lt;intercept-url pattern="/\*\*" access="ROLE\_USER" /&gt;   
    &lt;form-login /&gt;   
    &lt;anonymous /&gt;   
    &lt;http-basic /&gt;   
    &lt;logout /&gt;   
    &lt;remember-me /&gt;   
&lt;/http&gt;   
c. access-decision-manager-ref属性可以自定义的访问策略管理器。当系统角色名的前缀不是默认的ROLE\_时，需要自定义访问策略管理器。例如： 

&lt;!-- 访问投票管理 --&gt;  
 &lt;b:bean id="accessDecisionManager" class="org.springframework.security.vote.AffirmativeBased"&gt;  
  &lt;b:property name="decisionVoters"&gt;  
   &lt;b:list&gt;  
    &lt;b:bean class="org.springframework.security.vote.RoleVoter"&gt;  
     &lt;b:property name="rolePrefix" value="" /&gt;  
    &lt;/b:bean&gt;  
   &lt;/b:list&gt;  
  &lt;/b:property&gt;  
 &lt;/b:bean&gt;

 

**3.安全认证**   
a. &lt;authentication-manager alias="XX"/&gt;为AuthenticationManager注册一个别名，然后你可以application context的其他地方使用这个名字   
b. &lt;authentication-provider&gt;元素会创建一个DaoAuthenticationProvider bean;一个ProviderManager bean通常是由命名空间过程系统创建的， DaoAuthenticationProvider自动注册到它上面   
c. &lt;user-service&gt;元素会创建一个InMemoryDaoImpl。   
d. 使用&lt;custom-authentication-provider&gt;元素可以为ProviderManager注册另外的AuthenticationProvider bean 

**4.http/intercept-url**   
a. &lt;intercept-url pattern="/XX" access="ROLE\_XXX" /&gt;定义了对某个资源的访问需要某个角色的权限   
b. 对于在http/intercept-url中没有进行定义的URL，将会默认使用系统内置的过滤器链进行权限认证(就是说无须认证和授权就能访问了);   
c. 若使用了pattern="/\*" 则一般情况下要配置匿名认证过滤器--登陆页面也被保护的话则会陷入死循环中.   
d. 由于一般情况下资源对应的权限都配置在数据库的资源权限表中,所以这个元素一般情况下没用   
e. 如果数据库中的资源权限表中不存在该资源记录,那么匿名认证过滤器实际上时不需要的. 

**5.&lt;security:custom-filter&gt;**   
a. &lt;security:custom-filter position="XXX"/&gt;会使用自定义的过滤器替换(position)某个过滤器,但是有3个不能替换:HttpSessionContextIntegrationFilter, ExceptionTranslationFilter, FilterSecurityInterceptor.但是可以使用before或after   
b. 除了使用position外，还可以使用before，after加入自定义的过滤器，还有的关键字包括first，last代表这个过滤器链的头和尾 

**6.数据库的资源权限表中应该只保存.action的记录，把所有受保护的jsp页面放到/WEB-INF文件夹下，外部的用户就无法访问了，/WEB-INF下的JSP只能通过Servlet的转发访问。**