You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
18 Hrs
12 Min
14 Seg

Crear filtro para verificar el JWT

20/23
Resources

How to validate a JSON Web Token (JWT) with Spring Security?

Security is a critical aspect in applications that handle sensitive data and critical operations. One of the most widely used security technologies in microservices is the JSON Web Token (JWT). In this article, we will explore how to implement a security filter that validates these tokens using Spring Security. Our focus will be on implementing a custom filter that will evaluate requests and authenticate requests securely.

How to create a custom filter in Spring Security?

Creating a custom filter that validates JWTs requires a series of methodical steps. Within our project, a strategic plan must be followed for the filter to be integrated correctly:

  1. Creation of the JWTFilter class:

    • The JWTFilter class must be housed within the config package and must be annotated with @Component. This annotation is essential for Spring to discover and inject this class during its dependency lifecycle.
  2. Extend the class to catch the requests:

    • It must be extended from a Spring-specific class, the OncePerRequestFilter. This ensures that the doFilterInternal method is executed with each incoming HTTP request, allowing the capture and evaluation of such requests.
  3. Override the doFilterInternal method:

    • This method is the core where authentication will be handled. It receives parameters such as HttpServletRequest, HttpServletResponse and FilterChain.

What are the steps to validate a JWT?

Validating a JWT involves following a structured set of steps to ensure its authenticity and its relationship to a valid user.

  1. Validate the authorization header:

    • Retrieve the header with request.getHeader(HttpHeaders.AUTHORIZATION).
    • Check if it is present, not null or empty, and if it starts with the string "Bearer".
    • If any of these checks fail, the request should be left to process through the rest of the filter chain without further action.
  2. Validate the JWT:

    • Extract the JWT from the header.
    • Use the isValid method of the JWTUtil class to check its validity. If the token is invalid, the filter will continue without marking the user as authenticated.
  3. Load the user from the UserDetailService:

    • Get the username from the valid token.
    • Look up this username in the UserDetailService, which retrieves the user's details from a database.
  4. Authenticate and upload to the security context:

    • Create a UsernamePasswordAuthenticationToken with the username, password and authorities.
    • Use the SecurityContextHolder to set this token as the current authentication.

How to implement the user and security context?

Proper implementation of the user and security context is essential to allow other parts of the application to handle authentication in a uniform way. Here are some critical steps:

  • Dependency injection:inject necessary instances such as JWTUtil and UserDetailService with @Autowired to manage them through Spring.
@Autowiredprivate JWTUtil jwtUtil;
 @Autowiredprivate UserDetailsService userDetailsService;
  • Loading the context:use SecurityContextHolder.getContext().setAuthentication(authenticationToken) to update the security context once the user has passed all validations.

What's next after implementing this filter?

Once this security filter is implemented, the next step is to include it in the Spring Security filter chain, ensuring that all requests to secure resources pass through this filter. This ensures that only valid JWTs and authenticated users can access protected application operations. It is always key to review the official Spring Security documentation to maintain best practices and security updates.

Getting into the details, handling exceptions, and a clear understanding of the Spring Security configuration are critical to a successful implementation. Don't miss the opportunity to continue exploring new features and always stay on top of security best practices.

Contributions 4

Questions 2

Sort by:

Want to see more contributions, questions and answers from the community?

que curso mas genial y practico ! y eso que he visto otros en spring security, eso de mencionar cosas porque se implementa por ejemplo el UserDetailsService son detalles que en otros materiales no existen y que en serio ayudan mucho a entender el porque.

Si empieza por "Bearer " entonces no está vacío, entonces no hace falta comprobar si está vacío

En vez del split se me ocurrió como alternativa usar :

String jwt = authHeader.replace("Bearer ", "");

De esa manera no generamos un Array que es una estructura que requiere mas memoria.