Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom JWT error messages for authentication failures #42

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
nmarulo marked this conversation as resolved.
Show resolved Hide resolved
@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 {

nmarulo marked this conversation as resolved.
Show resolved Hide resolved
this.delegate.commence(request, response, authException);
nmarulo marked this conversation as resolved.
Show resolved Hide resolved

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));
}

}