今天聊一聊spring security登出相關(guān)的話題浑劳。相對于登陸象迎,登出功能的使用和配置就相對簡單一些凡橱。
項目準備
我在上一篇文章,搭建了一個很基礎(chǔ)的環(huán)境台汇。里面有一個受保護的restful api苛骨,然后就是一些登陸的配置。今天我們接著這個項目來說一說這個logout功能励七。
開工
首先我們項目里面現(xiàn)在有一個登陸頁面/demo-login.html
智袭,我們現(xiàn)在需要寫一個首頁,至少得讓我們的登出功能有個地方放吧掠抬。
我們在\src\main\resources\resources
路徑下新建一個index.html
文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首頁</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<form action="/logout">
<button type="submit">登出</button>
</form>
<button onclick="logout()">登出</button>
</body>
<script>
function logout() {
$.ajax('/logout', {
method: 'post',
success() {
console.log('登出成功');
}
});
}
</script>
</html>
頁面之中訪問了一個/logout
接口吼野,這個接口是spring security默認的登出地址,后面我們可以自己去設(shè)置這個地址的两波。
這個文件新建完成之后瞳步,我們啟動項目闷哆,訪問剛新建的這個頁面localhost:8080/index.html
,這里是我項目里面的地址单起,你們自己換成你們自己的地址抱怔。
因為我們還沒有登錄,所以訪問這個index
頁面的時候spring security會把我們引導(dǎo)到我們的登錄頁/demo-login.html
嘀倒,登錄成功之后會轉(zhuǎn)到index
頁面
現(xiàn)在我們點擊第一個登出按鈕頁面會自動跳轉(zhuǎn)到登錄頁面撩匕,登出成功了憔辫。再次登錄到index頁面嘗試點擊一下第二個登出按鈕,控制臺會打印出登出成功,但是頁面沒有反應(yīng)卤妒,我們刷新一下之后會發(fā)現(xiàn)也跳轉(zhuǎn)到登錄頁楣导,說明登出也成功了烦租。
登出這個功能其實spring security默認幫我們配置好了豺鼻,我們現(xiàn)在已經(jīng)嘗試過怎么使用了。現(xiàn)在就遺留一些問題挨约,我們來解決一下味混。
配置登出接口地址
剛才我們在項目中使用的是默認的登出地址/logout
,我們想使用其他的地址可不可以呢诫惭?答案是肯定的翁锡。只需要簡單的配置一下spring security即可。接著上一個項目的配置夕土,我整體的貼一下配置盗誊。
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/demo-login.html")
.loginProcessingUrl("/demo-login")
.and()
//登出的配置
.logout()
.logoutUrl("/demo-logout")
.and()
.authorizeRequests()
.antMatchers("/demo-login.html", "/demo-login").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable();
}
這里需要注意的是csrf防護需要關(guān)閉,如果不關(guān)閉csrf的話登出功能只支持post方法隘弊,且需要帶上csrf token。關(guān)于csrf防護相關(guān)的東西我之后會寫一篇文章專門講一下這個東西荒适。
配置登出成功跳轉(zhuǎn)的地址
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/demo-login.html")
.loginProcessingUrl("/demo-login")
.and()
//登出的配置
.logout()
.logoutUrl("/demo-logout")
.logoutSuccessUrl("/demo-login.html")
.and()
.authorizeRequests()
.antMatchers("/demo-login.html", "/demo-login").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable();
}
配置登出成功后的自定義邏輯
我們可能需要在用戶登出成功之后做一些業(yè)務(wù)操作梨熙,spring security把登出成功之后的邏輯封裝到LogoutSuccessHandler
接口之中。我們首先需要實現(xiàn)這個接口刀诬。
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
System.out.println(String.format("用戶%s成功登出咽扇,恭喜!", ((User)authentication.getPrincipal()).getUsername()));
response.sendRedirect("/demo-login.html");
}
}
在onLogoutSuccess
這個方法里面實現(xiàn)你自己的業(yè)務(wù)邏輯陕壹。
然后再配置一下spring security即可质欲。
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final LogoutSuccessHandlerImpl logoutSuccessHandler;
public SecurityConfig(LogoutSuccessHandlerImpl logoutSuccessHandler) {
this.logoutSuccessHandler = logoutSuccessHandler;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/demo-login.html")
.loginProcessingUrl("/demo-login")
.and()
//登出的配置
.logout()
.logoutUrl("/demo-logout")
.logoutSuccessUrl("/demo-login.html")
.logoutSuccessHandler(logoutSuccessHandler)
.and()
.authorizeRequests()
.antMatchers("/demo-login.html", "/demo-login").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable();
}
}
這樣配置好就可以了。登出成功會進入到你自己定義的方法里面糠馆。
結(jié)束
更多spring security的教程到我主頁上面查看