阅读(2587)

JSP 开发之Spring Security详解

最后一次修改 2017年08月21日

JSP 开发之Spring Security详解

前言:

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下最成熟的安全系统,它为我们提供了强大而灵活的企业级安全服务,如:

  •              认证授权机制

  •              Web资源访问控制

  •              业务方法调用访问控制

  •             领域对象访问控制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如下:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"  
  xmlns="http://java.sun.com/xml/ns/javaee"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
   
  <!--加载Spring XML配置文件 --> 
  <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
      classpath:securityConfig.xml       
    </param-value> 
  </context-param> 
   
  <!-- Spring Secutiry3.1的过滤器链配置 --> 
  <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> 
   
  <!-- Spring 容器启动监听器 --> 
  <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener> 
   
   
 <welcome-file-list> 
  <welcome-file>index.jsp</welcome-file> 
 </welcome-file-list> 
</web-app>

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

<?xml version="1.0" encoding="UTF-8"?> 
<b:beans xmlns="http://www.springframework.org/schema/security" 
xmlns:b="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 
 
  <!--登录页面不过滤 --> 
<http pattern="/login.jsp" security="none"/> 
<http access-denied-page="/accessDenied.jsp"> 
  <form-login login-page="/login.jsp"/> 
  <!--访问/admin.jsp资源的用户必须具有ROLE_ADMIN的权限 --> 
  <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/> 
  <!--访问/**资源的用户必须具有ROLE_USER的权限 --> 
<intercept-url pattern="/**" access="ROLE_USER"/> 
<session-management> 
  <concurrency-control max-sessions="1" error-if-maximum-exceeded="false"/> 
</session-management> 
</http> 
<authentication-manager> 
<authentication-provider> 
  <user-service> 
    <user name="john" password="john" authorities="ROLE_USER" /> 
    <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />                 <user name="guest" password="guest" authorities="ROLE_GUEST" />  
  </user-service> 
</authentication-provider> 
</authentication-manager> 
</b:beans>

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

<body> 
  <form action="j_spring_security_check" method="POST"> 
    <table> 
      <tr> 
        <td>用户:</td> 
        <td><input type='text'name='j_username'></td> 
      </tr> 
      <tr> 
        <td>密码:</td> 
        <td><input type='password'name='j_password'></td> 
      </tr> 
      <tr> 
        <td><input name="reset"type="reset"></td> 
        <td><input name="submit"type="submit"></td> 
      </tr> 
    </table> 
  </form> 
</body>

6、在WebRoot中创建accessDenied.jsp,

<body> 
 您的访问被拒绝,无权访问该资源!<br> 
</body>

  创建admin.jsp内容如下:

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

 修改index.jsp内容如下:

<body> 
    这是首页,欢迎<sec:authentication property="name"/>!<br> 
  <a href="admin.jsp" rel="external nofollow" >进入admin页面</a> 
  <a href="other.jsp" rel="external nofollow" >进入其它页面</a> 
 
 </body>

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

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

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

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

以上就是JSP 开发中Spring Security 的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!