別人的都是最佳實務,因為我目前的設定沒有按照參考文檔推薦,還是採用DelegatingFilterProxy,所以我只能說簡明實踐。先貼我的applicationContext-security.xml
<?xml version="1.0" encoding="UTF-8"?></p><p><beans:beans xmlns="http://www.springframework.org/schema/security"<br /> xmlns:beans="http://www.springframework.org/schema/beans"<br /> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br /> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd<br /> http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"><br /><global-method-security secured-annotations="enabled"><br /></global-method-security><br /> <http auto-config="true"></p><p> <!-- intercept-url pattern="/**" filters="none" /--><br /><intercept-url pattern="/login.jsp*" filters="none"/><br /><intercept-url pattern="/common/pages/**" access="IS_AUTHENTICATED_REMEMBERED" /><br /><intercept-url pattern="/common/**" filters="none" /><br /> <intercept-url pattern="/secure/extreme/**" access="ROLE_SUPERVISOR"/><br /> <intercept-url pattern="/secure/**" access="IS_AUTHENTICATED_REMEMBERED" /><br /> <intercept-url pattern="/**" access="IS_AUTHENTICATED_REMEMBERED" /></p><p> <form-login login-page='/login.jsp' authentication-failure-url="/login.jsp?login_error=1" default-target-url='/index.jsp' /></p><p> </http><br /><authentication-provider><br /><jdbc-user-service data-source-ref="dataSource"<br />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=?"<br />/></p><p><password-encoder hash="plaintext"/><br /></authentication-provider><br /></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"<br />class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><br /><property name="basename" value="/WEB-INF/ssm"/><br /></bean><br /><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包裡面解壓出來的東西。