停用tomcat会话

时间:2014-03-05 12:13:15

标签: tomcat logout

我有一个项目在我的工作,我作为一个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");
}

但是当我在主页中重定向时,我没有提示输入用户名或密码。现在有人怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我得到了上述问题的答案。我在互联网上看到了很多答案,但都是一样的。最后从一个人我意识到你必须向服务器发送错误的验证数据(用户名和密码),所以那家伙写了这个:

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函数,创建一个调用此函数的链接(Log out):

<a href='javascript:logout()'>logout</a>

或者作为退出页面中的第一个方法。

(P.S。抱歉,但我不记得网站的网址)