Spring Security 3.x 出来一段时间了,跟Acegi是大不同了,与2.x的版本也有一些小小的区别,网上有一些文档,也有人翻译Spring Security 3.x的guide,但通过阅读guide,无法马上就能很容易的实现一个完整的实例。

我花了点儿时间,根据以前的实战经验,整理了一份完整的入门教程,供需要的朋友们参考。
1,建一个web project,并导入所有需要的lib,这步就不多讲了。
2,配置web.xml,使用Spring的机制装载:

Spring Security 3.x 完整入门教程<?xml version="1.0" encoding="UTF-8"?>
Spring Security 3.x 完整入门教程<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
Spring Security 3.x 完整入门教程    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Spring Security 3.x 完整入门教程    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
Spring Security 3.x 完整入门教程    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
Spring Security 3.x 完整入门教程    <context-param>
Spring Security 3.x 完整入门教程        <param-name>contextConfigLocation</param-name>
Spring Security 3.x 完整入门教程        <param-value>classpath:applicationContext*.xml</param-value>
Spring Security 3.x 完整入门教程    </context-param>
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程    <listener>
Spring Security 3.x 完整入门教程        <listener-class>
Spring Security 3.x 完整入门教程            org.springframework.web.context.ContextLoaderListener
Spring Security 3.x 完整入门教程        </listener-class>
Spring Security 3.x 完整入门教程    </listener>
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程    <filter>
Spring Security 3.x 完整入门教程        <filter-name>springSecurityFilterChain</filter-name>
Spring Security 3.x 完整入门教程        <filter-class>
Spring Security 3.x 完整入门教程            org.springframework.web.filter.DelegatingFilterProxy
Spring Security 3.x 完整入门教程        </filter-class>
Spring Security 3.x 完整入门教程    </filter>
Spring Security 3.x 完整入门教程    <filter-mapping>
Spring Security 3.x 完整入门教程        <filter-name>springSecurityFilterChain</filter-name>
Spring Security 3.x 完整入门教程        <url-pattern>/*</url-pattern>
Spring Security 3.x 完整入门教程    </filter-mapping>
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程    <welcome-file-list>
Spring Security 3.x 完整入门教程        <welcome-file>login.jsp</welcome-file>
Spring Security 3.x 完整入门教程    </welcome-file-list>
Spring Security 3.x 完整入门教程</web-app>
Spring Security 3.x 完整入门教程

这个文件中的内容我相信大家都很熟悉了,不再多说了。

2,来看看applicationContext-security.xml这个配置文件,关于Spring Security的配置均在其中:

Spring Security 3.x 完整入门教程<?xml version="1.0" encoding="UTF-8"?>
Spring Security 3.x 完整入门教程<beans:beans xmlns="http://www.springframework.org/schema/security"
Spring Security 3.x 完整入门教程    xmlns:beans="http://www.springframework.org/schema/beans"
Spring Security 3.x 完整入门教程    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Spring Security 3.x 完整入门教程    xsi:schemaLocation="http://www.springframework.org/schema/beans
Spring Security 3.x 完整入门教程           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Spring Security 3.x 完整入门教程           http://www.springframework.org/schema/security
Spring Security 3.x 完整入门教程           http://www.springframework.org/schema/security/spring-security-3.0.xsd">
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程    <http access-denied-page="/403.jsp"><!-- 当访问被拒绝时,会转到403.jsp -->
Spring Security 3.x 完整入门教程        <intercept-url pattern="/login.jsp" filters="none" />
Spring Security 3.x 完整入门教程        <form-login login-page="/login.jsp"
Spring Security 3.x 完整入门教程            authentication-failure-url="/login.jsp?error=true"
Spring Security 3.x 完整入门教程            default-target-url="/index.jsp" />
Spring Security 3.x 完整入门教程        <logout logout-success-url="/login.jsp" />
Spring Security 3.x 完整入门教程        <http-basic />
Spring Security 3.x 完整入门教程        <!-- 增加一个filter,这点与Acegi是不一样的,不能修改默认的filter了,这个filter位于FILTER_SECURITY_INTERCEPTOR之前 -->
Spring Security 3.x 完整入门教程        <custom-filter before="FILTER_SECURITY_INTERCEPTOR"
Spring Security 3.x 完整入门教程            ref="myFilter" />
Spring Security 3.x 完整入门教程    </http>
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程    <!-- 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性,
Spring Security 3.x 完整入门教程    我们的所有控制将在这三个类中实现,解释详见具体配置 -->
Spring Security 3.x 完整入门教程    <beans:bean >


3,来看看自定义filter的实现:

Spring Security 3.x 完整入门教程package com.robin.erp.fwk.security;
Spring Security 3.x 完整入门教程import java.io.IOException;
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程import javax.servlet.Filter;
Spring Security 3.x 完整入门教程import javax.servlet.FilterChain;
Spring Security 3.x 完整入门教程import javax.servlet.FilterConfig;
Spring Security 3.x 完整入门教程import javax.servlet.ServletException;
Spring Security 3.x 完整入门教程import javax.servlet.ServletRequest;
Spring Security 3.x 完整入门教程import javax.servlet.ServletResponse;
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程import org.springframework.security.access.SecurityMetadataSource;
Spring Security 3.x 完整入门教程import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
Spring Security 3.x 完整入门教程import org.springframework.security.access.intercept.InterceptorStatusToken;
Spring Security 3.x 完整入门教程import org.springframework.security.web.FilterInvocation;
Spring Security 3.x 完整入门教程import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程public class MyFilterSecurityInterceptor extends AbstractSecurityInterceptor
}

最核心的代码就是invoke方法中的InterceptorStatusToken token = super.beforeInvocation(fi);这一句,即在执行doFilter之前,进行权限的检查,而具体的实现已经交给accessDecisionManager了,下文中会讲述。

4,来看看authentication-provider的实现:

Spring Security 3.x 完整入门教程package com.robin.erp.fwk.security;
Spring Security 3.x 完整入门教程import java.util.ArrayList;
Spring Security 3.x 完整入门教程import java.util.Collection;
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程import org.springframework.dao.DataAccessException;
Spring Security 3.x 完整入门教程import org.springframework.security.core.GrantedAuthority;
Spring Security 3.x 完整入门教程import org.springframework.security.core.authority.GrantedAuthorityImpl;
Spring Security 3.x 完整入门教程import org.springframework.security.core.userdetails.User;
Spring Security 3.x 完整入门教程import org.springframework.security.core.userdetails.UserDetails;
Spring Security 3.x 完整入门教程import org.springframework.security.core.userdetails.UserDetailsService;
Spring Security 3.x 完整入门教程import org.springframework.security.core.userdetails.UsernameNotFoundException;
Spring Security 3.x 完整入门教程
}

在这个类中,你就可以从数据库中读入用户的密码,角色信息,是否锁定,账号是否过期等,我想这么简单的代码就不再多解释了。

5,对于资源的访问权限的定义,我们通过实现FilterInvocationSecurityMetadataSource这个接口来初始化数据。

Spring Security 3.x 完整入门教程package com.robin.erp.fwk.security;
Spring Security 3.x 完整入门教程import java.util.ArrayList;
Spring Security 3.x 完整入门教程import java.util.Collection;
Spring Security 3.x 完整入门教程import java.util.HashMap;
Spring Security 3.x 完整入门教程import java.util.Iterator;
Spring Security 3.x 完整入门教程import java.util.Map;
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程import org.springframework.security.access.ConfigAttribute;
Spring Security 3.x 完整入门教程import org.springframework.security.access.SecurityConfig;
Spring Security 3.x 完整入门教程import org.springframework.security.web.FilterInvocation;
Spring Security 3.x 完整入门教程import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
Spring Security 3.x 完整入门教程import org.springframework.security.web.util.AntUrlPathMatcher;
Spring Security 3.x 完整入门教程import org.springframework.security.web.util.UrlMatcher;
Spring Security 3.x 完整入门教程
}

看看loadResourceDefine方法,我在这里,假定index.jsp和i.jsp这两个资源,需要ROLE_ADMIN角色的用户才能访问。
这个类中,还有一个最核心的地方,就是提供某个资源对应的权限定义,即getAttributes方法返回的结果。注意,我例子中使用的是AntUrlPathMatcher这个path matcher来检查URL是否与资源定义匹配,事实上你还要用正则的方式来匹配,或者自己实现一个matcher。

6,剩下的就是最终的决策了,make a decision,其实也很容易,呵呵。

Spring Security 3.x 完整入门教程package com.robin.erp.fwk.security;
Spring Security 3.x 完整入门教程import java.util.Collection;
Spring Security 3.x 完整入门教程import java.util.Iterator;
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程import org.springframework.security.access.AccessDecisionManager;
Spring Security 3.x 完整入门教程import org.springframework.security.access.AccessDeniedException;
Spring Security 3.x 完整入门教程import org.springframework.security.access.ConfigAttribute;
Spring Security 3.x 完整入门教程import org.springframework.security.access.SecurityConfig;
Spring Security 3.x 完整入门教程import org.springframework.security.authentication.InsufficientAuthenticationException;
Spring Security 3.x 完整入门教程import org.springframework.security.core.Authentication;
Spring Security 3.x 完整入门教程import org.springframework.security.core.GrantedAuthority;
Spring Security 3.x 完整入门教程
Spring Security 3.x 完整入门教程

在这个类中,最重要的是decide方法,如果不存在对该资源的定义,直接放行;否则,如果找到正确的角色,即认为拥有权限,并放行,否则throw new AccessDeniedException("no right");这样,就会进入上面提到的403.jsp页面。

相关文章: