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_SUFFIXFields inherited from class org.springframework.web.filter.GenericFilterBean
logger -
Constructor Summary
ConstructorsConstructorDescriptionJwtAuthFilter(JwtUtils jwtUtils, UserDetailsServiceImpl userDetailsService) Costruttore per l'iniezione delle dipendenze necessarie. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoFilterInternal(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, shouldNotFilterErrorDispatchMethods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
-
Constructor Details
-
JwtAuthFilter
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.- Controlla se l'header "Authorization" contiene un token Bearer.
- Estrae l'username (email) dal token.
- Se l'utente non è già autenticato nel contesto, carica i dettagli utente.
- Se il token è valido, crea un oggetto di autenticazione e lo imposta nel contesto di sicurezza.
- Specified by:
doFilterInternalin classorg.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.
-