Class JwtAuthenticationEntryPoint

java.lang.Object
me.josephsf.jobportaljosephsfeir.config.JwtAuthenticationEntryPoint
All Implemented Interfaces:
org.springframework.security.web.AuthenticationEntryPoint

@Component public class JwtAuthenticationEntryPoint extends Object implements org.springframework.security.web.AuthenticationEntryPoint
Authentication entry point for handling unauthorized access attempts in the Job Portal application.

This component implements Spring Security's AuthenticationEntryPoint interface to provide a custom response when an unauthenticated user attempts to access a protected resource. Instead of redirecting to a login page, it returns a JSON response with error details.

When authentication fails or a user attempts to access a secured endpoint without proper authentication, this entry point is triggered to handle the exception and format the response.

Since:
2025-05-14
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new JWT authentication entry point.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    commence(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, org.springframework.security.core.AuthenticationException authException)
    Handles an authentication failure by returning a structured JSON error response.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JwtAuthenticationEntryPoint

      public JwtAuthenticationEntryPoint()
      Constructs a new JWT authentication entry point.

      This default constructor initializes the entry point with no specific configuration. The entry point will format unauthorized errors as JSON responses with HTTP 401 status code.

  • Method Details

    • commence

      public void commence(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, org.springframework.security.core.AuthenticationException authException) throws IOException, jakarta.servlet.ServletException
      Handles an authentication failure by returning a structured JSON error response.

      This method is called by Spring Security when an AuthenticationException is thrown during the authentication process or when an unauthenticated user attempts to access a secured resource. It creates a JSON response containing error details and sends it to the client.

      Specified by:
      commence in interface org.springframework.security.web.AuthenticationEntryPoint
      Parameters:
      request - The HTTP request that resulted in an authentication exception
      response - The HTTP response to be modified with error details
      authException - The authentication exception that triggered this method
      Throws:
      IOException - If an I/O error occurs while writing the response
      jakarta.servlet.ServletException - If a servlet-related error occurs