From 503c80d4816b6830fbbc14e81ad8427cf9b1e23a Mon Sep 17 00:00:00 2001 From: sunny2you Date: Sun, 24 Nov 2024 03:50:42 +0900 Subject: [PATCH] [GOORM-4]-fix:cors --- .../team49/common/auth/CorsConfig.java | 32 ++++++++----------- .../comment/application/CommentService.java | 6 ++-- .../team49/domain/comment/domain/Comment.java | 12 +++---- .../presentation/CommentController.java | 4 +-- .../response/CommentListResponse.java | 5 +-- .../response/CommentResponse.java | 7 ++-- .../presentation/UserInfoResponseDto.java | 9 ------ 7 files changed, 30 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/goormthon3/team49/common/auth/CorsConfig.java b/src/main/java/com/goormthon3/team49/common/auth/CorsConfig.java index 76c5ec2..4807b35 100644 --- a/src/main/java/com/goormthon3/team49/common/auth/CorsConfig.java +++ b/src/main/java/com/goormthon3/team49/common/auth/CorsConfig.java @@ -5,27 +5,23 @@ import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration -public class CorsConfig { - - @Bean - public CorsFilter corsFilter() { - CorsConfiguration config = new CorsConfiguration(); - config.setAllowCredentials(true); // 인증 정보 포함 허용 - config.addAllowedOrigin("http://localhost:3000"); // Origin 허용 - config.addAllowedMethod("GET"); - config.addAllowedMethod("POST"); - config.addAllowedMethod("PUT"); - config.addAllowedMethod("DELETE"); - config.addAllowedMethod("OPTIONS"); // 명시적으로 OPTIONS 메서드 포함 - config.addAllowedHeader("Content-Type"); // Content-Type 명시 - config.addAllowedHeader("Authorization"); // Authorization 허용 - - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - source.registerCorsConfiguration("/**", config); - return new CorsFilter(source); +public class CorsConfig implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOriginPatterns("*") + .allowedMethods("GET", "POST", "PUT", "DELETE") + .allowedHeaders("Authorization", "Content-Type") + .exposedHeaders("Custom-Header") + .allowCredentials(true) + .maxAge(3600); } + } + diff --git a/src/main/java/com/goormthon3/team49/domain/comment/application/CommentService.java b/src/main/java/com/goormthon3/team49/domain/comment/application/CommentService.java index 6ac9755..6d039a2 100644 --- a/src/main/java/com/goormthon3/team49/domain/comment/application/CommentService.java +++ b/src/main/java/com/goormthon3/team49/domain/comment/application/CommentService.java @@ -23,10 +23,10 @@ public class CommentService { private final UserRepository userRepository; @Transactional - public CommentResponse createComment(Long product_id, CommentRequest request, User user) { - Comment comment=Comment.create(request.content(),user,product_id); + public CommentResponse createComment(Long product_id, CommentRequest request, String username) { + Comment comment=Comment.create(request.content(),username,product_id); commentRepository.save(comment); - return CommentResponse.of(comment,user); + return CommentResponse.of(comment); } @Transactional diff --git a/src/main/java/com/goormthon3/team49/domain/comment/domain/Comment.java b/src/main/java/com/goormthon3/team49/domain/comment/domain/Comment.java index 0d789a7..6ae4d3b 100644 --- a/src/main/java/com/goormthon3/team49/domain/comment/domain/Comment.java +++ b/src/main/java/com/goormthon3/team49/domain/comment/domain/Comment.java @@ -16,25 +16,23 @@ public class Comment extends BaseTimeEntity { @Id @GeneratedValue(strategy = IDENTITY) - private Long id; + private Long commentId; @Column(nullable = false, length = 2000) private String content; - @ManyToOne - @JoinColumn - private User user; + @Column(nullable = false) + private String username; @Column(nullable = false) private Long productId; - public static Comment create(String content, User user, Long product_id) { + public static Comment create(String content, String username, Long product_id) { return Comment.builder() .content(content) - .user(user) + .username(username) .productId(product_id) .build(); } - } diff --git a/src/main/java/com/goormthon3/team49/domain/comment/presentation/CommentController.java b/src/main/java/com/goormthon3/team49/domain/comment/presentation/CommentController.java index 1e499cd..8927617 100644 --- a/src/main/java/com/goormthon3/team49/domain/comment/presentation/CommentController.java +++ b/src/main/java/com/goormthon3/team49/domain/comment/presentation/CommentController.java @@ -35,8 +35,8 @@ public ResponseEntity createReview( String token = authHeader.substring(7); Long kakaouserID=userLoginService.getKakaoUserIdFromAccessToken(token); - User user=userLoginService.findUserByFromKakaoUserId(kakaouserID); - CommentResponse response = commentService.createComment(productId, request, user); + String username=userLoginService.findUserByFromKakaoUserId(kakaouserID).getUserName(); + CommentResponse response = commentService.createComment(productId, request, username); return ResponseEntity.status(CREATED).body(response); } diff --git a/src/main/java/com/goormthon3/team49/domain/comment/presentation/response/CommentListResponse.java b/src/main/java/com/goormthon3/team49/domain/comment/presentation/response/CommentListResponse.java index 8e8e473..74e8561 100644 --- a/src/main/java/com/goormthon3/team49/domain/comment/presentation/response/CommentListResponse.java +++ b/src/main/java/com/goormthon3/team49/domain/comment/presentation/response/CommentListResponse.java @@ -15,8 +15,9 @@ public static CommentListResponse of(Long product_id, Long comment_count, List CommentResponse.of(comment, comment.getUser())) // User 객체를 제공 - .toList()) + .map(CommentResponse::of) // CommentResponse.of(comment) -> 이 부분 수정 + .toList()) // 끝에 toList()를 제대로 닫기 .build(); } } + diff --git a/src/main/java/com/goormthon3/team49/domain/comment/presentation/response/CommentResponse.java b/src/main/java/com/goormthon3/team49/domain/comment/presentation/response/CommentResponse.java index acc935c..a011ac4 100644 --- a/src/main/java/com/goormthon3/team49/domain/comment/presentation/response/CommentResponse.java +++ b/src/main/java/com/goormthon3/team49/domain/comment/presentation/response/CommentResponse.java @@ -16,12 +16,11 @@ public record CommentResponse( @DateTimeFormat(pattern="yyyy-MM-dd") String createdAt ) { - public static CommentResponse of(Comment comment, User user) { + public static CommentResponse of(Comment comment) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); return CommentResponse.builder() - .comment_id(comment.getId()) - .user_id(user.getUserId()) - .username(user.getUserName()) + .comment_id(comment.getCommentId()) + .username(comment.getUsername()) .content(comment.getContent()) .createdAt(comment.getCreatedAt().format(formatter)) .build(); diff --git a/src/main/java/com/goormthon3/team49/domain/user/presentation/UserInfoResponseDto.java b/src/main/java/com/goormthon3/team49/domain/user/presentation/UserInfoResponseDto.java index a4120fb..0134471 100644 --- a/src/main/java/com/goormthon3/team49/domain/user/presentation/UserInfoResponseDto.java +++ b/src/main/java/com/goormthon3/team49/domain/user/presentation/UserInfoResponseDto.java @@ -25,15 +25,6 @@ public static class KakaoAccount { @JsonProperty("profile") private Profile profile; - @JsonSetter("profile") - public void setProfile(Profile profile) { - if (profile == null) { - this.profile = new Profile(); // 기본값을 설정 (빈 Profile 객체) - } else { - this.profile = profile; - } - } - @Getter @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true)