现在的位置: 首页 > 综合 > 正文

Spring Security 2.0 安全框架 使用及文件配置说明

2013年10月09日 ⁄ 综合 ⁄ 共 8221字 ⁄ 字号 评论关闭

 Spring Security 2.0 安全框架使用说明 结合extjs的图片验证登陆

环境:spring2.0+extjs

 

Acegi是基于Spring的一个开源的安全认证框架,现在的最新版本是Spring Security 2.0。它实现了简易配置的承诺,提高了开发者的生产力。 它已经是java平台上应用最广的安全框架了,Spring Security 2.0又提供了一系列的新功能。

 

1. Spring Security是什么

Spring Security是目前用于替换acegi的框架,它提供了一系列新的功能。

大为简化了配置

继承OpenID,标准单点登录

支持windows NTLM,在windows合作网络上实现单点登录

支持JSR 250("EJB 3")的安全注解

支持AspectJ切点表达式语言

全面支持REST Web请求授权

长期要求的支持组,层级角色和用户管理API

提升了功能,使用后台数据库的remember-me实现

通过spring webflow 2.0对web状态和流转授权进行新的支持

通过Spring Web Services 1.5加强对WSS(原来的WS-Security)的支持

 

2. 使用步骤

 1. 第一步是下载最新的Spring Security 2.0框架jar文件,

           将文件放到工程里面,比如/WEB-INF/lib/。

 2. 在web.xml文件里配置DelegatingFilterProxy,默认配置如下 

第一种: 

<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>

 

过滤器的默认名称是springSecurityFilterChain,如果你想修改名称,需要配置targetBeanName参数,参数值为springSecurityFilterChain,如下

第二种:

<filter> 
     <filter-name>myName</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
     <init-param> 
            <param-name>targetBeanName</param-name> 
            <param-value>springSecurityFilterChain</param-value> 
     </init-param> 
</filter> 
 
<filter-mapping> 
     <filter-name>myName</filter-name>     
     <url-pattern>/*</url-pattern>     
</filter-mapping>

 

  3. 新建文件spring-security.xml,可以在applicationContext.xml中载入,也可以在web.xml随applicationContext.xml 一起加载 .

    spring-security.xml  主要部分讲解:

          3.1 http节点的配置

<http auto-config="true" lowercase-comparisons="false" path-type="ant" access-denied-page="/accessDenied.jsp" access-decision-manager-ref="accessDecisionManager"> 
        <intercept-url pattern="/index.jsp*" access="ROLE_ANONYMOUS"/> 
        <intercept-url pattern="/logout.jsp*" access="ROLE_ANONYMOUS"/> 
        <intercept-url pattern="/accessDenied.jsp*" access="ROLE_ANONYMOUS"/> 
 
        <intercept-url pattern="/**/*.jsp*" access="ADMIN,SYS_MANAGER"/>          
        <intercept-url pattern="/**/*.htm*" access="ADMIN,SYS_MANAGER"/> 
        <intercept-url pattern="/**/*.html*" access="ADMIN,SYS_MANAGER"/> 
        <intercept-url pattern="/**/*.action*" access="ADMIN,SYS_MANAGER"/> 
        <intercept-url pattern="/**/*.ftl*" access="ADMIN,SYS_MANAGER"/> 
 
        <form-login login-page="/index.jsp" always-use-default-target="true" login-processing-url="/j_security_check" authentication-failure-url="/index.jsp?login_error=1" default-target-url="/main.action"/> 
        <concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="false"/> 
        <logout logout-url="/j_security_logout" logout-success-url="/index.jsp" invalidate-session="true"/> 
</http> 

 

配置说明:

           lowercase-comparisons:表示URL比较前先转为小写。
           path-type:表示使用Apache Ant的匹配模式。

          access-denied-page:访问拒绝时转向的页面。

          access-decision-manager-ref:指定了自定义的访问策略管理器。当系统角色名的前缀不是默认的ROLE_时,需要自定义访问策略管理器。

 

          login-page:指定登录页面。
          login-processing-url:指定了客户在登录页面中按下 Sign In 按钮时要访问的 URL。与登录页面form的action一致。其默认值为:/j_spring_security_check。
         authentication-failure-url:指定了身份验证失败时跳转到的页面。
         default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面。
         always-use-default-target:指定了是否在身份验证通过后总是跳转到default-target-url属性指定的URL。

 

         logout-url:指定了用于响应退出系统请求的URL。其默认值为:/j_spring_security_logout。
         logout-success-url:退出系统后转向的URL。
         invalidate-session:指定在退出系统时是否要销毁Session。

 

         max-sessions:允许用户帐号登录的次数。范例限制用户只能登录一次。

         exception-if-maximum-exceeded: 默认为false,此值表示:用户第二次登录时,前一次的登录信息都被清空。
          当exception-if-maximum-exceeded="true"时系统会拒绝第二次登录。

 

我的http部分:

 

