In my test application, filter authentication is configured. When you first start the program, you need to enter twice the login and password, because the first time it cannot authorize, it displays an error about incorrect login / password. Here is my post method from AuthServlet:
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String login = req.getParameter("login"); String password = req.getParameter("password"); if (login.equals("admin") && password.equals("admin")) { HttpSession admSession = req.getSession(); admSession.setAttribute("login", "admin"); req.setAttribute("role", Role.ADMIN); resp.sendRedirect(String.format("%s/users", req.getContextPath())); } else { if (ValidateService.getInstance().isValidLogPass(login, password)) { HttpSession session = req.getSession(); session.setAttribute("login", login); User user = DBStore.getInstance().findByLogin(login); session.setAttribute("role", user.getRole()); session.setAttribute("user", user); resp.sendRedirect(String.format("%s/users", req.getContextPath())); } else { req.setAttribute("error", "Invalid credentials"); doGet(req, resp); } } }