-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ee881d
commit 65176b2
Showing
10 changed files
with
366 additions
and
20 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -4,10 +4,12 @@ | |
|
||
public enum ErrorCode { | ||
|
||
// user | ||
USER_NOT_FOUND(HttpStatus.BAD_REQUEST, "user is not found"), | ||
|
||
EMAIL_DUPLICATION(HttpStatus.CONFLICT, "Duplicated email exists."), | ||
USERNAME_DUPLICATION(HttpStatus.CONFLICT, "Duplicated username exists."), | ||
|
||
PASSWORD_DISMATCH(HttpStatus.FORBIDDEN, "password is dismatch."), | ||
|
||
INVALID_USERNAME_NULL(HttpStatus.BAD_REQUEST, "username must be provided."), | ||
|
@@ -16,7 +18,13 @@ public enum ErrorCode { | |
INVALID_EMAIL_NULL(HttpStatus.BAD_REQUEST, "address must be provided."), | ||
INVALID_EMAIL_PATTERN(HttpStatus.BAD_REQUEST, "address must be provided by limited pattern like '[email protected]'."), | ||
INVALID_PASSWORD_NULL(HttpStatus.BAD_REQUEST, "password must be provided."), | ||
INVALID_PASSWORD_LENGTH(HttpStatus.BAD_REQUEST, "password length must be between 6 and 20 characters.") | ||
INVALID_PASSWORD_LENGTH(HttpStatus.BAD_REQUEST, "password length must be between 6 and 20 characters."), | ||
|
||
// authentication | ||
INVALID_EXPIRED_JWT(HttpStatus.BAD_REQUEST, "this jwt has expired."), | ||
INVALID_MALFORMED_JWT(HttpStatus.BAD_REQUEST, "this jwt was malformed."), | ||
INVALID_UNSUPPORTED_JWT(HttpStatus.BAD_REQUEST, "this jwt wat not supported."), | ||
INVALID_ILLEGAL_ARGUMENT_JWT(HttpStatus.BAD_REQUEST, "this jwt was wrong."); | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
|
39 changes: 39 additions & 0 deletions
39
src/main/java/com/study/realworld/global/exception/ErrorResponse.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,39 @@ | ||
package com.study.realworld.global.exception; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@JsonTypeName("errors") | ||
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME) | ||
public class ErrorResponse { | ||
|
||
@JsonProperty("body") | ||
private List<String> body; | ||
|
||
protected ErrorResponse() { | ||
} | ||
|
||
private ErrorResponse(List<String> body) { | ||
this.body = body; | ||
} | ||
|
||
public static String toJson(RuntimeException e) throws JsonProcessingException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
return objectMapper.writeValueAsString(new ErrorResponse(List.of(e.getMessage()))); | ||
} | ||
|
||
public static ResponseEntity<ErrorResponse> of(ErrorCode e) { | ||
return ResponseEntity.status(e.getHttpStatus()).body(new ErrorResponse(List.of(e.getMessage()))); | ||
} | ||
|
||
public static ResponseEntity<ErrorResponse> from(Exception e, HttpStatus httpStatus) { | ||
return ResponseEntity.status(httpStatus).body(new ErrorResponse(List.of(e.getMessage()))); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/study/realworld/global/exception/GlobalExceptionHandler.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,42 @@ | ||
package com.study.realworld.global.exception; | ||
|
||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
import static org.springframework.http.HttpStatus.METHOD_NOT_ALLOWED; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException excpetion) { | ||
log.debug("Bad request exception occurred : {}", excpetion.getMessage(), excpetion); | ||
return ErrorResponse.from(excpetion, BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
public ResponseEntity<?> handleMethodNotAllowedException(Exception exception) { | ||
return ErrorResponse.from(exception, METHOD_NOT_ALLOWED); | ||
} | ||
|
||
@ExceptionHandler(BusinessException.class) | ||
public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException exception) { | ||
log.warn("Unexpected service exception occurred: {}", exception.getMessage(), exception); | ||
return ErrorResponse.of(exception.getErrorCode()); | ||
} | ||
|
||
// TODO | ||
// @ExceptionHandler(Exception.class) | ||
// public ResponseEntity<ErrorResponse> handleGlobalException(Exception exception) { | ||
// log.error("Unexpected exception occurred: {}", exception.getMessage(), exception); | ||
// return ErrorResponse.from(exception, BAD_REQUEST); | ||
// } | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/study/realworld/global/exception/JwtException.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,30 @@ | ||
package com.study.realworld.global.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public class JwtException extends RuntimeException { | ||
|
||
private ErrorCode errorCode; | ||
|
||
public JwtException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
|
||
public ErrorCode getErrorCode() { | ||
return errorCode; | ||
} | ||
|
||
public HttpStatus getHttpStatus() { | ||
return errorCode.getHttpStatus(); | ||
} | ||
|
||
public int getHttpStatusValue() { | ||
return getHttpStatus().value(); | ||
} | ||
|
||
public String getMessage() { | ||
return errorCode.getMessage(); | ||
} | ||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/study/realworld/security/JwtAuthenticationEntiyPoint.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,25 @@ | ||
package com.study.realworld.security; | ||
|
||
import com.study.realworld.global.exception.ErrorResponse; | ||
import java.io.IOException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class JwtAuthenticationEntiyPoint implements AuthenticationEntryPoint { | ||
|
||
private static final String CONTENT_TYPE = "application/json"; | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authException) throws IOException { | ||
response.setContentType(CONTENT_TYPE); | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
|
||
response.getWriter().write(ErrorResponse.toJson(authException)); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/study/realworld/security/JwtExceptionFilter.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,43 @@ | ||
package com.study.realworld.security; | ||
|
||
import com.study.realworld.global.exception.ErrorResponse; | ||
import com.study.realworld.global.exception.JwtException; | ||
import java.io.IOException; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Component | ||
public class JwtExceptionFilter extends OncePerRequestFilter { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(JwtExceptionFilter.class); | ||
|
||
private static final String CONTENT_TYPE = "application/json"; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (JwtException exception) { | ||
log.debug("Wrong jwt request exception occurred : {}", exception.getMessage()); | ||
sendErrorMessage(response, exception); | ||
} catch (RuntimeException exception) { | ||
log.error("Unexpected runtime exception occurred: {}", exception.getMessage(), exception); | ||
} | ||
} | ||
|
||
private void sendErrorMessage(HttpServletResponse response, JwtException e) throws IOException { | ||
response.setContentType(CONTENT_TYPE); | ||
response.setStatus(e.getHttpStatusValue()); | ||
|
||
response.getWriter().write(ErrorResponse.toJson(e)); | ||
} | ||
|
||
} |
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
Oops, something went wrong.