1. 概述
本文基於我們提供的 Form Login 教程,將重點介紹如何配置 退出登錄與 Spring Security。
2. 基本配置
使用 logout() 方法的 Spring 註銷功能 的基本配置相當簡單:@Configuration
@EnableWebSecurity
public class SecSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
//...
.logout()
//...
}
//...
}並且使用XML配置:
<http>
...
<logout/>
</http>該元素啓用默認註銷機制——該機制配置使用以下 註銷 URL: /logout,此前在 Spring Security 4中為 /j_spring_security_logout。
3. JSP 與註銷鏈接
繼續這個簡單的示例,在 Web 應用程序中提供 註銷鏈接 的方法如下:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head></head>
<body>
<a href="<c:url value="/logout" />">Logout</a>
</body>
</html>4. 高級定製
This section covers advanced customization options for the system. These options allow you to tailor the system to your specific needs and workflows.
4.1. Custom Event Handling
You can define custom event handlers to respond to specific actions within the system. This allows you to perform actions based on triggers, such as user actions, data changes, or system events.
4.1.1. Defining Event Handlers
To define an event handler, you need to:
- Specify the event type you want to listen for.
- Provide the code that will be executed when the event occurs.
// Example: Handling a 'user_created' event
function onUserCreated(userId, username, email) {
// Perform actions related to the new user
console.log("New user created:", userId, username, email);
// Example: Send a welcome email
// sendWelcomeEmail(userId, username, email);
}
// Register the event handler
system.on('user_created', onUserCreated);
4.2. Custom UI Components
You can create custom UI components to extend the system's user interface. This allows you to add new elements and functionality to the interface.
4.2.1. Creating Custom Components
To create a custom component, you need to:
- Define the component's structure and appearance.
- Implement the component's functionality.
<!-- Example: A simple custom button -->
<button id="myButton" onclick="doSomething()">Click Me</button>
<script>
function doSomething() {
alert("Button clicked!");
}
</script>
4.1. logoutSuccessUrl()
在註銷流程成功執行後,Spring Security 將會將用户重定向到指定的頁面。默認情況下,該頁面是根頁面 (/),但可以配置更改。
//...
.logout()
.logoutSuccessUrl("/afterlogout.html")
//...這也可以使用 XML 配置來實現:
<logout logout-success-url="/afterlogout.html" />根據應用程序的不同,一個好的做法是將其用户重定向回登錄頁面:
//...
.logout()
.logoutSuccessUrl("/login.html")
//...4.2. logoutUrl()
類似於 Spring Security 中的其他默認值,觸發註銷機制的 URL 也具有默認值,即 /logout。
然而,為了確保沒有關於應用程序使用的框架的信息被公開,最好更改此默認值。
.logout()
.logoutUrl("/perform_logout")通過 XML:
<logout
logout-success-url="/anonymous.html"
logout-url="/perform_logout" />4.3. invalidateHttpSession 和 deleteCookies
這些高級屬性控制會話失效以及在用户註銷時要刪除的 Cookie 列表。 因此,invalidateHttpSession 允許會話在註銷時不會被失效(默認值為 <em>true</em>)。
deleteCookies 方法也很簡單:
.logout()
.logoutUrl("/perform_logout")
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")以及XML版本:
<logout
logout-success-url="/anonymous.html"
logout-url="/perform_logout"
delete-cookies="JSESSIONID" />4.4. <em>logoutSuccessHandler()</em>
對於更復雜的場景,當命名空間不夠靈活時,可以替換 Spring Context 中的 <em>LogoutSuccessHandler</em> Bean 為自定義引用:
@Bean
public LogoutSuccessHandler logoutSuccessHandler() {
return new CustomLogoutSuccessHandler();
}
//...
.logout()
.logoutSuccessHandler(logoutSuccessHandler());
//...其等效的XML配置如下:
<logout
logout-url="/perform_logout"
delete-cookies="JSESSIONID"
success-handler-ref="customLogoutSuccessHandler" />
...
<beans:bean name="customUrlLogoutSuccessHandler" />任何需要在用户成功登出時運行的自定義應用程序邏輯,都可以通過自定義登出成功處理程序來實現。例如,一個簡單的審計機制,記錄用户觸發登出時最後訪問的頁面:
public class CustomLogoutSuccessHandler extends
SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {
@Autowired
private AuditService auditService;
@Override
public void onLogoutSuccess(
HttpServletRequest request,
HttpServletResponse response,
Authentication authentication)
throws IOException, ServletException {
String refererUrl = request.getHeader("Referer");
auditService.track("Logout from: " + refererUrl);
super.onLogoutSuccess(request, response, authentication);
}
}此外,請務必記住,這個自定義 Bean 的職責是確定用户註銷後將用户引導到哪個目的地。因此,將 logoutSuccessHandler 屬性與 logoutSuccessUrl 屬性配對將無法正常工作,因為兩者都覆蓋了類似的功能。
5. 結論
在本示例中,我們首先設置了一個簡單的退出登錄樣本,並隨後討論了 Spring Security 中可用的更高級選項。
當項目在本地運行時,樣本 HTML 可在以下地址訪問: