0

I have mvc spring app with logout controller, I want to remove some cookies when I send act param via get method. But cookies are still alive and no errors/warnings, I trying a lot of things, but no result. Maybe anybody faced with a same problems?

EDIT: I think It's not dublicate, because this code works fine without spring mvc. SetContentType text/html doesn't solve a problem.

@RequestMapping(value = {"/{act}", "/{act}/"}, method = RequestMethod.GET)
public void getLogout(@PathVariable(value = "act", required = true) String act,
                         ModelMap model,
                        HttpServletRequest request,
                        HttpServletResponse response,
                        HttpSession session) throws IOException, 

ServletException {

if(act.equals("logout")) {

    CacheControl.maxAge(0, TimeUnit.SECONDS);

    Cookie login_auth;

    Cookie[] cookies = request.getCookies();

    if(cookies != null){
        for(Cookie cookie : cookies){

            if(cookie.getName().equals("login_auth")) {
                login_auth = cookie;
                login_auth.setMaxAge(0);

                response.addCookie(login_auth);

               response.sendRedirect(String.format("%s%s", request.getContextPath(), "/login"));
            }

        }
    }

}

}

So, after a lot of tests I see that for /admin/logout was set another cookie with a same name and correct MaxAge = 0, It's not expectable behaviour.

I solved It when change /admin/logout mapping on simple /admin?act=logout

Nesquik27
  • 234
  • 1
  • 7
  • 18
  • Possible duplicate of [How do you remove a Cookie in a Java Servlet](https://stackoverflow.com/questions/890935/how-do-you-remove-a-cookie-in-a-java-servlet) – Ori Marko Aug 10 '17 at 15:29
  • 1
    @Nesquik27 While you are thinking you've solved your problem let me inform you that using a GET request to implement logout functionality is not a good programming practice and should be avoided. Use POST instead. This link may interest you. https://stackoverflow.com/questions/3521290/logout-get-or-post – Perry Aug 10 '17 at 20:16

0 Answers0