SecurityExceptionView and related work in JwtFilter.

This commit is contained in:
Jesse Brault 2024-08-02 17:46:02 -05:00
parent 83b1f63a56
commit e4bf81d14f
3 changed files with 42 additions and 5 deletions

View File

@ -24,7 +24,7 @@ public class RecipeController {
this.recipeService = recipeService; this.recipeService = recipeService;
} }
@ExceptionHandler @ExceptionHandler(RecipeException.class)
public ResponseEntity<RecipeExceptionView> onRecipeException(RecipeException recipeException) { public ResponseEntity<RecipeExceptionView> onRecipeException(RecipeException recipeException) {
final HttpStatus status = switch (recipeException.getType()) { final HttpStatus status = switch (recipeException.getType()) {
case INVALID_ID, INVALID_USERNAME_OR_SLUG -> HttpStatus.NOT_FOUND; case INVALID_ID, INVALID_USERNAME_OR_SLUG -> HttpStatus.NOT_FOUND;

View File

@ -1,10 +1,14 @@
package app.mealsmadeeasy.api.security; package app.mealsmadeeasy.api.security;
import app.mealsmadeeasy.api.jwt.JwtService; import app.mealsmadeeasy.api.jwt.JwtService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.security.SecurityException;
import jakarta.servlet.FilterChain; import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -21,12 +25,16 @@ import java.io.IOException;
@Component @Component
public final class JwtFilter extends OncePerRequestFilter { public final class JwtFilter extends OncePerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(JwtFilter.class);
private final UserDetailsService userDetailsService; private final UserDetailsService userDetailsService;
private final JwtService jwtService; private final JwtService jwtService;
private final ObjectMapper objectMapper;
public JwtFilter(UserDetailsService userDetailsService, JwtService jwtService) { public JwtFilter(UserDetailsService userDetailsService, JwtService jwtService, ObjectMapper objectMapper) {
this.userDetailsService = userDetailsService; this.userDetailsService = userDetailsService;
this.jwtService = jwtService; this.jwtService = jwtService;
this.objectMapper = objectMapper;
} }
@Override @Override
@ -38,10 +46,18 @@ public final class JwtFilter extends OncePerRequestFilter {
return; return;
} }
if (authorizationHeader.startsWith("Bearer ") if (authorizationHeader.startsWith("Bearer ") && authorizationHeader.length() > 7) {
&& authorizationHeader.length() > 7) {
final String token = authorizationHeader.substring(7); final String token = authorizationHeader.substring(7);
final String username = this.jwtService.getSubject(token); final String username;
try {
username = this.jwtService.getSubject(token);
} catch (SecurityException e) {
logger.error("Error while getting username from token.", e);
final SecurityExceptionView view = new SecurityExceptionView(401, e.getMessage());
response.setStatus(401);
response.getWriter().write(this.objectMapper.writeValueAsString(view));
return;
}
final UserDetails userDetails = this.userDetailsService.loadUserByUsername(username); final UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
final var authenticationToken = new UsernamePasswordAuthenticationToken( final var authenticationToken = new UsernamePasswordAuthenticationToken(
userDetails, userDetails,

View File

@ -0,0 +1,21 @@
package app.mealsmadeeasy.api.security;
public class SecurityExceptionView {
private final int status;
private final String message;
public SecurityExceptionView(int status, String message) {
this.status = status;
this.message = message;
}
public int getStatus() {
return this.status;
}
public String getMessage() {
return this.message;
}
}