From 422ac88b736cd4e016f0f5860127a957606c7baa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marten=20Prie=C3=9F?= Date: Wed, 5 Sep 2018 21:39:00 +0200 Subject: [PATCH] added exception handler --- .../filter/JwtAuthenticationTokenFilter.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/commons-auth-core/src/main/java/io/rocketbase/commons/filter/JwtAuthenticationTokenFilter.java b/commons-auth-core/src/main/java/io/rocketbase/commons/filter/JwtAuthenticationTokenFilter.java index 92379f4e..87cc76ec 100644 --- a/commons-auth-core/src/main/java/io/rocketbase/commons/filter/JwtAuthenticationTokenFilter.java +++ b/commons-auth-core/src/main/java/io/rocketbase/commons/filter/JwtAuthenticationTokenFilter.java @@ -4,11 +4,13 @@ import io.jsonwebtoken.JwtException; import io.jsonwebtoken.MalformedJwtException; import io.rocketbase.commons.config.JwtProperties; +import io.rocketbase.commons.dto.ErrorResponse; import io.rocketbase.commons.model.AppUser; import io.rocketbase.commons.security.CustomAuthoritiesProvider; import io.rocketbase.commons.security.JwtTokenService; import io.rocketbase.commons.service.AppUserService; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; @@ -44,9 +46,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse String authToken = getAuthToken(request); String username = getValidatedUsername(authToken); - tryToAuthenticate(authToken, username, request); - - chain.doFilter(request, response); + try { + tryToAuthenticate(authToken, username, request); + chain.doFilter(request, response); + } catch (Exception e) { + int status = HttpStatus.BAD_REQUEST.value(); + response.setStatus(status); + response.getWriter().write(String.format("{\"status\": %d, \"message\": \"%s\"}", status, + e.getMessage().replace("\"", "\\"))); + } } protected String getAuthToken(HttpServletRequest request) {