Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: profile 환경에 따른 cookie 설정 분리 및 config 업데이트 #102

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ ENV SPRING_ACTIVE_PROFILES ${SPRING_ACTIVE_PROFILES}

COPY build/libs/moabam-server-0.0.1-SNAPSHOT.jar moabam.jar

ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=${SPRING_ACTIVE_PROFILES}", "/moabam.jar"]
ENTRYPOINT ["java", "-jar", "-Duser.timezone=Asia/Seoul", "-Dspring.profiles.active=${SPRING_ACTIVE_PROFILES}", "/moabam.jar"]
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.moabam.api.infrastructure.redis.TokenRepository;
import com.moabam.global.auth.model.AuthorizationMember;
import com.moabam.global.auth.model.PublicClaim;
import com.moabam.global.common.util.CookieUtils;
import com.moabam.global.common.util.GlobalConstant;
import com.moabam.global.common.util.cookie.CookieUtils;
import com.moabam.global.config.OAuthConfig;
import com.moabam.global.config.TokenConfig;
import com.moabam.global.error.exception.BadRequestException;
Expand All @@ -44,6 +44,7 @@ public class AuthorizationService {
private final MemberService memberService;
private final JwtProviderService jwtProviderService;
private final TokenRepository tokenRepository;
private final CookieUtils cookieUtils;

public void redirectToLoginPage(HttpServletResponse httpServletResponse) {
String authorizationCodeUri = getAuthorizationCodeUri();
Expand Down Expand Up @@ -81,11 +82,11 @@ public void issueServiceToken(HttpServletResponse response, PublicClaim publicCl
tokenRepository.saveToken(publicClaim.id(), tokenSaveRequest);

response.addCookie(
CookieUtils.typeCookie("Bearer", tokenConfig.getRefreshExpire()));
cookieUtils.typeCookie("Bearer", tokenConfig.getRefreshExpire()));
response.addCookie(
CookieUtils.tokenCookie("access_token", accessToken, tokenConfig.getRefreshExpire()));
cookieUtils.tokenCookie("access_token", accessToken, tokenConfig.getRefreshExpire()));
response.addCookie(
CookieUtils.tokenCookie("refresh_token", refreshToken, tokenConfig.getRefreshExpire()));
cookieUtils.tokenCookie("refresh_token", refreshToken, tokenConfig.getRefreshExpire()));
}

