简介

Spring Security是一个能够为基于Spring的企业应用系统提供描述性安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC(依赖注入,也称控制反转)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。Spring Security 为基于J2EE企业应用软件提供了全面安全服务。特别是使用领先的J2EE解决方案-Spring框架开发的企业软件项目。

功能

Spring Security对Web安全性的支持大量地依赖于Servlet过滤器。这些过滤器拦截进入请求,并且在应用程序处理该请求之前进行某些安全处理。 Spring Security提供有若干个过滤器,它们能够拦截Servlet请求,并将这些请求转给认证和访问决策管理器处理,从而增强安全性。根据自己的需要,可以使用表7.4中所列的几个过滤器来保护自己的应用程序。

如果使用过Servlet过滤器,那么知道要让它们生效,就必须在Web应用程序的web.xml文件中使用<filter> 和<filter-mapping>元素配置它们。虽然这样做能起作用,但是它并不适用于使用依赖注入进行的配置。   

FilterToBeanProxy是一个特殊的Servlet过滤器,它本身做的工作并不多,而是将自己的工作委托给Spring应用程序上下文 中的一个Bean来完成。被委托的Bean几乎和其他的Servlet过滤器一样,实现javax.servlet.Filter接 口,但它是在Spring配置文件而不是web.xml文件中配置的。   

实际上,FilterToBeanProxy代理给的那个Bean可以是javax.servlet.Filter的任意实现。这可以是 Spring Security的任何一个过滤器,或者它可以是自己创建的一个过滤器。但是正如本书已经提到的那样,Spring Security要求至少配置四个而且可能一打或者更多的过滤器

 

通过在许多项目中实践应用以及社区的贡献,如今的Spring Security已经成为Spring Framework下最成熟的安全系统,它为我们提供了强大而灵活的企业级安全服务,如:

Ø        认证授权机制

Ø         

Ø        业务方法调用访问控制

Ø        领域对象访问控制Access Control List(ACL)

Ø        单点登录(Central Authentication Service)

Ø        X509认证

Ø        信道安全(Channel Security)管理等功能

简单例子 

1、创建web工程springSecurity3

2、把从spring网站下载的spring-security-3.1.0.RELEASE解压,并将其中的spring-security-samples-contacts-3.1.0.RELEASE.war解压,将jar包放到lib目录下。

3、修改配置web.xml如下:

     



[html]  view plain  copy

 

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <web-app version="2.5"   
3. xmlns="http://java.sun.com/xml/ns/javaee"   
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
6. >  
7.       
8. <!--加载Spring XML配置文件 -->  
9. <context-param>  
10. <param-name>contextConfigLocation</param-name>  
11. <param-value>  
12.             classpath:securityConfig.xml              
13. </param-value>  
14. </context-param>  
15.       
16. <!-- Spring Secutiry3.1的过滤器链配置  -->  
17. <filter>  
18. <filter-name>springSecurityFilterChain</filter-name>  
19. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
20. </filter>  
21.   
22. <filter-mapping>  
23. <filter-name>springSecurityFilterChain</filter-name>  
24. <url-pattern>/*</url-pattern>  
25. </filter-mapping>  
26.       
27. <!--  Spring 容器启动监听器 -->  
28. <listener>  
29. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
30. </listener>  
31.       
32.       
33. <welcome-file-list>  
34. <welcome-file>index.jsp</welcome-file>  
35. </welcome-file-list>  
36. </web-app>


4、在src下面创建securityConfig.xml文件内容如下:



[html]  view plain  copy

 



1. <?xml version="1.0" encoding="UTF-8"?>  
2. <b:beans xmlns="http://www.springframework.org/schema/security"  
3. xmlns:b="http://www.springframework.org/schema/beans"  
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
6. >  
7.   
8. <!--登录页面不过滤 -->  
9. <http pattern="/login.jsp" security="none"/>  
10. <http access-denied-page="/accessDenied.jsp">  
11. <form-login login-page="/login.jsp"/>  
12. <!--访问/admin.jsp资源的用户必须具有ROLE_ADMIN的权限 -->  
13. <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/>  
14. <!--访问/**资源的用户必须具有ROLE_USER的权限 -->  
15. <intercept-url pattern="/**" access="ROLE_USER"/>  
16. <session-management>  
17. <concurrency-control max-sessions="1" error-if-maximum-exceeded="false"/>  
18. </session-management>  
19. </http>  
20. <authentication-manager>  
21. <authentication-provider>  
22. <user-service>  
23. <user name="john" password="john" authorities="ROLE_USER" />  
24. <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />                                  <user name="guest" password="guest" authorities="ROLE_GUEST" />    
25. </user-service>  
26. </authentication-provider>  
27. </authentication-manager>  
28. </b:beans>



5、在WebRoot中创建login.jsp内容如下:



[html]  view plain  copy

 



1. <body>  
2. <form action="j_spring_security_check" method="POST">  
3. <table>  
4. <tr>  
5. <td>用户:</td>  
6. <td><input type='text'name='j_username'></td>  
7. </tr>  
8. <tr>  
9. <td>密码:</td>  
10. <td><input type='password'name='j_password'></td>  
11. </tr>  
12. <tr>  
13. <td><input name="reset"type="reset"></td>  
14. <td><input name="submit"type="submit"></td>  
15. </tr>  
16. </table>  
17. </form>  
18. </body>



6、在WebRoot中创建accessDenied.jsp,



[html]  view plain  copy

 


1. <body>  
2. <br>  
3. </body>


  创建admin.jsp内容如下:



[html]  view plain  copy

 



    1. <body>  
    2. 欢迎来到管理员页面. <br>  
    3. </body>



     

    修改index.jsp内容如下:



    [html]  view plain  copy

     



      1. <span style="font-size:12px;"><body>  
      2. <sec:authentication property="name"/>!<br>  
      3. <a href="admin.jsp">进入admin页面</a>  
      4. <a href="other.jsp">进入其它页面</a>  
      5.   
      6. </body></span>



      好了,部署项目,并访问index.jsp.

      用户名就是刚才部署的那个用户名。什么?忘了。那好吧,我再给你指出来

      <user name="john" password="john" authorities="ROLE_USER" />
        <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />

      权限不同访问的页面就不同。可以试试的