gpt4 book ai didi

java - Spring 安全: can't clear authentication object

转载 作者:行者123 更新时间:2023-12-02 08:52:48 25 4
gpt4 key购买 nike

尝试注销,有一个 Controller 和一个 .jsp:

@Controller
public class LogoutController {
@RequestMapping(value = {"/logout"}, method = RequestMethod.GET)
public String logoutDo(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 "redirect:/";
}
}
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

<html>

<head>
<title>Play the game!</title>

<link type="text/css"
rel="stylesheet"
href="/static/css/index.css">
</head>

<body>

<security:authorize access="isAuthenticated()">
authenticated as <security:authentication property="principal.username" />
</security:authorize>

<a href="/logout">Logout</a>
...

当我点击Logout时,它会重定向回/页面,但principal.username(用户)仍然显示。请帮忙....

最佳答案

也尝试取消当前身份验证。

    HttpSession session = request.getSession(false);
if (session != null) {
logger.debug("Invalidating session: " + session.getId());
session.invalidate();
}
}

SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(null);

SecurityContextHolder.clearContext();

关于java - Spring 安全: can't clear authentication object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60675343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com