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

Code
<?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屬性為配置過濾器的種類與先后順序,注意,順序不能配置錯誤哦 -->
<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>
<!-- 看看你是否已經登錄了,如果登錄了就略過下面的過濾器了,直接訪問資源 -->
<bean id="httpSessionIntegrationFilter"
class="org.springframework.security.context.HttpSessionContextIntegrationFilter" />
<!-- 安全驗證入口 -->
<bean id="authenticationEntryPoint"
class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/index.jsp" /><!--默認登錄頁面-->
<property name="forceHttps" value="true" /><!--使登錄頁面通過HTTPS安全地進行顯示-->
</bean>
<!-- 身份驗證過濾器,就是驗證身份用的嘛 -->
<bean id="authenticationProcessingFilter"
class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
<!-- 驗證連接名稱,對應表單的action -->
<property name="filterProcessesUrl"
value="/j_spring_security_check" />
<!-- 驗證失敗后去哪 -->
<property name="authenticationFailureUrl"
value="/index.jsp?error=1" />
<!-- 驗證成功后去哪 -->
<property name="defaultTargetUrl"
value="/security/security.jsp" />
<!--依靠一個身份驗證管理器來驗證身份 其實這個才是干活的BEAN-->
<property name="authenticationManager"
ref="authenticationManager" />
</bean>
<!-- 用于處理登錄失敗異常和權限不足異常 -->
<bean id="exceptionTranslationFilter"
class="org.springframework.security.ui.ExceptionTranslationFilter">
<!--配置出現exception時跳轉到登錄頁-->
<property name="authenticationEntryPoint"
ref="authenticationEntryPoint" />
<!--配置403(權限不足)錯誤后跳轉的頁面-->
<property name="accessDeniedHandler" ref="accessDeniedHandler" />
</bean>
<!-- 配置權限不足時跳轉到的頁面 -->
<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">
<!-- 驗證管理者 -->
<property name="authenticationManager"
ref="authenticationManager" />
<!-- 權限決定管理者,他手下的一幫人投票決定登錄者是否有權訪問該資源 -->
<property name="accessDecisionManager"
ref="accessDecisionManager" />
<!--受保護資源-->
<property name="objectDefinitionSource">
<!-- 下面表示/security/security.jsp需要ROLE_ADMIN權限才能訪問 -->
<value><![CDATA[
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/security/security.jsp=ROLE_ADMIN]]>
</value>
</property>
</bean>
<!-- 驗證管理者,他管理DAO驗證提供者來驗證 -->
<bean id="authenticationManager"
class="org.springframework.security.providers.ProviderManager">
<property name="providers">
<list>
<!-- DAO驗證提供者,SPRING SECURITY支持各種驗證,這里可以添加相應配置 -->
<ref local="daoAuthenticationProvider" />
</list>
</property>
</bean>
<!-- -->
<bean id="accessDecisionManager"
class="org.springframework.security.vote.AffirmativeBased">
<!-- 如果所有投票者都棄權則不讓訪問 -->
<property name="allowIfAllAbstainDecisions">
<value>false</value>
</property>
<!-- 參加投票的BEAN -->
<property name="decisionVoters">
<list>
<bean class="org.springframework.security.vote.RoleVoter">
<!-- 權限的前綴 -->
<property name="rolePrefix" value="ROLE_" />
</bean>
<bean class="org.springframework.security.vote.AuthenticatedVoter" />
</list>
</property>
</bean>
<!-- DAO驗證提供者依靠userDetailsService獲得一個userDetails實例,進而驗證權限 -->
<bean id="daoAuthenticationProvider"
class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
<!-- jdbcDaoImpl實現了userDetailsService接口 -->
<property name="userDetailsService">
<ref local="jdbcDaoImpl" />
</property>
</bean>
<bean id="jdbcDaoImpl"
class="org.springframework.security.userdetails.jdbc.JdbcDaoImpl">
<!-- 根據用戶名獲得用戶名、密碼、用戶是否啟用等信息 -->
<property name="usersByUsernameQuery">
<value>
select username,password,enabled from user where
username=?
</value>
</property>
<!-- 通過用戶名獲取用戶權限 -->
<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>
這個是最簡單的一個例子,配了141行,呼~~繼續研究其深入功能,離成功越來越近了
ps.傳說spring security2.0有了超級簡單的配置方法,還沒有學到手,努力ing
源碼下載(需要自己添加spring和mysql的jar包)
文章來源:
http://www.cnblogs.com/xiaoao808/archive/2008/08/04/1259523.html
posted on 2008-08-04 00:46
破名超難起 閱讀(665)
評論(0) 編輯 收藏