【Spring Security實戰系列】Spring Security實戰(四)
阿新 • • 發佈:2019-02-15
<?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"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 配置不過濾的資源(靜態資源及登入相關).是忽略攔截某些資源的意思,主要是針對靜態資源 -->
<http pattern="/**/*.css" security="none"></http>
<http pattern="/**/*.jpg" security="none"></http>
<http pattern="/**/*.jpeg" security="none"></http>
<http pattern="/**/*.gif" security="none"></http>
<http pattern="/**/*.png" security="none"></http>
<http pattern="/js/*.js" security="none"></http>
<http pattern="/login.jsp" security="none"></http>
<http pattern="/getCode" security="none" /><!-- 不過濾驗證碼 -->
<http pattern="/test/**" security="none"></http><!-- 不過濾測試內容 -->
<http auto-config="false">
<intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<!-- 表示訪問app.jsp時,需要ROLE_SERVICE許可權 -->
<intercept-url pattern="/adminPage.jsp" access="hasRole('ROLE_ADMIN')"></intercept-url>
<!--表示訪問任何資源都需要ROLE_ADMIN許可權。-->
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"></intercept-url>
<!--
登陸頁面肯定是不能攔截的,任何人都應該可以訪問,
<intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />配置表示允許匿名使用者訪問,
就是不用身份都可以訪問;
還有另一種配置方式:<http pattern="/login.jsp" security="none"></http>,這種配置達到的目的都是一樣的。
-->
<!--
form-login這個標籤是配置登陸頁面的,其中的屬性login-page是配置登陸頁面的,
default-target-url配置登陸成功後跳轉到的頁面,
authentication-failure-url配置認證失敗後的跳轉頁面。
form-login標籤中還有一個特別要注意的屬性use-expressions,如果設定為true,
這配置access就要做相應的改變,否則專案啟動的時候會報錯。
如果use-expressns="true"時,則表示改為 SpEL 表示式。 SpEL 允許使用特定的訪問控制規則表示式語言。
與簡單的字串如 ROLE_USER 不同,配置檔案可以指明表示式語言觸發方法呼叫、引用系統屬性、計算機值等等。
如 :<intercept-url pattern="/login.jsp" access="permitAll" />
-->
<form-login login-page="/login.jsp" default-target-url="/index.jsp"
always-use-default-target="false"
authentication-failure-url="/login.jsp?error=true"></form-login>
<!--
logout這個標籤用來配置退出或者登出,其中的屬性invalidate-session,
配置否是要清除session,logout-success-url配置登出成功後的跳轉頁面,
logout-url提交退出或者登出的地址,因此我們在配置退出或者登出的時候,
只需要將url設定為/j_spring_security_logout即可,這個地址也是security內部實現了的。
-->
<logout invalidate-session="true" logout-success-url="/login.jsp"
logout-url="/j_spring_security_logout"></logout>
<!--將CSRF保護功能禁用,設定為true即為啟用-->
<!-- 必須新增此段宣告,禁用CSRF功能 -->
<!-- <csrf disabled="false"/>-->
<!--<csrf request-matcher-ref="csrfSecurityRequestMatcher"></csrf>-->
<!-- max-sessions只容許一個賬號登入,error-if-maximum-exceeded 後面賬號登入後踢出前一個賬號,
expired-url session過期跳轉介面
如果concurrency-control標籤配置了error-if-maximum-exceeded="true",max-sessions="1",
那麼第二次登入時,是登入不了的。如果error-if-maximum-exceeded="false",
那麼第二次是能夠登入到系統的,但是第一個登入的賬號再次發起請求時,會跳轉到expired-url配置的url中-->
<session-management session-authentication-error-url="/login.jsp">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="false"
expired-url="/login.jsp" session-registry-ref="sessionRegistry" />
</session-management>
<expression-handler ref="webexpressionHandler" ></expression-handler>
</http>
<!--這裡添加了一個屬性execludeUrls,允許人為排除哪些url。
這裡約定所有/rest/開頭的都是Rest服務地址,上面的配置就把/rest/排除在csrf驗證的範圍之外了.
原始碼可以發現,POST方法被排除在外了,也就是說只有GET|HEAD|TRACE|OPTIONS這4類方法會被放行,
其它Method的http請求,都要驗證_csrf的token是否正確,
而通常post方式呼叫rest服務時,又沒有_csrf的token,所以校驗失敗。
解決方法:自己弄一個Matcher-->
<!--<beans:bean id="csrfSecurityRequestMatcher" class="cn.quan.ssm.sec.CsrfSecurityRequestMatcher">
<beans:property name="execludeUrls">
<beans:list>
<beans:value>/rest/</beans:value>
</beans:list>
</beans:property>
</beans:bean>-->
<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl"/>
<!--配置web端使用許可權控制-->
<beans:bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>
<!-- 匯入資料來源 -->
<beans:import resource="applicationContext-dataSource.xml"></beans:import>
<!-- 預設資料庫對使用者進行儲存 Spring Security預設情況下需要兩張表,使用者表和許可權表。-->
<authentication-manager>
<authentication-provider>
<!-- <user-service>
<user name="admin" password="123" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="user" password="123" authorities="ROLE_USER" />
</user-service>-->
<jdbc-user-service data-source-ref="mysqlDataSource"
users-by-username-query="select username,`password`,`status` as enabled from `user` where username = ?"
authorities-by-username-query="select `user`.username,role.`name` from `user`,role,user_role where `user`.id=user_role.user_id and user_role.role_id=role.id and `user`.username = ?" />
</authentication-provider>
</authentication-manager>
</beans:beans>