这取决于。如果您的应用程序可以将已注销的用户放置在“您已注销”页面上,那么这可能没问题。但是您不能确定您的用户是否真的会被注销(例如,如果浏览器正在抑制重定向)。
以编程方式,您可以通过以下方式注销:
public void logout(HttpServletRequest request, HttpServletResponse response) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
new SecurityContextLogoutHandler().logout(request, response, auth);
}
SecurityContextHolder.getContext().setAuthentication(null);
}
正如@LateralFractal 指出的那样,如果您没有更改默认值SecurityContextLogoutHandler
(请参阅https://github.com/spring-projects/spring-security/blob/3.2.x/web/src/main/java/org/ springframework/security/web/authentication/logout/SecurityContextLogoutHandler.java#L96)你可以把它减少到
public void logout(HttpServletRequest request) {
new SecurityContextLogoutHandler().logout(request, null, null);
}
甚至(虽然有点丑)
public void logout() {
HttpServletRequest request =
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes())
.getRequest();
new SecurityContextLogoutHandler().logout(request, null, null);
有关获取HttpServletRequest
. _