<http auto-config='true' access-denied-page="/securityDispatch.action?action=denied">
 <intercept-url pattern="/index.htm*" filters="none" />
 <intercept-url pattern="/mainFrame.action*" access="ROLE_SP,ROLE_CP,ROLE_CARRIER" />
 <form-login login-page="/index.htm" default-target-url="/securityDispatch.action?action=success"
  always-use-default-target="true"
  authentication-failure-url="/securityDispatch.action?action=failure&amp;errorcode=100" />
 <logout invalidate-session="true" />
 <concurrent-session-control max-sessions="1"
  exception-if-maximum-exceeded="false" />
</http>

      

      3.2 自定义安全策略

<beans:bean id="menuLoader"class="com.szmeiton.security.ui.PropertiesLoader">
  <beans:property name="menusFile" value="../menu.properties" />
</beans:bean>

      3.3 用户身份验证 (简单的模拟)

  <authentication-provider>
    <user-service>
     <user name="sp" password="123" authorities="ROLE_SP" />
     <user name="admin" password="123" authorities="ROLE_CARRIER" />
     <user name="cp" password="123" authorities="ROLE_CP" />
   </user-service>
 </authentication-provider>

 

数据库验证:

<authentication-provider user-service-ref='userDetailsService'>
   <password-encoder hash="md5" />
</authentication-provider>
<beans:bean id="userDetailsService" class="org.springframework.security.userdetails.jdbc.JdbcDaoImpl">
 <beans:property name="dataSource" ref="dataSource" />
  <beans:property name="usersByUsernameQuery">
  <beans:value>select username,password,'true' as 'enabled' from t_users where username=?</beans:value>
 </beans:property>
 <beans:property name="authoritiesByUsernameQuery">
  <beans:value>select username,roletoken as 'authority' from t_users where username=?</beans:value>
 </beans:property>
</beans:bean> 

 

3.4 图片验证登陆部分

 <beans:bean id="imageVerifyCodeFilter"
  class="com.szmeiton.security.ImageVerifyCodeFilter">
  <custom-filter before="AUTHENTICATION_PROCESSING_FILTER" />
  <beans:property name="imageUrl" value="/verifyCode.jpg" />
  <beans:property name="verifyUrl" value="/j_spring_security_check" />
  <beans:property name="errorUrl" value="securityDispatch.action?action=failure&amp;errorcode=200" />
 </beans:bean>
 

 

 

            

               

这里是我完整的spring-security.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="
http://www.springframework.org/schema/security"
 xmlns:beans="
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-2.0.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"
 default-autowire="byType" default-lazy-init="true">
  <http auto-config='true' access-denied-page="/securityDispatch.action?action=denied">
  <intercept-url pattern="/index.htm*" filters="none" />
  <intercept-url pattern="/mainFrame.action*" access="ROLE_SP,ROLE_CP,ROLE_CARRIER" />
  <form-login login-page="/index.htm" default-target-url="/securityDispatch.action?action=success"
   always-use-default-target="true"
   authentication-failure-url="/securityDispatch.action?action=failure&amp;errorcode=100" />
  <logout invalidate-session="true" />
  <concurrent-session-control max-sessions="1"
   exception-if-maximum-exceeded="false" />
 </http>
 

 <authentication-provider>
  <user-service>
   <user name="sp" password="123" authorities="ROLE_SP" />
   <user name="admin" password="123" authorities="ROLE_CARRIER" />
   <user name="cp" password="123" authorities="ROLE_CP" />
  </user-service>
  
 </authentication-provider>

<!-- 
 <authentication-provider user-service-ref='userDetailsService'>
   <password-encoder hash="md5" />
 </authentication-provider>
 <beans:bean id="userDetailsService"
  class="org.springframework.security.userdetails.jdbc.JdbcDaoImpl">
  <beans:property name="dataSource" ref="dataSource" />
  <beans:property name="usersByUsernameQuery">
   <beans:value>select username,password,'true' as 'enabled' from
    t_users where username=?</beans:value>
  </beans:property>
  <beans:property name="authoritiesByUsernameQuery">
   <beans:value>select username,roletoken as 'authority' from
    t_users where username=?</beans:value>
  </beans:property>
 </beans:bean>
 --> 
 
 <beans:bean id="imageVerifyCodeFilter"
  class="com.szmeiton.security.ImageVerifyCodeFilter">
  <custom-filter before="AUTHENTICATION_PROCESSING_FILTER" />
  <beans:property name="imageUrl" value="/verifyCode.jpg" />
  <beans:property name="verifyUrl" value="/j_spring_security_check" />
  <beans:property name="errorUrl" value="securityDispatch.action?action=failure&amp;errorcode=200" />
 </beans:bean>
 
 <beans:bean id="menuLoader"
  class="com.szmeiton.security.ui.PropertiesLoader">
  <beans:property name="menusFile" value="../menu.properties" />
 </beans:bean>
 
</beans:beans>

 

抱歉!评论已关闭.