Acegi基本原理等
http://acegi.group.iteye.com/group/wiki/375
Spring Security 3.x 完整入门教程
http://www.blogjava.net/fastzch/archive/2011/04/18/315028.html
- 关键步骤
1. 添加filter声明到web.xml
<filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>..., /WEB-INF/pm-applicationContext-security.xml</param-value> <!--Spring Security用到的配置文件,可以取自己喜欢的名字--> </context-param>
2. 使用安全命名空间配置<http>
3. 配置<authentication-manager>
- 典型实例:
A. Example 1 - 最简单的例子(无任何代码需要,常用于测试或者十分简单的认证授权管理)
Step 1: web.xml中的配置无特别之处, 参考上面的关键步骤1
Step 2: 在Spring Security配置文件中添加如下配置
<http auto-config='true'> <intercept-url pattern="/**" access="ROLE_USER"/> </http>
Step 3: 在Spring Security配置文件中添加如下配置
<authentication-manager> <authentication-provider> <user-service> <user name="jimi" password="jimispassword" authorities="ROLE_USER,ROLE_ADMIN"/> <user name="bob" password="bobspassword" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager>
B. Example 2 - 自定义认证管理器和决策管理器(通过插入自定义的过滤器来实现, 实现灵活和复杂的认证授权管理)
Step 1: web.xml中的配置无特别之处, 参考上面的关键步骤1
Step 2: 在Spring Security配置文件中添加类似如下配置
<http access-denied-page="/403.jsp"> <!--对每个资源URI的权限控制,配置在这里--> <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <intercept-url pattern="/index.html*" access="ROLE_HILL"/> <intercept-url pattern="/client.jsp*" access="ROLE_CLIENT"/> <intercept-url pattern="/page.jsp*" access="ROLE_PAGE"/> <intercept-url pattern="/admin.html*" access="ROLE_ADMIN"/> <intercept-url pattern="/**" access="ROLE_USER" /> <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" default-target-url="/index.jsp" /> <logout logout-success-url="/login.jsp" /> <http-basic /> <custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="PmFilter" /> </http>
Step 3: 登录页面login.jsp
<form action='/%context_path%/j_spring_security_check' method='POST'> <table> <tr> <td>User:</td> <td><input type='text' name='j_username' value=''></td> </tr> <tr> <td>Password:</td> <td><input type='password' name='j_password'/></td> </tr> <tr> <td colspan='2'><input name="submit" type="submit" value="Login"/></td> </tr> </table> </form>
Step 4: 在Spring Security配置文件中添加类似如下配置
我们的所有控制将在这三个类中实现,解释详见具体配置 -->
<beans:bean id=" PmFilter" class="com.pan.hill.security. PmFilterSecurityInterceptor">
<beans:property name="authenticationManager" ref=" authenticationManager" />
<beans:property name="accessDecisionManager" ref=" PmAccessDecisionManagerBean" />
<beans:property name="securityMetadataSource" ref=" securityMetadataSource" />
</beans:bean>
<!-- 认证管理器authenticationManager,实现用户认证的入口,主要实现UserDetailsService接口即可 -->
<authentication-manager alias="authenticationManager">
< authentication-provider user-service-ref=" PmUserDetailService">
<!-- 如果用户的密码采用加密的话,可以加点“盐” --><password-encoder hash="sha" />
</authentication-provider>
</authentication-manager>
<beans:bean id="PmUserDetailService" class="com.pan.hill.security.PmUserDetailService" />
<!-- 访问决策器accessDecisionManager,决定某个用户具有的角色,是否有足够的权限去访问某个资源 -->
<beans:bean id="PmAccessDecisionManagerBean" class="com.pan.hill.security. PmAccessDecisionManager">
</beans:bean>
<!-- 资源源数据定义securityMetadataSource,即定义某一资源可以被哪些角色访问 -->
<beans:bean id="securityMetadataSource" class="com.pan.hill.security. PmInvocationSecurityMetadataSource" />
Step 5: 实现custom-filter、authentication-provider、accessDecisionManager、securityMetadataSource
例子分别如下:PmFilterSecurityInterceptor、PmUserDetailService、PmAccessDecisionManager、PmInvocationSecurityMetadataSource
PmFilterSecurityInterceptor: 拦截器,是进行认证授权处理的起点;
PmUserDetailService:认证(authentication)信息加载,用户基本信息(密码、权限等)加载;
PmAccessDecisionManager:授权(authorization),对当前用户的实际ROLE和访问当前资源所需要的ROLE进行比较,如果符合,正常返回;否则,抛出异常;
PmInvocationSecurityMetadataSource: 授权(authorization)信息加载,加载资源(URI)和ROLE的对应关系,提供方法getAttributes(), 这个方法根据URL得到访问该URL应该拥有的ROLE集合;
Step 6: 实现URL和访问权限的对应关系,实际代码是实现在PmInvocationSecurityMetadataSource的loadResourceDefine() 方法中 (这个方法的代码如何实现,直接关系到访问权限配置的格式)
3 Key points:
i. login form的action属性值, user name, password字段名要符合spring security framework要求;
ii. 认证(authentication)信息加载在UserDetailService中实现;
iii. 对每个资源URI的权限控制设置,配置在spring security配置文件中.
P.S. 网上的一篇spring security详解教程,觉得不错,转过来