public void validTokenPair(Long id, String oldRefreshToken) {
Expand All @@ -112,7 +113,7 @@ public void removeToken(HttpServletRequest httpServletRequest, HttpServletRespon
Arrays.stream(httpServletRequest.getCookies())
.forEach(cookie -> {
if (cookie.getName().contains("token")) {
httpServletResponse.addCookie(CookieUtils.deleteCookie(cookie));
httpServletResponse.addCookie(cookieUtils.deleteCookie(cookie));
}
});
}
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/com/moabam/global/common/util/CookieUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.moabam.global.common.util.cookie;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import jakarta.servlet.http.Cookie;

@Component
@Profile({"dev", "local", "test"})
public class CookieDevUtils extends CookieUtils {

protected Cookie detailCookies(String name, String value, long expireTime) {
Cookie cookie = new Cookie(name, value);
cookie.setSecure(true);
cookie.setHttpOnly(true);
cookie.setPath("/");
cookie.setMaxAge((int)expireTime);
cookie.setAttribute("SameSite", "None");

return cookie;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.moabam.global.common.util.cookie;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import jakarta.servlet.http.Cookie;

@Component
@Profile({"prod"})
public class CookieProdUtils extends CookieUtils {

protected Cookie detailCookies(String name, String value, long expireTime) {
Cookie cookie = new Cookie(name, value);
cookie.setSecure(true);
cookie.setHttpOnly(true);
cookie.setPath("/");
cookie.setMaxAge((int)expireTime);

return cookie;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.moabam.global.common.util.cookie;

import jakarta.servlet.http.Cookie;

public abstract class CookieUtils {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호 prod와 개발 서버를 나누기 위해서 결국 이렇게 됐군요...!


public Cookie tokenCookie(String name, String value, long expireTime) {
return detailCookies(name, value, expireTime);
}

public Cookie typeCookie(String value, long expireTime) {
return detailCookies("token_type", value, expireTime);
}

public Cookie deleteCookie(Cookie cookie) {
cookie.setMaxAge(0);
cookie.setPath("/");
return cookie;
}

protected abstract Cookie detailCookies(String name, String value, long expireTime);
}
4 changes: 3 additions & 1 deletion src/main/java/com/moabam/global/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand Down Expand Up @@ -54,7 +55,8 @@ public PathResolver pathResolver() {
PathMapper.parsePath("/images/*"),
PathMapper.parsePath("/webjars/*"),
PathMapper.parsePath("/favicon/*"),
PathMapper.parsePath("/*/icon-*")
PathMapper.parsePath("/*/icon-*"),
PathMapper.parsePath("/serverTime", List.of(HttpMethod.GET))
))
.build();

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config
2 changes: 1 addition & 1 deletion src/main/resources/static/docs/coupon.html
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ <h3 id="_쿠폰_사용_진행_중">쿠폰 사용 (진행 중)</h3>
<div id="footer">
<div id="footer-text">
Version 0.0.1-SNAPSHOT<br>
Last updated 2023-11-16 02:29:30 +0900
Last updated 2023-11-16 18:30:03 +0900
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ <h4 id="_상태코드httpstatus"><a class="link" href="#_상태코드httpstatus"
<div id="footer">
<div id="footer-text">
Version 0.0.1-SNAPSHOT<br>
Last updated 2023-11-16 02:29:30 +0900
Last updated 2023-11-16 18:30:03 +0900
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/docs/notification.html
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ <h4 id="_응답" class="discrete">응답</h4>
<div id="footer">
<div id="footer-text">
Version 0.0.1-SNAPSHOT<br>
Last updated 2023-11-16 02:29:30 +0900
Last updated 2023-11-16 18:30:04 +0900
</div>
</div>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
import com.moabam.api.infrastructure.redis.TokenRepository;
import com.moabam.global.auth.model.AuthorizationMember;
import com.moabam.global.auth.model.PublicClaim;
import com.moabam.global.common.util.CookieUtils;
import com.moabam.global.common.util.cookie.CookieDevUtils;
import com.moabam.global.common.util.cookie.CookieUtils;
import com.moabam.global.config.OAuthConfig;
import com.moabam.global.config.TokenConfig;
import com.moabam.global.error.exception.BadRequestException;
Expand Down Expand Up @@ -68,16 +69,19 @@ class AuthorizationServiceTest {
@Mock
TokenRepository tokenRepository;

CookieUtils cookieUtils;
OAuthConfig oauthConfig;
TokenConfig tokenConfig;
AuthorizationService noPropertyService;
OAuthConfig noOAuthConfig;

@BeforeEach
public void initParams() {
cookieUtils = new CookieDevUtils();
tokenConfig = new TokenConfig(null, 100000, 150000,
"testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttest");
ReflectionTestUtils.setField(authorizationService, "tokenConfig", tokenConfig);
ReflectionTestUtils.setField(authorizationService, "cookieUtils", cookieUtils);

oauthConfig = new OAuthConfig(
new OAuthConfig.Provider("https://authorization/url", "http://redirect/url", "http://token/url",
Expand All @@ -93,7 +97,7 @@ public void initParams() {
);
noPropertyService = new AuthorizationService(noOAuthConfig, tokenConfig,
oAuth2AuthorizationServerRequestService,
memberService, jwtProviderService, tokenRepository);
memberService, jwtProviderService, tokenRepository, cookieUtils);
}

@DisplayName("인가코드 URI 생성 매퍼 실패")
Expand Down Expand Up @@ -291,9 +295,9 @@ void error_with_expire_token(@WithMember AuthorizationMember authorizationMember
// given
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
httpServletRequest.setCookies(
CookieUtils.tokenCookie("access_token", "value", 100000),
CookieUtils.tokenCookie("refresh_token", "value", 100000),
CookieUtils.typeCookie("Bearer", 100000)
cookieUtils.tokenCookie("access_token", "value", 100000),
cookieUtils.tokenCookie("refresh_token", "value", 100000),
cookieUtils.typeCookie("Bearer", 100000)
);

MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/com/moabam/global/common/util/CookieMakeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.moabam.global.common.util;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import com.moabam.global.common.util.cookie.CookieDevUtils;
import com.moabam.global.common.util.cookie.CookieProdUtils;
import com.moabam.global.common.util.cookie.CookieUtils;

import jakarta.servlet.http.Cookie;

class CookieMakeTest {

CookieUtils cookieDevUtils;
CookieUtils cookieProdUtils;

@BeforeEach
void setUp() {
cookieDevUtils = new CookieDevUtils();
cookieProdUtils = new CookieProdUtils();
}

@DisplayName("prod환경에서 cookie 생성 테스트")
@Test
void prodUtilsTest() {
// Given
Cookie cookie = cookieProdUtils.tokenCookie("access_token", "value", 10000);

// When + Then
assertAll(
() -> assertThat(cookie.getSecure()).isTrue(),
() -> assertThat(cookie.getSecure()).isTrue(),
() -> assertThat(cookie.getPath()).isEqualTo("/"),
() -> assertThat(cookie.getMaxAge()).isEqualTo(10000)
);
}

@DisplayName("dev환경에서 cookie 생성 테스트")
@Test
void devUtilsTest() {
// Given
Cookie cookie = cookieDevUtils.tokenCookie("access_token", "value", 10000);

// When + Then
assertAll(
() -> assertThat(cookie.getSecure()).isTrue(),
() -> assertThat(cookie.getSecure()).isTrue(),
() -> assertThat(cookie.getPath()).isEqualTo("/"),
() -> assertThat(cookie.getMaxAge()).isEqualTo(10000),
() -> assertThat(cookie.getAttribute("SameSite")).isEqualTo("None")
);
}
}
11 changes: 7 additions & 4 deletions src/test/java/com/moabam/support/common/WithFilterSupporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.web.context.WebApplicationContext;

import com.moabam.api.application.auth.JwtProviderService;
import com.moabam.global.common.util.CookieUtils;
import com.moabam.global.common.util.cookie.CookieUtils;
import com.moabam.global.config.TokenConfig;
import com.moabam.support.fixture.PublicClaimFixture;

Expand All @@ -32,18 +32,21 @@ public class WithFilterSupporter {
@Autowired
TokenConfig tokenConfig;

@Autowired
CookieUtils cookieUtils;

protected MockMvc mockMvc;

@BeforeEach
void setUpMockMvc(RestDocumentationContextProvider contextProvider) {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(RestDocsFactory.restdocs(contextProvider))
.defaultRequest(get("/")
.cookie(CookieUtils.typeCookie("Bearer", tokenConfig.getRefreshExpire()))
.cookie(CookieUtils.tokenCookie("access_token",
.cookie(cookieUtils.typeCookie("Bearer", tokenConfig.getRefreshExpire()))
.cookie(cookieUtils.tokenCookie("access_token",
jwtProviderService.provideAccessToken(PublicClaimFixture.publicClaim()),
tokenConfig.getRefreshExpire()))
.cookie(CookieUtils.tokenCookie("refresh_token",
.cookie(cookieUtils.tokenCookie("refresh_token",
jwtProviderService.provideRefreshToken(),
tokenConfig.getRefreshExpire())))
.build();
Expand Down