-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π [Deploy] - μμ
λ‘κ·ΈμΈ λ° ResponseDto λ³κ²½μ¬ν λ°μ (#13)
* Feat: customException ꡬν * Feat: responseDto ꡬν * Feat: Spring Security, JWT Cookie, μμ λ‘κ·ΈμΈ κΈ°λ₯ ꡬν * Feat: Spring Security, JWT Cookie, μμ λ‘κ·ΈμΈ κΈ°λ₯ ꡬν * Fix: μ½λ μλ¬ μμ * Refactor: μμ§ μ¬μ©νμ§ μλ μ½λ μμ * Feat: oauth2 μμ‘΄μ± μΆκ° * Chore: Credentials μΆκ° --------- Co-authored-by: Jang99u <[email protected]> Co-authored-by: λ―Όμ₯κ· <[email protected]>
- Loading branch information
1 parent
5237ca3
commit 0985de4
Showing
44 changed files
with
1,520 additions
and
3 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
Submodule spot-server-properties
updated
from 6bf7ff to 88515a
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,11 @@ | ||
package ice.spot.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface UserId { | ||
} |
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,33 @@ | ||
package ice.spot.config; | ||
|
||
import ice.spot.constant.Constants; | ||
import ice.spot.interceptor.pre.UserIdArgumentResolver; | ||
import ice.spot.interceptor.pre.UserIdInterceptor; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@RequiredArgsConstructor | ||
public class WebMVCConfig implements WebMvcConfigurer { | ||
private final UserIdArgumentResolver userIdArgumentResolver; | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
WebMvcConfigurer.super.addArgumentResolvers(resolvers); | ||
resolvers.add(this.userIdArgumentResolver); | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(new UserIdInterceptor()) | ||
.addPathPatterns("/**") | ||
.excludePathPatterns(Constants.NO_NEED_AUTH); | ||
} | ||
} |
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,16 @@ | ||
package ice.spot.constant; | ||
|
||
import java.util.List; | ||
|
||
public class Constants { | ||
public static String CLAIM_USER_ID = "uuid"; | ||
public static String CLAIM_USER_ROLE = "role"; | ||
public static String PREFIX_BEARER = "Bearer "; | ||
public static String PREFIX_AUTH = "authorization"; | ||
public static String ACCESS_COOKIE_NAME = "access_token"; | ||
public static String REFRESH_COOKIE_NAME = "refresh_token"; | ||
public static List<String> NO_NEED_AUTH = List.of( | ||
"/api/auth/sign-up", | ||
"/api/auth/sign-in" | ||
); | ||
} |
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,27 @@ | ||
package ice.spot.controller; | ||
|
||
import ice.spot.annotation.UserId; | ||
import ice.spot.dto.global.ResponseDto; | ||
import ice.spot.dto.request.OauthSignUpDto; | ||
import ice.spot.service.AuthService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping("/api") | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
|
||
private final AuthService authService; | ||
|
||
@PostMapping("/oauth2/sign-up") | ||
public ResponseDto<?> signUp(@UserId Long userId, @RequestBody OauthSignUpDto oauthSignUpDto) { | ||
authService.signUp(userId, oauthSignUpDto); | ||
return ResponseDto.ok(null); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ice.spot.dto.global; | ||
|
||
import ice.spot.exeption.ErrorCode; | ||
|
||
public record ExceptionDto( | ||
Integer code, | ||
String message | ||
) { | ||
public ExceptionDto(ErrorCode errorCode) { | ||
this(errorCode.getCode(), errorCode.getMessage()); | ||
} | ||
|
||
public static ExceptionDto of(ErrorCode errorCode) { | ||
return new ExceptionDto(errorCode); | ||
} | ||
} |
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,60 @@ | ||
package ice.spot.dto.global; | ||
|
||
import ice.spot.exeption.CommonException; | ||
import ice.spot.exeption.ErrorCode; | ||
import jakarta.annotation.Nullable; | ||
import jakarta.validation.constraints.NotNull; | ||
import net.minidev.json.annotate.JsonIgnore; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.MissingServletRequestParameterException; | ||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
|
||
public record ResponseDto<T> ( | ||
@JsonIgnore HttpStatus httpStatus, | ||
boolean success, | ||
@Nullable T data, | ||
@Nullable ExceptionDto exceptionDto | ||
) { | ||
public static <T> ResponseDto<T> ok(T data){ | ||
return new ResponseDto<>( | ||
HttpStatus.OK, | ||
true, | ||
data, | ||
null | ||
); | ||
} | ||
public static ResponseDto<Boolean> created(Boolean data){ | ||
return new ResponseDto<>( | ||
HttpStatus.CREATED, | ||
true, | ||
data, | ||
null | ||
); | ||
} | ||
public static ResponseDto<?> fail(@NotNull CommonException e){ | ||
return new ResponseDto<>( | ||
e.getErrorCode().getHttpStatus(), | ||
false, | ||
null, | ||
new ExceptionDto(e.getErrorCode()) | ||
); | ||
} | ||
|
||
public static ResponseDto<?> fail(final MissingServletRequestParameterException e) { | ||
return new ResponseDto<>( | ||
HttpStatus.BAD_REQUEST, | ||
false, | ||
null, | ||
new ExceptionDto(ErrorCode.MISSING_REQUEST_PARAMETER) | ||
); | ||
} | ||
|
||
public static ResponseDto<?> fail(final MethodArgumentTypeMismatchException e) { | ||
return new ResponseDto<>( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
false, | ||
null, | ||
new ExceptionDto(ErrorCode.INVALID_PARAMETER_FORMAT) | ||
); | ||
} | ||
} |
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,9 @@ | ||
package ice.spot.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record OauthSignUpDto( | ||
@JsonProperty("nickname") | ||
String nickname | ||
) { | ||
} |
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,18 @@ | ||
package ice.spot.dto.response; | ||
|
||
import lombok.Builder; | ||
|
||
import java.io.Serializable; | ||
|
||
@Builder | ||
public record JwtTokenDto( | ||
String accessToken, | ||
String refreshToken | ||
) implements Serializable { | ||
public static JwtTokenDto of(String accessToken, String refreshToken) { | ||
return JwtTokenDto.builder() | ||
.accessToken(accessToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
} | ||
} |
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,13 @@ | ||
package ice.spot.dto.type; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum EProvider { | ||
|
||
KAKAO("KAKAO"); | ||
|
||
private final String name; | ||
} |
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,16 @@ | ||
package ice.spot.dto.type; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ERole { | ||
|
||
GUEST("GUEST", "ROLE_GUEST"), | ||
USER("USER", "ROLE_USER"), | ||
ADMIN("ADMIN", "ROLE_ADMIN"); | ||
|
||
private final String role; | ||
private final String securityRole; | ||
} |
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,11 @@ | ||
package ice.spot.exeption; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class CommonException extends RuntimeException { | ||
private final ErrorCode errorCode; | ||
public String getMessage() { return this.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ice.spot.exeption; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorCode { | ||
//400 | ||
WRONG_ENTRY_POINT(40000, HttpStatus.BAD_REQUEST, "μλͺ»λ μ κ·Όμ λλ€"), | ||
MISSING_REQUEST_PARAMETER(40001, HttpStatus.BAD_REQUEST, "νμ μμ² νλΌλ―Έν°κ° λλ½λμμ΅λλ€."), | ||
INVALID_PARAMETER_FORMAT(40002, HttpStatus.BAD_REQUEST, "μμ²μ μ ν¨νμ§ μμ μΈμ νμμ λλ€."), | ||
BAD_REQUEST_JSON(40003, HttpStatus.BAD_REQUEST, "μλͺ»λ JSON νμμ λλ€."), | ||
|
||
//401 | ||
INVALID_HEADER_VALUE(40100, HttpStatus.UNAUTHORIZED, "μ¬λ°λ₯΄μ§ μμ ν€λκ°μ λλ€."), | ||
EXPIRED_TOKEN_ERROR(40101, HttpStatus.UNAUTHORIZED, "λ§λ£λ ν ν°μ λλ€."), | ||
INVALID_TOKEN_ERROR(40102, HttpStatus.UNAUTHORIZED, "μ ν¨νμ§ μμ ν ν°μ λλ€."), | ||
TOKEN_MALFORMED_ERROR(40103, HttpStatus.UNAUTHORIZED, "ν ν°μ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€."), | ||
TOKEN_TYPE_ERROR(40104, HttpStatus.UNAUTHORIZED, "ν ν° νμ μ΄ μΌμΉνμ§ μκ±°λ λΉμ΄μμ΅λλ€."), | ||
TOKEN_UNSUPPORTED_ERROR(40105, HttpStatus.UNAUTHORIZED, "μ§μνμ§μλ ν ν°μ λλ€."), | ||
TOKEN_GENERATION_ERROR(40106, HttpStatus.UNAUTHORIZED, "ν ν° μμ±μ μ€ν¨νμμ΅λλ€."), | ||
TOKEN_UNKNOWN_ERROR(40107, HttpStatus.UNAUTHORIZED, "μ μ μλ ν ν°μ λλ€."), | ||
LOGIN_FAILURE(40108, HttpStatus.UNAUTHORIZED, "λ‘κ·ΈμΈμ μ€ν¨νμ΅λλ€"), | ||
|
||
//403 | ||
FORBIDDEN_ROLE(40300, HttpStatus.FORBIDDEN, "κΆνμ΄ μ‘΄μ¬νμ§ μμ΅λλ€."), | ||
|
||
//404 | ||
NOT_FOUND_USER(40400, HttpStatus.NOT_FOUND, "μ‘΄μ¬νμ§ μλ μ¬μ©μμ λλ€."), | ||
|
||
//500 | ||
INTERNAL_SERVER_ERROR(50000, HttpStatus.INTERNAL_SERVER_ERROR, "μλ² λ΄λΆ μ€λ₯μ λλ€"), | ||
; | ||
|
||
private final Integer code; | ||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
Oops, something went wrong.