繼續SpringSecurity,今天我們來聊聊退出登錄時的頁面跳轉和session註銷。SpringSecurity提供了這方面的支持。在上一篇文章的基礎之上直接來聊聊:
springsecrity.xml
<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/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<!--
配置具體的規則:
auto-config="true" 不用自己編寫登錄的頁面,框架提供默認登錄頁面
use-expressions="false" 是否使用SPEL表達式(沒學習過)
-->
<beans:bean id="failureCDW" class="com.alibaba.failureCDW"></beans:bean>
<http auto-config="false" use-expressions="false">
<!--配置具體攔截的url,pattern是攔截的url,access是訪問被攔截的url需要的權限-->
<intercept-url pattern="/*" access="ROLE_USER" />
<intercept-url pattern="/**/to**" access="ROLE_USER" />
<intercept-url pattern="/**/gotoUserLoginPageJsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<!-- 定義跳轉的具體頁面
form-login是spring security命名空間配置登錄相關信息的標籤,它包含如下屬性:
1. login-page 自定義登錄頁url,默認爲/login
2. login-processing-url 登錄請求攔截的url,也就是form表單提交時指定的action
3. default-target-url 默認登錄成功後跳轉的url
4. always-use-default-target 是否總是使用默認的登錄成功後跳轉url
5. authentication-failure-url 登錄失敗後跳轉的url
6. username-parameter 用戶名的請求字段 默認爲userName
7. password-parameter 密碼的請求字段 默認爲password
8. authentication-success-handler-ref 指向一個
AuthenticationSuccessHandler用於處理認證成功的請求,不能和default-target-url
還有always-use-default-target同時使用
9. authentication-success-forward-url 用於authentication-failure-handler-ref
10. authentication-failure-handler-ref 指向一個AuthenticationFailureHandler用於處理失敗的認證請求
11. authentication-failure-forward-url 用於authentication-failure-handler-ref
12. authentication-details-source-ref 指向一個AuthenticationDetailsSource,在認證過濾器中使用
-->
<form-login
login-page="/SpringSecurityPageController/gotoUserLoginPageJsp.do"
default-target-url="/SpringSecurityPageController/toUserWelComePageJsp.do"
authentication-failure-url="/SpringSecurityPageController/gotoUserLoginPageJsp.do?error=error"
always-use-default-target="true"
username-parameter="username"
password-parameter="password" />
<!-- logout 屬性詳解
logout-url LogoutFilter要讀取的url,也就是指定spring security攔截的註銷url
logout-success-url 用戶退出後要被重定向的url
invalidate-session 默認爲true,用戶在退出後Http session失效
success-handler-ref 對一個LogoutSuccessHandler的引用,用來自定義退出成功後的操作
這裏需要注意的一點是,spring security 3.x默認的註銷攔截url爲/j_spring_security_logout,而4.x則默認使用/logout-->
<logout logout-url="/SpringSecurityPageController/toUserLogoutPageJsp*"
logout-success-url="/SpringSecurityPageController/gotoUserLoginPageJsp"
invalidate-session="true" />
<!--關閉跨域請求,不關閉就會攔截所有的訪問,顯示權限不夠-->
<csrf/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="chendawei" password="123456" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
在SpringSecurityPageController頁面跳轉的裏面加入:
/**
* 用戶退出成功以後的頁面
*/
@RequestMapping(value="/toUserLogoutPageJsp",method = RequestMethod.GET)
public String toUserLogoutPageJsp(HttpServletRequest request, HttpServletResponse response) {
try {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
new SecurityContextLogoutHandler().logout(request, response, auth);
}
//You can redirect wherever you want, but generally it's a good practice to show login screen again.
return "redirect:SpringSecurityPageController/toUserLogoutPageJsp";
}
catch (Exception e) {
System.out.println("SpringSecurityPageController/toUserLoginPageJsp Exception:" + e.getMessage());
return null;
}
}
以上大概就是主要的邏輯。因爲註銷就是將sesson退出消除,這個沒啥可說的。