Class FiltroJwt

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
com.gestorrh.api.security.FiltroJwt
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 FiltroJwt extends org.springframework.web.filter.OncePerRequestFilter
Componente de filtrado de seguridad para la interceptación de peticiones HTTP.

Este filtro se ejecuta una vez por cada solicitud (extiende de OncePerRequestFilter) para validar la presencia y validez de un token JWT en el encabezado Authorization. Si el token es válido, establece la autenticación del usuario en el SecurityContextHolder.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final ServicioJwt
    Servicio para la gestión de operaciones relacionadas con JWT.

    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
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    doFilterInternal(jakarta.servlet.http.HttpServletRequest peticion, jakarta.servlet.http.HttpServletResponse respuesta, jakarta.servlet.FilterChain cadenaFiltros)
    Realiza el filtrado interno de la petición para gestionar la seguridad basada en JWT.

    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
  • Field Details

    • servicioJwt

      private final ServicioJwt servicioJwt
      Servicio para la gestión de operaciones relacionadas con JWT.
  • Constructor Details

    • FiltroJwt

      public FiltroJwt()
  • Method Details

    • doFilterInternal

      protected void doFilterInternal(@NonNull jakarta.servlet.http.HttpServletRequest peticion, @NonNull jakarta.servlet.http.HttpServletResponse respuesta, @NonNull jakarta.servlet.FilterChain cadenaFiltros) throws jakarta.servlet.ServletException, IOException
      Realiza el filtrado interno de la petición para gestionar la seguridad basada en JWT.

      El proceso sigue estos pasos: 1. Verifica si existe un encabezado 'Authorization' con el prefijo 'Bearer '. 2. Extrae el token JWT del encabezado. 3. Valida el token y extrae el correo del usuario y su rol. 4. Si el usuario no está ya autenticado, establece el contexto de seguridad con las autoridades correspondientes. 5. Continúa con la cadena de filtros.

      Specified by:
      doFilterInternal in class org.springframework.web.filter.OncePerRequestFilter
      Parameters:
      peticion - La solicitud HTTP recibida.
      respuesta - La respuesta HTTP que se enviará.
      cadenaFiltros - La cadena de filtros de seguridad.
      Throws:
      jakarta.servlet.ServletException - Si ocurre un error en el procesamiento del servlet.
      IOException - Si ocurre un error de entrada/salida.