經(jīng)過若干天斷斷續(xù)續(xù)地研究,終于做出了第一個(gè)spring security的實(shí)例,真是艱難啊,配置太復(fù)雜了,若干個(gè)Bean之間存在著這樣或那樣的關(guān)系......
下面給出我的小例子,主要是配置文件拉~~別的東西自己看源碼吧!


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<!-- 過濾器鏈配置,其中filterInvocationDefinitionSource屬性為配置過濾器的種類與先后順序,注意,順序不能配置錯(cuò)誤哦 -->
<bean id="filterChainProxy"
class="org.springframework.security.util.FilterChainProxy">
<property name="filterInvocationDefinitionSource">
<value><![CDATA[
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=httpSessionIntegrationFilter,authenticationProcessingFilter,exceptionTranslationFilter,filterSecurityInterceptor]]>
</value>
</property>
</bean>
<!-- 看看你是否已經(jīng)登錄了,如果登錄了就略過下面的過濾器了,直接訪問資源 -->
<bean id="httpSessionIntegrationFilter"
class="org.springframework.security.context.HttpSessionContextIntegrationFilter" />
<!-- 安全驗(yàn)證入口 -->
<bean id="authenticationEntryPoint"
class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/index.jsp" /><!--默認(rèn)登錄頁(yè)面-->
<property name="forceHttps" value="true" /><!--使登錄頁(yè)面通過HTTPS安全地進(jìn)行顯示-->
</bean>
<!-- 身份驗(yàn)證過濾器,就是驗(yàn)證身份用的嘛 -->
<bean id="authenticationProcessingFilter"
class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
<!-- 驗(yàn)證連接名稱,對(duì)應(yīng)表單的action -->
<property name="filterProcessesUrl"
value="/j_spring_security_check" />
<!-- 驗(yàn)證失敗后去哪 -->
<property name="authenticationFailureUrl"
value="/index.jsp?error=1" />
<!-- 驗(yàn)證成功后去哪 -->
<property name="defaultTargetUrl"
value="/security/security.jsp" />
<!--依靠一個(gè)身份驗(yàn)證管理器來驗(yàn)證身份 其實(shí)這個(gè)才是干活的BEAN-->
<property name="authenticationManager"
ref="authenticationManager" />
</bean>
<!-- 用于處理登錄失敗異常和權(quán)限不足異常 -->
<bean id="exceptionTranslationFilter"
class="org.springframework.security.ui.ExceptionTranslationFilter">
<!--配置出現(xiàn)exception時(shí)跳轉(zhuǎn)到登錄頁(yè)-->
<property name="authenticationEntryPoint"
ref="authenticationEntryPoint" />
<!--配置403(權(quán)限不足)錯(cuò)誤后跳轉(zhuǎn)的頁(yè)面-->
<property name="accessDeniedHandler" ref="accessDeniedHandler" />
</bean>
<!-- 配置權(quán)限不足時(shí)跳轉(zhuǎn)到的頁(yè)面 -->
<bean id="accessDeniedHandler"
class="org.springframework.security.ui.AccessDeniedHandlerImpl">
<property name="errorPage" value="/error.jsp" />
</bean>
<!-- 安全攔截器,下面看看它是干嘛的 -->
<bean id="filterSecurityInterceptor"
class="org.springframework.security.intercept.web.FilterSecurityInterceptor">
<!-- 驗(yàn)證管理者 -->
<property name="authenticationManager"
ref="authenticationManager" />
<!-- 權(quán)限決定管理者,他手下的一幫人投票決定登錄者是否有權(quán)訪問該資源 -->
<property name="accessDecisionManager"
ref="accessDecisionManager" />
<!--受保護(hù)資源-->
<property name="objectDefinitionSource">
<!-- 下面表示/security/security.jsp需要ROLE_ADMIN權(quán)限才能訪問 -->
<value><![CDATA[
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/security/security.jsp=ROLE_ADMIN]]>
</value>
</property>
</bean>
<!-- 驗(yàn)證管理者,他管理DAO驗(yàn)證提供者來驗(yàn)證 -->
<bean id="authenticationManager"
class="org.springframework.security.providers.ProviderManager">
<property name="providers">
<list>
<!-- DAO驗(yàn)證提供者,SPRING SECURITY支持各種驗(yàn)證,這里可以添加相應(yīng)配置 -->
<ref local="daoAuthenticationProvider" />
</list>
</property>
</bean>
<!-- -->
<bean id="accessDecisionManager"
class="org.springframework.security.vote.AffirmativeBased">
<!-- 如果所有投票者都棄權(quán)則不讓訪問 -->
<property name="allowIfAllAbstainDecisions">
<value>false</value>
</property>
<!-- 參加投票的BEAN -->
<property name="decisionVoters">
<list>
<bean class="org.springframework.security.vote.RoleVoter">
<!-- 權(quán)限的前綴 -->
<property name="rolePrefix" value="ROLE_" />
</bean>
<bean class="org.springframework.security.vote.AuthenticatedVoter" />
</list>
</property>
</bean>
<!-- DAO驗(yàn)證提供者依靠userDetailsService獲得一個(gè)userDetails實(shí)例,進(jìn)而驗(yàn)證權(quán)限 -->
<bean id="daoAuthenticationProvider"
class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
<!-- jdbcDaoImpl實(shí)現(xiàn)了userDetailsService接口 -->
<property name="userDetailsService">
<ref local="jdbcDaoImpl" />
</property>
</bean>
<bean id="jdbcDaoImpl"
class="org.springframework.security.userdetails.jdbc.JdbcDaoImpl">
<!-- 根據(jù)用戶名獲得用戶名、密碼、用戶是否啟用等信息 -->
<property name="usersByUsernameQuery">
<value>
select username,password,enabled from user where
username=?
</value>
</property>
<!-- 通過用戶名獲取用戶權(quán)限 -->
<property name="authoritiesByUsernameQuery">
<value>
select username,authority from authentication where
username=?
</value>
</property>
<!-- DataSource,不用我說了吧 -->
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="org.gjt.mm.mysql.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/user">
</property>
<property name="username" value="root"></property>
<property name="password" value="hicc"></property>
</bean>
</beans>
這個(gè)是最簡(jiǎn)單的一個(gè)例子,配了141行,呼~~繼續(xù)研究其深入功能,離成功越來越近了
ps.傳說spring security2.0有了超級(jí)簡(jiǎn)單的配置方法,還沒有學(xué)到手,努力ing
文章來源:http://www.cnblogs.com/xiaoao808/archive/2008/08/04/1259523.html