-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from 9oormthonUniv-seoultech/develop
Develop to Main #1
- Loading branch information
Showing
71 changed files
with
1,644 additions
and
44 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
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
28 changes: 28 additions & 0 deletions
28
core/src/main/java/com/pocket/core/config/SwaggerConfig.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,28 @@ | ||
package com.pocket.core.config; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springdoc.core.models.GroupedOpenApi; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@OpenAPIDefinition( | ||
info = @io.swagger.v3.oas.annotations.info.Info( | ||
title = "POCKET 4 CUT API 명세서", | ||
description = "POCKET 4 CUT BE API 명세서입니다.", | ||
version = "v1") | ||
) | ||
@RequiredArgsConstructor | ||
@Configuration | ||
public class SwaggerConfig { | ||
|
||
@Bean | ||
public GroupedOpenApi stOpenApi() { | ||
|
||
return GroupedOpenApi.builder() | ||
.group("POCKET 4 CUT API v1") | ||
.pathsToMatch("/**") | ||
.build(); | ||
} | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
core/src/main/java/com/pocket/core/exception/common/ApiResponse.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,48 @@ | ||
package com.pocket.core.exception.common; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@JsonPropertyOrder({"statusCode", "message", "content"}) | ||
public class ApiResponse<T> { | ||
|
||
@JsonProperty("statusCode") | ||
@NonNull | ||
private final String statusCode; | ||
|
||
@JsonProperty("message") | ||
@NonNull | ||
private final String message; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonProperty("content") | ||
private T content; | ||
|
||
// 성공한 경우 응답 생성 | ||
public static <T> ApiResponse<T> onSuccess(T content) { | ||
return new ApiResponse<>(HttpStatus.OK.name(), HttpStatus.OK.getReasonPhrase(), content); | ||
} | ||
|
||
// 실패한 경우 응답 생성 | ||
public static <T> ApiResponse<T> onFailure(String statusCode, String message) { | ||
return new ApiResponse<>(statusCode, message, null); | ||
} | ||
|
||
public static <T> ApiResponse<T> onFailure(String statusCode, String message, T content) { | ||
return new ApiResponse<>(statusCode, message, content); | ||
} | ||
|
||
// Json serialize | ||
public String toJsonString() throws JsonProcessingException { | ||
return new ObjectMapper().writeValueAsString(this); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
core/src/main/java/com/pocket/core/exception/common/ApplicationResponse.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,108 @@ | ||
package com.pocket.core.exception.common; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.jackson.Jacksonized; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Jacksonized | ||
public class ApplicationResponse<T> { | ||
private ApplicationResult result; | ||
private T payload; | ||
|
||
public static <T> ApplicationResponse<T> ok(T payload) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(HttpStatus.OK.value()) | ||
.message("API 호출 성공") | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
|
||
public static <T> ApplicationResponse<T> ok(T payload, String message) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(HttpStatus.OK.value()) | ||
.message(message) | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
|
||
public static <T> ApplicationResponse<T> badRequest(T payload) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(HttpStatus.BAD_REQUEST.value()) | ||
.message("잘못된 요청입니다.") | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
|
||
public static <T> ApplicationResponse<T> badRequest(T payload, String message) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(HttpStatus.BAD_REQUEST.value()) | ||
.message(message) | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
|
||
public static <T> ApplicationResponse<T> notAuthenticated(T payload) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(HttpStatus.UNAUTHORIZED.value()) | ||
.message("잘못된 접근입니다.") | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
|
||
public static <T> ApplicationResponse<T> notAuthenticated(T payload, String message) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(HttpStatus.UNAUTHORIZED.value()) | ||
.message(message) | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
|
||
public static <T> ApplicationResponse<T> server(T payload) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(HttpStatus.INTERNAL_SERVER_ERROR.value()) | ||
.message("API 호출 실패") | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
|
||
public static <T> ApplicationResponse<T> server(T payload, String message) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(HttpStatus.INTERNAL_SERVER_ERROR.value()) | ||
.message(message) | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
|
||
public static <T> ApplicationResponse<T> custom(T payload, Integer code, String message) { | ||
return ApplicationResponse.<T>builder() | ||
.result(ApplicationResult.builder() | ||
.code(code) | ||
.message(message) | ||
.build()) | ||
.payload(payload) | ||
.build(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
core/src/main/java/com/pocket/core/exception/common/ApplicationResult.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,17 @@ | ||
package com.pocket.core.exception.common; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Jacksonized | ||
public class ApplicationResult { | ||
private Integer code; | ||
private String message; | ||
} |
22 changes: 22 additions & 0 deletions
22
core/src/main/java/com/pocket/core/exception/common/BaseEntity.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,22 @@ | ||
package com.pocket.core.exception.common; | ||
|
||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
@EntityListeners(AuditingEntityListener.class) | ||
public abstract class BaseEntity { | ||
|
||
@CreatedDate | ||
private LocalDateTime createdAt; | ||
|
||
@LastModifiedDate | ||
protected LocalDateTime updatedAt; | ||
} |
14 changes: 14 additions & 0 deletions
14
core/src/main/java/com/pocket/core/exception/common/BaseErrorCode.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,14 @@ | ||
package com.pocket.core.exception.common; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface BaseErrorCode { | ||
|
||
HttpStatus getHttpStatus(); | ||
|
||
String getCode(); | ||
|
||
String getMessage(); | ||
|
||
ApiResponse<Void> getErrorResponse(); | ||
} |
22 changes: 22 additions & 0 deletions
22
core/src/main/java/com/pocket/core/exception/common/GlobalErrorCode.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,22 @@ | ||
package com.pocket.core.exception.common; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum GlobalErrorCode implements BaseErrorCode { | ||
VALIDATION_FAILED(HttpStatus.BAD_REQUEST, "ERROR4000", "입력값에 대한 검증에 실패했습니다."), | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "ERROR5000", "서버에 문제가 발생했습니다. 잠시 후 다시 시도해주세요."); | ||
|
||
private final HttpStatus httpStatus; | ||
private final String code; | ||
private final String message; | ||
|
||
@Override | ||
public ApiResponse<Void> getErrorResponse() { | ||
return ApiResponse.onFailure(code, message); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
core/src/main/java/com/pocket/core/exception/jwt/JwtAccessDeniedHandler.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,29 @@ | ||
package com.pocket.core.exception.jwt; | ||
|
||
import com.pocket.core.util.HttpResponseUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* 인증된 사용자가 필요한 권한없이 접근하려고 할 때 발생하는 예외 처리 | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class JwtAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, | ||
AccessDeniedException accessDeniedException) throws IOException { | ||
log.warn("Access Denied: ", accessDeniedException); | ||
|
||
HttpResponseUtil.setErrorResponse(response, HttpStatus.FORBIDDEN, | ||
SecurityErrorCode.FORBIDDEN.getErrorResponse()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
core/src/main/java/com/pocket/core/exception/jwt/JwtAuthenticationEntryPoint.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,38 @@ | ||
package com.pocket.core.exception.jwt; | ||
|
||
import com.pocket.core.exception.common.ApiResponse; | ||
import com.pocket.core.util.HttpResponseUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* 사용자가 인증되지 않은 상태에서 접근하려고 할 때 발생하는 예외 처리 | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authException) | ||
throws IOException { | ||
HttpStatus httpStatus; | ||
ApiResponse<String> errorResponse; | ||
|
||
log.error(">>>>>> AuthenticationException: ", authException); | ||
httpStatus = HttpStatus.UNAUTHORIZED; | ||
errorResponse = ApiResponse.onFailure( | ||
SecurityErrorCode.UNAUTHORIZED.getCode(), | ||
SecurityErrorCode.UNAUTHORIZED.getMessage(), | ||
authException.getMessage()); | ||
|
||
HttpResponseUtil.setErrorResponse(response, httpStatus, errorResponse); | ||
} | ||
} |
Oops, something went wrong.