Package filter

Class AuthenticationFilter

java.lang.Object
filter.AuthenticationFilter
All Implemented Interfaces:
javax.servlet.Filter

public class AuthenticationFilter extends Object implements javax.servlet.Filter
Filter that checks Authentication and Authorization Redirects unauthenticated users to login page Enforces role based access control
Author:
TAMIL MUGHILAN
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final Set<String>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Cleans up resources when filter is destroyed.
    void
    doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
    Processes requests to check Authentication and Authorization.
    void
    init(javax.servlet.FilterConfig fConfig)
    Initializes the filter when application starts.
    private boolean
    Checks if the requested path is a static resource.
    private void
    redirectBasedOnRole(String role, javax.servlet.http.HttpServletResponse response, String contextPath)
    Redirects user to appropriate page based on their role.
    private void
    sendAccessDenied(javax.servlet.http.HttpServletResponse response, String message)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • openPaths

      private static final Set<String> openPaths
  • Constructor Details

    • AuthenticationFilter

      public AuthenticationFilter()
  • Method Details

    • doFilter

      public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws IOException, javax.servlet.ServletException
      Processes requests to check Authentication and Authorization. Allows access based on user role and user requests.
      Specified by:
      doFilter in interface javax.servlet.Filter
      Parameters:
      request - the servlet request
      response - the servlet response
      chain - the filter chain
      Throws:
      IOException - if an I/O error occurs
      javax.servlet.ServletException - if a servlet error occurs
    • isStaticResource

      private boolean isStaticResource(String path)
      Checks if the requested path is a static resource.
      Parameters:
      path - the request path to check
      Returns:
      true if it's a static resource, false otherwise
    • redirectBasedOnRole

      private void redirectBasedOnRole(String role, javax.servlet.http.HttpServletResponse response, String contextPath) throws IOException
      Redirects user to appropriate page based on their role.
      Parameters:
      role - the user's role
      response - the HTTP response
      contextPath - the application context path
      Throws:
      IOException - if redirection fails
    • sendAccessDenied

      private void sendAccessDenied(javax.servlet.http.HttpServletResponse response, String message) throws IOException
      Throws:
      IOException
    • init

      public void init(javax.servlet.FilterConfig fConfig)
      Initializes the filter when application starts.
      Specified by:
      init in interface javax.servlet.Filter
      Parameters:
      fConfig - the filter configuration
    • destroy

      public void destroy()
      Cleans up resources when filter is destroyed.
      Specified by:
      destroy in interface javax.servlet.Filter