Skip to content

Commit

Permalink
Merge pull request #164 from APPS-sookmyung/nginx_feat/#149
Browse files Browse the repository at this point in the history
[Fix] cors 추가
  • Loading branch information
ajung7038 authored Dec 24, 2024
2 parents f96763c + f30c842 commit 3e650c9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/main/java/WELLET/welletServer/config/CorsMvcConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package WELLET.welletServer.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsMvcConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry corsRegistry) {

corsRegistry.addMapping("/**")
.exposedHeaders("Set-Cookie")
.allowedOrigins("https://wellet.netlify.app");// 프론트 서버 주소
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -37,7 +39,7 @@ public class KakaoLoginController {


@GetMapping("/callback")
public ResponseEntity<?> callback(@RequestParam("code") String code, HttpServletResponse response) throws IOException {
public String callback(@RequestParam("code") String code, HttpServletResponse response) throws IOException {
try {
// 1. 카카오에서 accessToken 받아오기
String accessToken = kakaoService.getAccessTokenFromKakao(code);
Expand All @@ -59,21 +61,25 @@ public ResponseEntity<?> callback(@RequestParam("code") String code, HttpServlet

// 4. JWT 생성
String jwtToken = jwtService.generateToken(member); // 생성된 또는 업데이트된 사용자로 JWT 생성

// 5. 쿠키에 JWT 저장
//
// // 5. 쿠키에 JWT 저장
Cookie jwtCookie = new Cookie("jwtToken", jwtToken);
jwtCookie.setHttpOnly(true); // JavaScript로 쿠키에 접근 불가
// jwtCookie.setSecure(true); // HTTPS에서만 전송
jwtCookie.setSecure(true); // HTTPS에서만 전송
jwtCookie.setMaxAge(60 * 60 * 24); // 쿠키 유효 시간 설정
jwtCookie.setPath("/"); // 쿠키를 모든 경로에 적용

jwtCookie.setDomain("wellet.netlify.app");
response.addCookie(jwtCookie);

// 6. 성공적으로 로그인 완료 시 OK 응답
return ResponseEntity.ok("로그인 성공");
// 리다이렉트 URL 설정
String redirectUrl = "https://wellet.netlify.app"; // 원하는 프론트엔드 URL
response.sendRedirect(redirectUrl);
return "로그인 성공";

} catch (Exception e) {
log.error("로그인 처리 중 오류 발생", e);
return new ResponseEntity<>("로그인 처리 중 오류", HttpStatus.INTERNAL_SERVER_ERROR);
return "로그인 처리 중 오류" + HttpStatus.INTERNAL_SERVER_ERROR;
}
}
}
Expand Down

0 comments on commit 3e650c9

Please sign in to comment.