1. 程式人生 > >Spring Security簡明實踐及相關國際化處理

Spring Security簡明實踐及相關國際化處理

logs replace accept remember 沒有 dex glob ada XML

別人的都是最佳實踐,因為我目前的設置沒有按照參考文檔推薦,還是采用DelegatingFilterProxy,所以我只能說簡明實踐。先貼我的applicationContext-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/<a href="http://lib.csdn.net/base/javaee" class=‘replace_word‘ title="Java EE知識庫" target=‘_blank‘ style=‘color:#df3434; font-weight:bold;‘>spring</a>
-beans-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"> <global-method-security secured-annotations="enabled"> </global-method-security> <http auto-config="true">
<!-- intercept-url pattern="/**" filters="none" /--> <intercept-url pattern="/login.jsp*" filters="none"/> <intercept-url pattern="/common/pages/**" access="IS_AUTHENTICATED_REMEMBERED" /> <intercept-url pattern="/common/**" filters="none" /> <intercept-url pattern="/secure/extreme/**" access="ROLE_SUPERVISOR"/> <intercept-url pattern="/secure/**" access="IS_AUTHENTICATED_REMEMBERED" /> <intercept-url pattern="/**" access="IS_AUTHENTICATED_REMEMBERED" /> <form-login login-page=‘/login.jsp‘ authentication-failure-url="/login.jsp?login_error=1" default-target-url=‘/index.jsp‘ /> </http> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" group-authorities-by-username-query="select U.username,G.group_name,GA.authority as ‘authority‘ from users U join group_members GM on U.username=GM.username join groups G on GM.group_id=G.id join group_authorities GA on G.id=GA.group_id where U.username=?" /> <password-encoder hash="plaintext"/> </authentication-provider> </beans:beans>

對上面的配置說明一下,form-login的authentication-failure-url和default-target-url屬性設置基本就可以免去使用ExceptionTranslationFilter的麻煩,authentication-provider使用上面這個配置方式是最方便可用的,至於因為我的數據庫建立在sql Server上,所以添加了group-authorities-by-username-query屬性,password-encoder我個人建議采用參考文檔上推薦的以username做salt的sha編碼,我這兒使用plaintext,是因為我這個項目目前還在開發測試階段,用戶管理這個模塊還沒有完全建立,所以為了測試的方便,用了plaintext的編碼。那個dataSource,我是按照API文檔推薦,建立了一個容器JNDI,然後在spring裏用JndiObjectFactoryBean代理了一下。

用戶管理的設施代碼采用以JdbcUserDetailsManager為主,jdbcTemplate為輔的方式,其中前者不要建立在springContext下面,否則會導致多userDetailService沖突。

下面說一下spring Security國際化(i18N)的配置處理,先貼代碼:

<bean id="messageSource"  
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
        <property name="basename" value="/WEB-INF/ssm"/>  
    </bean>  
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/>  

註意了:我上面的bean定義不在applicationContext-security.xml裏面,而是在一個以beans作為默認命名空間的applicationContext-beans.xml裏面,我說一下上面這個配置做的時候出現的問題,其中的basename的value,我剛開始采用開發指南上說的"org/springframework/security/messages",可是始終找不到jar包裏面的messages文件,後來,我看了一個老外的同問題貼,他采用上面這種方式,把messages文件放在了/WEB-INF/下面,反正這種配置方式下,messages文件終於可以找到,i18N可以正常運行了,至於那個messages文件,還是從jar包裏面解壓出來的東西。

Spring Security簡明實踐及相關國際化處理