Class JwtAuthFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
it.unicam.cs.ids2425.FilieraAgricola.config.JwtAuthFilter
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.EnvironmentAware, org.springframework.core.env.EnvironmentCapable, org.springframework.web.context.ServletContextAware

@Component public class JwtAuthFilter extends org.springframework.web.filter.OncePerRequestFilter
Filtro di sicurezza eseguito una volta per ogni richiesta HTTP.

Questo componente intercetta le richieste in arrivo per cercare un token JWT nell'header "Authorization". Se viene trovato un token valido, il filtro estrae l'identità dell'utente e configura il SecurityContextHolder di Spring Security, autenticando l'utente per la richiesta corrente.

  • Field Summary

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

    ALREADY_FILTERED_SUFFIX

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

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    JwtAuthFilter(JwtUtils jwtUtils, UserDetailsServiceImpl userDetailsService)
    Costruttore per l'iniezione delle dipendenze necessarie.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain)
    Esegue la logica di filtro interna.

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

    doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch

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

    addRequiredProperty, afterPropertiesSet, 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

    • JwtAuthFilter

      public JwtAuthFilter(JwtUtils jwtUtils, UserDetailsServiceImpl userDetailsService)
      Costruttore per l'iniezione delle dipendenze necessarie.
      Parameters:
      jwtUtils - Componente per la validazione e il parsing del token JWT.
      userDetailsService - Servizio per il caricamento dei dettagli utente dal database.
  • Method Details

    • doFilterInternal

      protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException
      Esegue la logica di filtro interna.
      1. Controlla se l'header "Authorization" contiene un token Bearer.
      2. Estrae l'username (email) dal token.
      3. Se l'utente non è già autenticato nel contesto, carica i dettagli utente.
      4. Se il token è valido, crea un oggetto di autenticazione e lo imposta nel contesto di sicurezza.
      Specified by:
      doFilterInternal in class org.springframework.web.filter.OncePerRequestFilter
      Parameters:
      request - La richiesta HTTP in arrivo.
      response - La risposta HTTP.
      filterChain - La catena di filtri da proseguire.
      Throws:
      jakarta.servlet.ServletException - In caso di errori servlet.
      IOException - In caso di errori di I/O.