Class AppAuthenticationFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
com.app.security.filter.AppAuthenticationFilter
All Implemented Interfaces:
jakarta.servlet.Filter, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.EnvironmentAware, org.springframework.context.MessageSourceAware, org.springframework.core.env.EnvironmentCapable, org.springframework.web.context.ServletContextAware

public class AppAuthenticationFilter extends org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
Custom filter to handle user authentication during login.

This filter processes the login request, extracts user credentials (username and password), authenticates the user using the provided AuthenticationManager, and generates authentication tokens upon successful login. It also sets the generated tokens in cookies and returns them in the response body.

  • Field Summary

    Fields inherited from class org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

    SPRING_SECURITY_FORM_PASSWORD_KEY, SPRING_SECURITY_FORM_USERNAME_KEY

    Fields inherited from class org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter

    authenticationDetailsSource, eventPublisher, messages

    Fields inherited from class org.springframework.web.filter.GenericFilterBean

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    AppAuthenticationFilter(TokenService tokenService, org.springframework.security.authentication.AuthenticationManager authenticationManager)
    Constructs a new AppAuthenticationFilter with the specified TokenService and AuthenticationManager.
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.security.core.Authentication
    attemptAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Attempts to authenticate the user based on the provided credentials.
    protected void
    successfulAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain, org.springframework.security.core.Authentication authResult)
    Handles successful authentication by generating and setting authentication tokens.

    Methods inherited from class org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

    getPasswordParameter, getUsernameParameter, obtainPassword, obtainUsername, setDetails, setPasswordParameter, setPostOnly, setUsernameParameter

    Methods inherited from class org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter

    afterPropertiesSet, doFilter, getAllowSessionCreation, getAuthenticationManager, getFailureHandler, getRememberMeServices, getSuccessHandler, requiresAuthentication, setAllowSessionCreation, setApplicationEventPublisher, setAuthenticationDetailsSource, setAuthenticationFailureHandler, setAuthenticationManager, setAuthenticationSuccessHandler, setContinueChainBeforeSuccessfulAuthentication, setFilterProcessesUrl, setMessageSource, setRememberMeServices, setRequiresAuthenticationRequestMatcher, setSecurityContextHolderStrategy, setSecurityContextRepository, setSessionAuthenticationStrategy, unsuccessfulAuthentication

    Methods inherited from class org.springframework.web.filter.GenericFilterBean

    addRequiredProperty, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext

    Methods inherited from class java.lang.Object

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

    • AppAuthenticationFilter

      public AppAuthenticationFilter(TokenService tokenService, org.springframework.security.authentication.AuthenticationManager authenticationManager)
      Constructs a new AppAuthenticationFilter with the specified TokenService and AuthenticationManager.
      Parameters:
      tokenService - the service responsible for generating tokens
      authenticationManager - the authentication manager used to authenticate the user
  • Method Details

    • attemptAuthentication

      public org.springframework.security.core.Authentication attemptAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws org.springframework.security.core.AuthenticationException
      Attempts to authenticate the user based on the provided credentials.

      The method reads the AuthenticationDto from the request body, which contains the username and password. It then creates a UsernamePasswordAuthenticationToken and delegates the authentication to the AuthenticationManager.

      Overrides:
      attemptAuthentication in class org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
      Parameters:
      request - the HTTP request containing the user's credentials
      response - the HTTP response
      Returns:
      the authenticated Authentication object
      Throws:
      org.springframework.security.core.AuthenticationException - if authentication fails
    • successfulAuthentication

      protected void successfulAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain, org.springframework.security.core.Authentication authResult) throws IOException, jakarta.servlet.ServletException
      Handles successful authentication by generating and setting authentication tokens.

      Upon successful authentication, this method generates the access and refresh tokens using the TokenService. The tokens are then added as HTTP-only cookies and returned in the response body in JSON format.

      Overrides:
      successfulAuthentication in class org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter
      Parameters:
      request - the HTTP request
      response - the HTTP response
      chain - the filter chain
      authResult - the authentication result
      Throws:
      IOException - if an I/O error occurs
      jakarta.servlet.ServletException - if a servlet exception occurs