-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from JinnJarBurger/feature/issue-22-custom-jwt…
…-error-message Add custom JWT error messages for authentication failures
- Loading branch information
Showing
2 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/dev/nmarulo/depensaapp/security/JwtAuthenticationEntryPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dev.nmarulo.depensaapp.security; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import dev.nmarulo.depensaapp.commons.dtos.ErrorRes; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException; | ||
import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationEntryPoint; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
private final AuthenticationEntryPoint delegate = new BearerTokenAuthenticationEntryPoint(); | ||
|
||
private final ObjectMapper mapper; | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, | ||
HttpServletResponse response, | ||
AuthenticationException authException) throws IOException, ServletException { | ||
|
||
this.delegate.commence(request, response, authException); | ||
|
||
if (authException instanceof InvalidBearerTokenException bearerTokenException) { | ||
var problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.UNAUTHORIZED, | ||
bearerTokenException.getMessage()); | ||
|
||
problemDetail.setTitle("Invalid Token"); | ||
|
||
response.setContentType("application/json"); | ||
|
||
mapper.writeValue(response.getWriter(), new ErrorRes(problemDetail)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters