Skip to content

Commit

Permalink
Merge pull request #475 from Clubber2024/refactor/#474-refactor-운영-환경…
Browse files Browse the repository at this point in the history
…에-따른-쿠키-설정-변경

refactor : 운영 환경에 따른 쿠키 설정 변경
  • Loading branch information
mjKim1229 authored Oct 17, 2024
2 parents c6a69c5 + 6e03e7e commit a9e0f5e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.clubber.ClubberServer;

import com.clubber.ClubberServer.global.infrastructure.outer.api.oauth.properties.KakaoProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ResponseEntity<CreateAdminsLoginResponse> createAdminsLogin(
@RequestBody @Valid CreateAdminsLoginRequest loginRequest) {
CreateAdminsLoginResponse createAdminsLoginResponse = adminService.createAdminsLogin(loginRequest);
return ResponseEntity.ok()
.headers(cookieHelper.getCookies(createAdminsLoginResponse))
.headers(cookieHelper.getCookies(createAdminsLoginResponse.getAccessToken(), createAdminsLoginResponse.getRefreshToken()))
.body(createAdminsLoginResponse);
}

Expand Down Expand Up @@ -80,7 +80,8 @@ public ResponseEntity<CreateAdminsLoginResponse> createAdminsTokenRefresh(
CreateAdminsLoginResponse createAdminsLoginResponse = adminService.getAdminsParseToken(
refreshTokenCookie != null ? refreshTokenCookie : refreshToken);
return ResponseEntity.ok()
.headers(cookieHelper.getCookies(createAdminsLoginResponse))
.headers(cookieHelper.getCookies(createAdminsLoginResponse.getAccessToken(),
createAdminsLoginResponse.getRefreshToken()))
.body(createAdminsLoginResponse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ResponseEntity<KakaoOauthResponse> getCredentialFromKakao(@RequestParam S
User user = authService.loginOrSignUp(userKakaoInfo);
KakaoOauthResponse kakaoOauthResponse = authService.generateUserToken(user);
return ResponseEntity.ok()
.headers(cookieHelper.getCookies(kakaoOauthResponse))
.headers(cookieHelper.getCookies(kakaoOauthResponse.getAccessToken(), kakaoOauthResponse.getRefreshToken()))
.body(kakaoOauthResponse);
}

Expand All @@ -83,7 +83,8 @@ public ResponseEntity<KakaoOauthResponse> tokenRefresh(
KakaoOauthResponse kakaoOauthResponse = authService.tokenRefresh(
refreshTokenCookie != null ? refreshTokenCookie : refreshToken);
return ResponseEntity.ok()
.headers(cookieHelper.getCookies(kakaoOauthResponse))
.headers(cookieHelper.getCookies(kakaoOauthResponse.getAccessToken(),
kakaoOauthResponse.getRefreshToken()))
.body(kakaoOauthResponse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import org.springframework.http.ResponseCookie;
import org.springframework.stereotype.Component;

import com.clubber.ClubberServer.domain.admin.dto.CreateAdminsLoginResponse;
import com.clubber.ClubberServer.domain.auth.dto.KakaoOauthResponse;
import com.clubber.ClubberServer.global.helper.SpringEnvironmentHelper;
import com.clubber.ClubberServer.global.jwt.JwtProperties;

import lombok.RequiredArgsConstructor;
Expand All @@ -15,78 +14,67 @@
public class CookieHelper {
private final JwtProperties jwtProperties;

public HttpHeaders getCookies(CreateAdminsLoginResponse createAdminsLoginResponse) {
ResponseCookie accessToken = ResponseCookie
.from("accessToken", createAdminsLoginResponse.getAccessToken())
.maxAge(jwtProperties.getAccessExp())
.secure(false)
.sameSite("Strict")
.httpOnly(true)
.path("/")
.build();
private final SpringEnvironmentHelper springEnvironmentHelper;

ResponseCookie refreshToken = ResponseCookie
.from("refreshToken", createAdminsLoginResponse.getRefreshToken())
.maxAge(jwtProperties.getRefreshExp())
.secure(false)
.sameSite("Strict")
.httpOnly(true)
.path("/")
.build();
public HttpHeaders getCookies(String accessToken, String refreshToken) {
String sameSite = "None";

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.SET_COOKIE, accessToken.toString());
httpHeaders.add(HttpHeaders.SET_COOKIE, refreshToken.toString());
return httpHeaders;
}
if(springEnvironmentHelper.isProdProfile()){
sameSite = "Strict";
}

public HttpHeaders getCookies(KakaoOauthResponse kakaoOauthResponse) {
ResponseCookie accessToken = ResponseCookie
.from("accessToken", kakaoOauthResponse.getAccessToken())
ResponseCookie accessTokenCookie = ResponseCookie
.from("accessToken", accessToken)
.maxAge(jwtProperties.getAccessExp())
.secure(true)
.sameSite("Strict")
.sameSite(sameSite)
.httpOnly(true)
.path("/")
.build();

ResponseCookie refreshToken = ResponseCookie
.from("refreshToken", kakaoOauthResponse.getRefreshToken())
ResponseCookie refreshTokenCookie = ResponseCookie
.from("refreshToken", refreshToken)
.maxAge(jwtProperties.getRefreshExp())
.secure(true)
.sameSite("Strict")
.sameSite(sameSite)
.httpOnly(true)
.path("/")
.build();

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.SET_COOKIE, accessToken.toString());
httpHeaders.add(HttpHeaders.SET_COOKIE, refreshToken.toString());
httpHeaders.add(HttpHeaders.SET_COOKIE, accessTokenCookie.toString());
httpHeaders.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString());
return httpHeaders;
}

public HttpHeaders deleteCookies() {
ResponseCookie accessToken = ResponseCookie
String sameSite = "None";

if(springEnvironmentHelper.isProdProfile()){
sameSite = "Strict";
}

ResponseCookie accessTokenCookie = ResponseCookie
.from("accessToken", null)
.maxAge(0)
.secure(true)
.sameSite("Strict")
.sameSite(sameSite)
.httpOnly(true)
.path("/")
.build();

ResponseCookie refreshToken = ResponseCookie
ResponseCookie refreshTokenCookie = ResponseCookie
.from("refreshToken", null)
.maxAge(0)
.secure(true)
.sameSite("Strict")
.sameSite(sameSite)
.httpOnly(true)
.path("/")
.build();

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.SET_COOKIE, accessToken.toString());
httpHeaders.add(HttpHeaders.SET_COOKIE, refreshToken.toString());
httpHeaders.add(HttpHeaders.SET_COOKIE, accessTokenCookie.toString());
httpHeaders.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString());
return httpHeaders;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.clubber.ClubberServer.global.helper;

import java.util.Arrays;
import java.util.List;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class SpringEnvironmentHelper {
private final Environment environment;

private static final String PROD = "prod";

public Boolean isProdProfile() {
String[] activeProfiles = environment.getActiveProfiles();
List<String> currentProfile = Arrays.stream(activeProfiles).toList();
return currentProfile.contains(PROD);
}
}

0 comments on commit a9e0f5e

Please sign in to comment.