[英]Spring LogOut function Not logging out
我正在嘗試編寫一個注銷 function 將用戶返回到主頁(“/”)但是當我按下按鈕時它什么也沒做。 我一直在關注教程,但它們似乎都有同樣的問題,它對我不起作用。
安全配置代碼
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login")
.permitAll()
.antMatchers("/admin")
.hasAnyRole("ADMIN", "USER")
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/admin")
.failureUrl("/login?error=true")
.permitAll()
.and()
.logout().logoutUrl("/login")
.permitAll()
.and()
.csrf()
.disable()
;
return;
}
Controller
@RequestMapping(value="/logout", method = RequestMethod.GET)
public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
new SecurityContextLogoutHandler().logout(request, response, auth);
}
return "redirect:/login?logout";
}
最后存儲 JSP Logout
<body>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
</body>
任何幫助將不勝感激。 以及任何其他提示,因為這是我在這里的第一篇文章:)
注銷()配置:
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/logout.done")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
或以編程方式在 controller 中:
@RequestMapping(value="/logout", method = RequestMethod.GET)
public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
HttpSession session= request.getSession(false);
SecurityContextHolder.clearContext();
session= request.getSession(false);
if(session != null) {
session.invalidate();
}
for(Cookie cookie : request.getCookies()) {
cookie.setMaxAge(0);
}
return "logout";
選擇其中一種方式,如果您已創建自定義路由,則不應添加.logout() 配置
在 jsp 上:
<a href="${pageContext.request.contextPath}/logout?${_csrf.parameterName}=${_csrf.token}">Logout</a>
像這樣修改您的 SecurityConfig:
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.permitAll()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.