Reputation: 197
I have a project on my work and i log-in as a tomcat user but i don't know how to log out, i try deactivate tomcat session, we use java spring and here is what i try to do from the controller:
@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");
}
but when i redirected in home page i don't prompted for username or password. Does anyone now how i can do this?
Upvotes: 1
Views: 1379
Reputation: 197
I got the answer for the above question. I had seen a lot of answers on internet but all were the same. Finally from a guy i realize that you have to send wrong verification data to server (username and password), so that guy have write this:
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;
}
Use the above javascript function, create a link (Log out) which calls this function:
<a href='javascript:logout()'>logout</a>
Or as the first method in a logout page.
(P.S. sorry but i don't remember the site url)
Upvotes: 1