When a user is authorized, cookies are created: There is a main page - index.jsp There is a home page, which get after authorization - home.jsp There is a Filter, which is tied to home.jsp. Authorization takes place in the filter (the class connects to the database) and if everything is successful, then cookies are created and then everything goes to home.jsp. In home.jsp at the very beginning there is a check for cookies and if they are, then the user remains on the page, if not, I do Redirect to index.jsp.

The essence of the problem is that after exiting the Filter there are no cookies. After it, there is a check in home.jsp and, accordingly, it redirects to index and there already cookies appear and it redirects to home. It’s the same if you do it all through Servlen

The question is how to treat this? I know the option to add cookies already on the home page itself.

Code to add to cookies:

Cookie cookie = new Cookie("login", Login); cookie.setMaxAge(CookiesTime); res.addCookie(cookie); 

Check for cookies in home.jsp

 <% String login = ""; String hash = ""; Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { if (cookie.getName().equals("badAuth")) { System.err.println("home.jsp -> Found badAuth!"); response.sendRedirect("index"); return; } else { if (cookie.getName().equals("login")) { System.out.println("home.jsp -> Found login"); login = cookie.getValue(); } if (cookie.getName().equals("hash")) { System.out.println("home.jsp -> Found hash"); hash = cookie.getValue(); } } } dbConnect connect = new dbConnect(); if (!login.equals("") && !hash.equals("")) { if (connect.checkHash(login, hash)) { int roleNum = connect.checkUserRole(login, dbConnect.ROLE_USER); if(roleNum == 1 || roleNum == 2) { request.setAttribute("role", roleNum); ArrayList<Link> linkArr = connect.GetLinks(connect.getId(login)); request.setAttribute("linkArr", linkArr); } else { response.sendRedirect("index"); return; } } } else { response.sendRedirect("index"); return; } %> <!DOCTYPE html> html страница... 
  • Give a piece of the source code of the program, where the presence of cookies is checked. In general, it is better to redirect if there are no cookies in the filter, and not in home.jsp - Sergey
  • And what about the other filters? which is in the chain .. throw them away or what? redirect in the filter is not entirely correct, but possible. Updated the first post, does not fit here. - Tsyklop
  • res.addCookie(cookie); - How do I understand the addition of cookies in response? And cookies = request.getCookies(); get a cookie from a request? Filter adds in response. jsp searches in the request. And does not find, because the request was without cookies, and the filter put it in response. Make it so that the filter also res.addCookie(...); req.addCookie(...); it into the query (empty, everyone thinks that it was always there) res.addCookie(...); req.addCookie(...); res.addCookie(...); req.addCookie(...); - Sergey
  • What is the point in other filters if authorization fails? - Sergey
  • @Sergey req has no method for adding cookies. Songs miraculously appear when redirecting from home to index, tobish at index, we already have cookies, those that were added to the filter. By and large, it makes no sense, but to break all this is also not good. - Tsyklop

0