我的工作中有一个项目,我以tomcat用户身份登录,但我不知道如何注销,我尝试停用tomcat会话,我们使用java spring,这是我尝试从控制器执行的操作:
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public ModelAndView logoutAction(HttpSession session, ServletRequest request, ServletResponse response, FilterChain chain) {
logger.info("Start HomeController.logoutAction");
session.invalidate();
request.getSession(true);
logger.info("End HomeController.logoutAction");
return new ModelAndView("home");
}
但是当我在首页中重定向时,我不会提示输入用户名或密码。现在有人能做到这一点吗?
最佳答案
我得到了上述问题的答案。我在互联网上看到了很多答案,但都一样。最后,从一个家伙那里我意识到您必须向服务器发送错误的验证数据(用户名和密码),所以那个家伙写了这样的代码:
function logout(safeLocation){
var mainWindow = window;
invalidateCurentSession();
var outcome, u, m = "You should be logged out now.";
// IE has a simple solution for it - API:
try { outcome = document.execCommand("ClearAuthenticationCache") }catch(e){}
// Other browsers need a larger solution - AJAX call with special user name - 'logout'.
if (!outcome) {
// Let's create an xmlhttp object
outcome = (function(x){
if (x) {
// the reason we use "random" value for password is
// that browsers cache requests. changing
// password effectively behaves like cache-busing.
x.open("HEAD", safeLocation || location.href, true, "logout", "wrongUser");
x.send("");
// x.abort()
return 1;// this is **speculative** "We are done."
} else {
return;
}
})(mainWindow.XMLHttpRequest ? new mainWindow.XMLHttpRequest() : console.log("active"))
}
if (!outcome) {
m = "Your browser is too old or too weird to support log out functionality. Close all windows and restart the browser."
}
window.location = "some url";
return outcome;
}
使用上面的javascript函数,创建一个调用此函数的链接(注销):
<a href='javascript:logout()'>logout</a>
或作为注销页面中的第一种方法。
(P.S.对不起,但我不记得该网站的网址了)