Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication filter and servlet for login

Tags:

I've a filter used for the login. It performs a textual checking, on fields "Username" and "Password". If and only if the textual checking is correctly done the request goes to the Servlet. This latter performs the control that has to interact with the Database. Is this chain correct?

like image 702
paolo2988 Avatar asked Nov 07 '12 16:11

paolo2988


People also ask

What is an authentication filter?

Authentication filters let you set an authentication scheme for individual controllers or actions. That way, your app can support different authentication mechanisms for different HTTP resources.

What is servlet authentication?

With basic authentication of a servlet, the web browser presents a standard login dialog that is not customizable. When a user submits their name and password, the server determines if the user name and password are those of an authorized user and sends the requested web resource if the user is authorized to view it.

What is servlet filter?

Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes − To intercept requests from a client before they access a resource at back end. To manipulate responses from server before they are sent back to the client.

What is authentication filter in MVC?

ASP.NET MVC filters are used to add extra logic at the different levels of MVC Framework request processing. Authentication Filter runs before any other filter or action method. Authentication confirms if you are a valid or invalid user.


1 Answers

Preface: I gather you're using homegrown login instead of container managed login. For all ways, see How to handle authentication/authorization with users in a database?


The filter (the interceptor) shouldn't check the validity of the username/password combo. That's the responsibility of the servlet (the controller).

The filter should merely check if the user is logged-in or not (usually by just checking the presence of a session attribute) and then continue the request or block it by redirecting back to the login page.

@WebFilter("/*") public class LoginFilter implements Filter {      @Override     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {             HttpServletRequest request = (HttpServletRequest) req;         HttpServletResponse response = (HttpServletResponse) res;         HttpSession session = request.getSession(false);         String loginURI = request.getContextPath() + "/login";          boolean loggedIn = session != null && session.getAttribute("user") != null;         boolean loginRequest = request.getRequestURI().equals(loginURI);          if (loggedIn || loginRequest) {             chain.doFilter(request, response);         } else {             response.sendRedirect(loginURI);         }     }      // ... } 

The servlet should collect the submitted data, find the associated User in database and if found then store it as a session attribute and then redirect to the home page, else redisplay the form with validation errors.

@WebServlet("/login") public class LoginServlet extends HttpServlet {      @EJB     private UserService userService;      @Override     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);     }      @Override     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         String username = request.getParameter("username");         String password = request.getParameter("password");         Map<String, String> messages = new HashMap<String, String>();          if (username == null || username.isEmpty()) {             messages.put("username", "Please enter username");         }          if (password == null || password.isEmpty()) {             messages.put("password", "Please enter password");         }          if (messages.isEmpty()) {             User user = userService.find(username, password);              if (user != null) {                 request.getSession().setAttribute("user", user);                 response.sendRedirect(request.getContextPath() + "/home");                 return;             } else {                 messages.put("login", "Unknown login, please try again");             }           }          request.setAttribute("messages", messages);         request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);     }  } 

See also:

  • Our servlet-filters wiki page
  • Our servlets wiki page
like image 125
BalusC Avatar answered Sep 28 '22 01:09

BalusC