[英]Logout doesn't work with Spring Boot, Spring Security and Thymleaf
我正在嘗試為我們的 Spring 引導應用程序添加注銷 function 但 Spring 注銷不起作用...經過身份驗證的用戶仍然存在並且它沒有清除安全上下文。 真正讓我吃驚的是,它有時有效,有時無效。 我不知道為什么!
Thymeleaf:
<li><a class="dropdown-item" href="javascript: document.logoutForm.submit()" role="menuitem"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
<form name="logoutForm" th:action="@{/logout}" method="post" th:hidden="true">
<input hidden type="submit" value="Sign Out"/>
</form>
這是我的注銷代碼塊;
.logout()
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/oauth2/authorization/login")
.addLogoutHandler(new HeaderWriterLogoutHandler(
new ClearSiteDataHeaderWriter(
ClearSiteDataHeaderWriter.Directive.CACHE,
ClearSiteDataHeaderWriter.Directive.COOKIES,
ClearSiteDataHeaderWriter.Directive.STORAGE)));
我也嘗試過,但沒有幫助;
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
有誰知道為什么它有這種奇怪的行為?
讓我假設問題是 oauth2 登錄/注銷配置的配置不完整:
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.logout()
.logoutSuccessHandler(logoutSuccessHandler())
.invalidateHttpSession(true)
.clearAuthentication(true)
.deleteCookies("JSESSIONID")
.and()
.oauth2Login();
}
private LogoutSuccessHandler logoutSuccessHandler() {
...
}
}
注意兩個時刻:
.oauth2Login()
- 使用 OAuth 2.0 配置身份驗證支持.logoutSuccessHandler(...)
- 為結束 session 的用戶重定向到正確的 oauth2 服務器注銷端點。 不幸的是,我不知道您的 oauth2 提供商,無論如何我建議查看OidcClientInitiatedLogoutSuccessHandler
以了解如何重定向到注銷 url。 如果您不使用openid
oauth2 提供程序,您可以基於SimpleUrlLogoutSuccessHandler
實現自己的注銷處理程序,您應該只知道注銷 url 和 url 參數,用於在 oauth2 服務器上成功注銷后重定向。我有類似的問題。 現在已經解決了。 我使用了以下代碼:
.and().logout().logoutSuccessUrl("/logoutSuccess").deleteCookies("JSESSIONID").invalidateHttpSession(true);
試試這個。 它正在我的機器上工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.