命名空間的配置包含如下幾個部分
- Web/HTTP安全:設(shè)置過濾器和一些用于框架驗(yàn)證機(jī)制的服務(wù)Bean。
- 業(yè)務(wù)對象的保護(hù) : 用于security服務(wù)層劲弦。
- AuthenticationManager : 權(quán)限管理器尸饺。處理從框架其他部分發(fā)來的權(quán)限請求君编。
- AccessDecisionManager:認(rèn)證管理器 拆火。 provides access decisions for web and method security歧譬。 系統(tǒng)會注冊一個默認(rèn)的認(rèn)證管理器循未。
- AuthenticationProviders
- UserDetailsService
使用命名空間(WEB)
1. web.xml中添加過濾器
<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>
用戶定義beans的時候不能將其命名為springSecurityFilterChain
陷猫。
2. 最精簡的<http>配置
<http>
<intercept-url pattern="/**" access="hasRole('USER')" />
<form-login />
<logout />
</http>
- 應(yīng)用中所有的url都收到保護(hù),需要ROLE_USER這個角色才能訪問的妖。
- 用戶使用表單進(jìn)行登錄绣檬,需要設(shè)計一個登出頁面。
- <intercept-url>
pattern:訪問路徑
access:訪問需求 - 可以使用多個<intercept-url>進(jìn)行配置嫂粟,但是系統(tǒng)會默認(rèn)使用第一個匹配的需求娇未,所以需要把最特殊匹配的放在最前面。
下面我們添加用戶
<authentication-manager>
<authentication-provider>
<user-service>
<user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="bobspassword" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
- <http>元素負(fù)責(zé)創(chuàng)建FilterChainProxy和他所使用的過濾器beans星虹。
- <authentication-manager>元素創(chuàng)建DaoAuthenticationProvider bean零抬。
- <user-service>元素創(chuàng)建InMemoryDaoImpl镊讼。
- 所有的<authentication-provider>元素必須是 <authentication-manager>的子元素, <authentication-manager>創(chuàng)建了一個ProviderManager并且為他注冊authentication providers平夜。
3. Form和基本的登錄選項(xiàng)
<http>
<intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page='/login.jsp' />
</http>
所有匿名用戶都可以進(jìn)入登錄頁面狠毯,其他頁面只有USER角色可以進(jìn)入。 AuthenticatedVoter-->查看此類源碼
** 可以用以下代碼來完全繞開security過濾器**
<http pattern="/css/**" security="none"/>
<http pattern="/login.jsp*" security="none"/>
<http use-expressions="false">
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page='/login.jsp' />
</http>
如果<http>元素省略了pattern屬性褥芒,那么他將對所有請求有效嚼松。
如果在<http>中設(shè)置路徑來繞開security過濾器,那么這些路徑中的內(nèi)容不能再訪問當(dāng)前用戶信息或者安全方法锰扶,如果還想在這些路徑中訪問献酗,那么可以考慮放棄將security="none",改為采用access='IS_AUTHENTICATED_ANONYMOUSLY'坷牛。
如果要使用基本用戶驗(yàn)證來代替表單驗(yàn)證罕偎,可以使用如下配置
<http use-expressions="false">
<intercept-url pattern="/**" access="ROLE_USER" />
<http-basic />
</http>
** 設(shè)置默認(rèn)登錄Post的目標(biāo)**
<http pattern="/login.htm*" security="none"/>
<http use-expressions="false">
<intercept-url pattern='/**' access='ROLE_USER' />
<form-login login-page='/login.htm' default-target-url='/home.htm' always-use-default-target='true' />
</http>
- 如果登陸界面并不是因?yàn)橛脩粢L問受保護(hù)資源而跳轉(zhuǎn)過來的,那么用戶登錄成功后將進(jìn)入default-target-url京闰。
- 如果強(qiáng)制用戶登錄成功后進(jìn)入default-target-url颜及,那么可以設(shè)置always-use-default-target='true'
- 如果要特別細(xì)地配置登錄成功后進(jìn)入的目標(biāo)可以通過設(shè)置 authentication-success-handlerref屬性來代替 default-target-url,這個屬性是類AuthenticationSuccessHandler的實(shí)例蹂楣。
** 登出配置**
系統(tǒng)默認(rèn)的登出鏈接是 /logout俏站,如果想在用戶登出時選擇其他路徑可以使用logout-url屬性。
** 使用其他 Authentication Providers**
用戶自定義UserDetailsService痊土,然后實(shí)例化為 myUserDetailsService肄扎。
<authentication-manager>
<authentication-provider user-service-ref='myUserDetailsService' />
</authentication-manager>
如果要使用數(shù)據(jù)庫進(jìn)行驗(yàn)證:
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="securityDataSource"/>
</authentication-provider>
</authentication-manager>
securityDataSource是DataSource bean的實(shí)例化名字,DataSource用來管理Spring Security的標(biāo)準(zhǔn)用戶數(shù)據(jù)表赁酝。
我們也可以配置一個JdbcDaoImpl犯祠,然后用user-service-ref屬性來引入:
<authentication-manager>
<authentication-provider user-service-ref='myUserDetailsService' />
</authentication-manager>
<beans:bean id="myUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
也可以使用標(biāo)準(zhǔn)的AuthenticationProvider:
<authentication-manager>
<authentication-provider ref='myAuthenticationProvider' />
</authentication-manager>
myAuthenticationProvider為用戶的一個bean的名字,這個bean需要實(shí)現(xiàn) AuthenticationProvider接口
** 添加密碼編碼**
<beans:bean name="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
<authentication-manager>
<authentication-provider>
<password-encoder ref="bcryptEncoder"/>
<user-service>
<user name="jimi" password="d7e6351eaa13189a5a3641bab846c8e8c69ba39f" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>