Skip to content

Commit

Permalink
Merge pull request #42 from JinnJarBurger/feature/issue-22-custom-jwt…
Browse files Browse the repository at this point in the history
…-error-message

Add custom JWT error messages for authentication failures
  • Loading branch information
nmarulo authored Oct 27, 2024
2 parents d44ca98 + 23b6b26 commit 71c7b93
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
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));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.nmarulo.depensaapp.configuration;
package dev.nmarulo.depensaapp.security;

import com.nimbusds.jose.KeyLengthException;
import com.nimbusds.jose.crypto.MACSigner;
Expand All @@ -8,6 +8,7 @@
import dev.nmarulo.depensaapp.app.users.UserRepository;
import dev.nmarulo.depensaapp.commons.component.LocalMessage;
import dev.nmarulo.depensaapp.commons.converter.CustomJwtAuthenticationConverter;
import dev.nmarulo.depensaapp.configuration.AppProperties;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class WebSecurityConfig {

private final LocalMessage localeMessage;

private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.securityMatcher(appProperties.getPathPrefix() + "/**")
Expand Down Expand Up @@ -105,7 +108,8 @@ public JwtDecoder jwtDecoder() throws KeyLengthException {
private Customizer<OAuth2ResourceServerConfigurer<HttpSecurity>> configOAuth2() {
final var converter = new CustomJwtAuthenticationConverter(this.userRepository, this.localeMessage);

return oauth -> oauth.jwt(jwtConfigurer -> jwtConfigurer.jwtAuthenticationConverter(converter));
return oauth -> oauth.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.jwt(jwtConfigurer -> jwtConfigurer.jwtAuthenticationConverter(converter));
}

}

0 comments on commit 71c7b93

Please sign in to comment.