Skip to content

Commit 59afea7

Browse files
authored
[refactor] https 배포 환경을 위한 쿠키 설정 수정 (#224)
* [refactor] https 배포 환경을 위한 쿠키 설정 수정 * [refactor] front redirectUrl 설정 * [refacor] 쿠키 도메인 설정 추가
1 parent b53cf6d commit 59afea7

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

src/main/java/com/back/domain/auth/controller/AuthController.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import jakarta.validation.Valid;
1919
import lombok.RequiredArgsConstructor;
2020
import lombok.extern.slf4j.Slf4j;
21+
import org.springframework.beans.factory.annotation.Value;
2122
import org.springframework.http.HttpHeaders;
2223
import org.springframework.http.ResponseCookie;
2324
import org.springframework.http.ResponseEntity;
@@ -33,6 +34,12 @@ public class AuthController {
3334

3435
private final AuthService authService;
3536

37+
@Value("${app.cookie.secure}")
38+
private boolean cookieSecure;
39+
40+
@Value("${app.cookie.domain}")
41+
private String cookieDomain;
42+
3643
/**
3744
* 회원가입
3845
*/
@@ -177,7 +184,8 @@ public ResponseEntity<RsData<AuthResponse>> refreshToken(
177184
private ResponseCookie createTokenCookie(String name, String value, long maxAgeSeconds) {
178185
return ResponseCookie.from(name, value)
179186
.httpOnly(true)
180-
.secure(false) // 운영시 true
187+
.secure(cookieSecure) // 환경변수로 제어, 개발: false, 운영: true
188+
.domain(cookieDomain) // 환경별 도메인 설정
181189
.path("/")
182190
.maxAge(maxAgeSeconds)
183191
.sameSite("Strict")
@@ -190,7 +198,8 @@ private ResponseCookie createTokenCookie(String name, String value, long maxAgeS
190198
private ResponseCookie deleteCookie(String name) {
191199
return ResponseCookie.from(name, "")
192200
.httpOnly(true)
193-
.secure(false)
201+
.secure(cookieSecure) // 환경변수로 제어, 개발: false, 운영: true
202+
.domain(cookieDomain) // 환경별 도메인 설정
194203
.path("/")
195204
.maxAge(0) // 즉시 만료
196205
.sameSite("Strict")

src/main/java/com/back/global/security/oauth2/OAuth2SuccessHandler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public class OAuth2SuccessHandler extends SimpleUrlAuthenticationSuccessHandler
4141
@Value("${app.frontend-url}")
4242
private String frontendUrl;
4343

44+
@Value("${app.cookie.secure}")
45+
private boolean cookieSecure;
46+
47+
@Value("${app.cookie.domain}")
48+
private String cookieDomain;
49+
4450
/**
4551
* OAuth2 로그인 성공 시 호출
4652
* JWT 토큰 생성 후 프론트엔드로 리다이렉트
@@ -116,7 +122,8 @@ public void onAuthenticationSuccess(
116122
private ResponseCookie createTokenCookie(String name, String value, long maxAgeSeconds) {
117123
return ResponseCookie.from(name, value)
118124
.httpOnly(true)
119-
.secure(false) // 개발: false, 운영: true
125+
.secure(cookieSecure) // 환경변수로 제어, 개발: false, 운영: true
126+
.domain(cookieDomain) // 환경별 도메인 설정
120127
.path("/")
121128
.maxAge(maxAgeSeconds)
122129
.sameSite("Strict")

src/main/resources/application-dev.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ spring:
1515
# JPA 설정
1616
jpa:
1717
database-platform: org.hibernate.dialect.H2Dialect
18+
19+
# https 설정
20+
app:
21+
cookie:
22+
secure: false
23+
domain: localhost
24+
frontend-url: http://localhost:3000
1825

1926
# JWT 설정 (.env 파일 없어도 동작하도록 기본값 제공)
2027
jwt:

src/main/resources/application-prod.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ custom:
3333
cookieDomain: "${custom.prod.cookieDomain}"
3434
frontUrl: "${custom.prod.frontUrl}"
3535
backUrl: "${custom.prod.backUrl}"
36-
name: mori-mori
36+
name: mori-mori
37+
38+
# https 설정
39+
app:
40+
cookie:
41+
secure: true
42+
domain: .mori-mori.store
43+
frontend-url: ${FRONTEND_URL:https://mori-mori.store}

src/main/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ spring:
8585

8686
# Frontend URL (OAuth2 리다이렉트용, UTM 링크 생성용)
8787
app:
88+
cookie:
89+
secure: false
90+
domain: localhost
8891
frontend-url: ${FRONTEND_URL:https://mori-mori.store}
8992

9093
# JWT 설정 (.env 파일 없어도 동작하도록 기본값 제공)

0 commit comments

Comments
 (0)