3 回答

TA貢獻1890條經驗 獲得超9個贊
在SecurityContextLogoutHandler默認情況下添加,所以你并不需要實現自定義注銷它。
如果你想添加其他LogoutHandler你可以在你的配置中做到:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/cont/**").access("hasRole('USER')")
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/login-success", true)
.failureUrl("/failLogin.html")
.permitAll()
.and()
.logout().logoutUrl("/logout-custom")
.logoutSuccessUrl("/login")
.addLogoutHandler(new CustomLogoutHandler())
.permitAll()
.and()
.csrf()
.disable();
}
logoutSuccessUrl("/login")成功注銷后,將用戶重定向到登錄。
更新:另外刪除 th: 并使用純 HTML 可以解決問題。

TA貢獻2037條經驗 獲得超6個贊
自從我使用 spring 和 thymeleaf 已經有一段時間了,但我認為 RedirectView 必須指向一個 url。我自己試過你的例子,它確實沒有用。然而,一些細微的變化使它起作用:
如果您的控制器中尚不存在 url 以登錄:
@GetMapping("/loginForm")
public String loginForm() {
return "loginForm";
}
更改您的重定向視圖:
return new RedirectView("/loginForm");
這是資源結構:
resources
-> templates
-> loginForm.html
-> logout.html

TA貢獻1795條經驗 獲得超7個贊
為什么使用 .logout().logoutUrl("/logout").permitAll()
同時,使用@RequestMapping(value="/logout-custom", method = RequestMethod.POST),它們應該是相同的 URL
或者讓它變得簡單
<form th:action="@{/logout}" method="post">
<button type="submit">Sign Out</button>
</form>
并在安全配置上使用。
.logout().permitAll();
默認情況下,注銷功能已經存在。
添加回答
舉報