Class SecurityFilter
- 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
The filter checks for token validity only if the application is configured to perform
advanced filtering (as indicated by the AdvancedFilterChecker). If advanced filtering
is enabled, it processes the token, validates it using the JwtService, retrieves the
associated user from the repository, and sets the authenticated user in the SecurityContextHolder.
-
Field Summary
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX -
Constructor Summary
ConstructorsConstructorDescriptionSecurityFilter(JwtService jwtService, org.springframework.context.ApplicationContext applicationContext, SecurityUtil securityUtil) Constructs a newSecurityFilter. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) Filters HTTP requests by validating JWT tokens and setting the authentication context.voidsetUserRepository(Class<?> userRepositoryClass) Sets the user repository class to be used for user lookups during authentication.protected booleanshouldNotFilter(jakarta.servlet.http.HttpServletRequest request) Determines if this filter should be applied to the given HTTP request.Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, 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
-
SecurityFilter
public SecurityFilter(JwtService jwtService, org.springframework.context.ApplicationContext applicationContext, SecurityUtil securityUtil) Constructs a newSecurityFilter.This constructor initializes the filter with the required dependencies:
JwtServicefor validating JWT tokens.ApplicationContextfor accessing Spring beans, such as the user repository.SecurityUtilfor handling user lookups and authority retrieval.
- Parameters:
jwtService- the JWT service used to validate and decode tokens.applicationContext- the Spring application context to fetch the user repository bean.securityUtil- utility class for performing security-related operations like user lookup and authority retrieval.
-
-
Method Details
-
setUserRepository
Sets the user repository class to be used for user lookups during authentication. The repository bean is retrieved from the Spring application context.- Parameters:
userRepositoryClass- the class type of the user repository.
-
doFilterInternal
protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException Filters HTTP requests by validating JWT tokens and setting the authentication context.If the token is valid, it retrieves the associated user from the repository and sets the user as the authenticated principal in the security context.
If the token is missing or invalid, the filter sends an appropriate error response with the status:
- 401 Unauthorized if the token is missing or invalid.
- 403 Forbidden if the user cannot be found or the token is invalid.
- Specified by:
doFilterInternalin classorg.springframework.web.filter.OncePerRequestFilter- Parameters:
request- the HTTP request to be filtered.response- the HTTP response to be sent back to the client.filterChain- the filter chain to pass the request and response to the next filter.- Throws:
jakarta.servlet.ServletException- if an error occurs during filter processing.IOException- if an I/O error occurs during the filter process.
-
shouldNotFilter
protected boolean shouldNotFilter(jakarta.servlet.http.HttpServletRequest request) Determines if this filter should be applied to the given HTTP request.This method checks if the request path is in the list of paths that should not be filtered.
- Overrides:
shouldNotFilterin classorg.springframework.web.filter.OncePerRequestFilter- Parameters:
request- the HTTP request.- Returns:
trueif the request should not be filtered;falseotherwise.
-