Class JwtAuthenticationEntryPoint
- All Implemented Interfaces:
org.springframework.security.web.AuthenticationEntryPoint
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
ConstructorsConstructorDescriptionConstructs a new JWT authentication entry point. -
Method Summary
Modifier and TypeMethodDescriptionvoidcommence(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.
-
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
AuthenticationExceptionis 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:
commencein interfaceorg.springframework.security.web.AuthenticationEntryPoint- Parameters:
request- The HTTP request that resulted in an authentication exceptionresponse- The HTTP response to be modified with error detailsauthException- The authentication exception that triggered this method- Throws:
IOException- If an I/O error occurs while writing the responsejakarta.servlet.ServletException- If a servlet-related error occurs
-