環(huán)境:spring2.0+extjs
Acegi是基于Spring的一個(gè)開(kāi)源的安全認(rèn)證框架,現(xiàn)在的最新版本是Spring Security 2.0。它實(shí)現(xiàn)了簡(jiǎn)易配置的承諾,提高了開(kāi)發(fā)者的生產(chǎn)力。 它已經(jīng)是java平臺(tái)上應(yīng)用最廣的安全框架了,Spring Security 2.0又提供了一系列的新功能。
1. Spring Security是什么
Spring Security是目前用于替換acegi的框架,它提供了一系列新的功能。
大為簡(jiǎn)化了配置
繼承OpenID,標(biāo)準(zhǔn)單點(diǎn)登錄
支持windows NTLM,在windows合作網(wǎng)絡(luò)上實(shí)現(xiàn)單點(diǎn)登錄
支持JSR 250("EJB 3")的安全注解
支持AspectJ切點(diǎn)表達(dá)式語(yǔ)言
全面支持REST Web請(qǐng)求授權(quán)
長(zhǎng)期要求的支持組,層級(jí)角色和用戶管理API
提升了功能,使用后臺(tái)數(shù)據(jù)庫(kù)的remember-me實(shí)現(xiàn)
通過(guò)spring webflow 2.0對(duì)web狀態(tài)和流轉(zhuǎn)授權(quán)進(jìn)行新的支持
通過(guò)Spring Web Services 1.5加強(qiáng)對(duì)WSS(原來(lái)的WS-Security)的支持
2. 使用步驟
1. 第一步是下載最新的Spring Security 2.0框架jar文件,
將文件放到工程里面,比如/WEB-INF/lib/。
2. 在web.xml文件里配置DelegatingFilterProxy,默認(rèn)配置如下
第一種:
<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>
過(guò)濾器的默認(rèn)名稱是springSecurityFilterChain,如果你想修改名稱,需要配置targetBeanName參數(shù),參數(shù)值為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節(jié)點(diǎn)的配置
<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>
配置說(shuō)明:
lowercase-comparisons:表示URL比較前先轉(zhuǎn)為小寫(xiě)。
path-type:表示使用Apache Ant的匹配模式。
access-denied-page:訪問(wèn)拒絕時(shí)轉(zhuǎn)向的頁(yè)面。
access-decision-manager-ref:指定了自定義的訪問(wèn)策略管理器。當(dāng)系統(tǒng)角色名的前綴不是默認(rèn)的ROLE_時(shí),需要自定義訪問(wèn)策略管理器。
login-page:指定登錄頁(yè)面。
login-processing-url:指定了客戶在登錄頁(yè)面中按下 Sign In 按鈕時(shí)要訪問(wèn)的 URL。與登錄頁(yè)面form的action一致。其默認(rèn)值為:/j_spring_security_check。
authentication-failure-url:指定了身份驗(yàn)證失敗時(shí)跳轉(zhuǎn)到的頁(yè)面。
default-target-url:指定了成功進(jìn)行身份驗(yàn)證和授權(quán)后默認(rèn)呈現(xiàn)給用戶的頁(yè)面。
always-use-default-target:指定了是否在身份驗(yàn)證通過(guò)后總是跳轉(zhuǎn)到default-target-url屬性指定的URL。
logout-url:指定了用于響應(yīng)退出系統(tǒng)請(qǐng)求的URL。其默認(rèn)值為:/j_spring_security_logout。
logout-success-url:退出系統(tǒng)后轉(zhuǎn)向的URL。
invalidate-session:指定在退出系統(tǒng)時(shí)是否要銷毀Session。
max-sessions:允許用戶帳號(hào)登錄的次數(shù)。范例限制用戶只能登錄一次。
exception-if-maximum-exceeded: 默認(rèn)為false,此值表示:用戶第二次登錄時(shí),前一次的登錄信息都被清空。
當(dāng)exception-if-maximum-exceeded="true"時(shí)系統(tǒng)會(huì)拒絕第二次登錄。
我的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&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 用戶身份驗(yàn)證 (簡(jiǎn)單的模擬)
<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>
數(shù)據(jù)庫(kù)驗(yàn)證:
<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 圖片驗(yàn)證登陸部分
<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&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&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&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>