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: refresh API aT 사용하는 것으로 변경 #222

Merged
merged 3 commits into from
Sep 11, 2022
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
12 changes: 6 additions & 6 deletions src/main/java/com/ceos/bankids/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.ceos.bankids.config.CommonResponse;
import com.ceos.bankids.controller.request.ExpoRequest;
import com.ceos.bankids.controller.request.FamilyRequest;
import com.ceos.bankids.controller.request.TokenRequest;
import com.ceos.bankids.controller.request.UserTypeRequest;
import com.ceos.bankids.controller.request.WithdrawalRequest;
import com.ceos.bankids.domain.User;
Expand All @@ -29,7 +30,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand Down Expand Up @@ -68,14 +68,14 @@ public CommonResponse<UserDTO> patchUserType(@AuthenticationPrincipal User authU
@ApiOperation(value = "토큰 리프레시")
@PatchMapping(value = "/refresh", produces = "application/json; charset=utf-8")
@ResponseBody
public CommonResponse<LoginDTO> refreshUserToken(
@CookieValue("refreshToken") String refreshToken, HttpServletResponse response) {
public CommonResponse<LoginDTO> refreshUserToken(@Valid @RequestBody TokenRequest tokenRequest,
HttpServletResponse response) {

log.info("api = 토큰 리프레시");
User user = userService.getUserByRefreshToken(refreshToken);
User user = userService.getUserByRefreshToken(tokenRequest.getAccessToken());
LoginDTO loginDTO = userService.issueNewTokens(user, user.getProvider());

userService.setNewCookie(user, response);
// userService.setNewCookie(user, response);
return CommonResponse.onSuccess(loginDTO);
}

Expand Down Expand Up @@ -148,7 +148,7 @@ public CommonResponse<UserDTO> patchExpoToken(@AuthenticationPrincipal User auth
log.info("api = 유저 엑스포 토큰 등록, user = {}", authUser.getUsername());
User user = userService.updateUserExpoToken(authUser, expoRequest);

userService.setNewCookie(user, response);
// userService.setNewCookie(user, response);
return CommonResponse.onSuccess(null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ceos.bankids.controller.request;

import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class TokenRequest {

@ApiModelProperty(example = "asdfasdfasdf")
@NotNull(message = "accessToken may not be null")
private String accessToken;
}
3 changes: 1 addition & 2 deletions src/main/java/com/ceos/bankids/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.ceos.bankids.dto.OptInDTO;
import com.ceos.bankids.dto.UserDTO;
import com.ceos.bankids.dto.oauth.KakaoUserDTO;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -24,7 +23,7 @@ public LoginDTO loginWithAppleAuthenticationCode(String authenticationCode,

public LoginDTO issueNewTokens(User user, String provider);

public void setNewCookie(User user, HttpServletResponse response);
// public void setNewCookie(User user, HttpServletResponse response);

public MyPageDTO getUserInformation(User user);

Expand Down
23 changes: 11 additions & 12 deletions src/main/java/com/ceos/bankids/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Calendar;
import java.util.Optional;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -163,17 +162,17 @@ public LoginDTO issueNewTokens(User user, String provider) {
return loginDTO;
}

@Override
@Transactional
public void setNewCookie(User user, HttpServletResponse response) {
Cookie cookie = new Cookie("refreshToken", user.getRefreshToken());
cookie.setMaxAge(14 * 24 * 60 * 60);
cookie.setSecure(true);
cookie.setHttpOnly(true);
cookie.setPath("/");

response.addCookie(cookie);
}
// @Override
// @Transactional
// public void setNewCookie(User user, HttpServletResponse response) {
// Cookie cookie = new Cookie("refreshToken", user.getRefreshToken());
// cookie.setMaxAge(14 * 24 * 60 * 60);
// cookie.setSecure(true);
// cookie.setHttpOnly(true);
// cookie.setPath("/");
//
// response.addCookie(cookie);
// }

@Override
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ceos.bankids.controller.UserController;
import com.ceos.bankids.controller.request.ExpoRequest;
import com.ceos.bankids.controller.request.FamilyRequest;
import com.ceos.bankids.controller.request.TokenRequest;
import com.ceos.bankids.controller.request.UserTypeRequest;
import com.ceos.bankids.controller.request.WithdrawalRequest;
import com.ceos.bankids.domain.Family;
Expand Down Expand Up @@ -571,7 +572,7 @@ public void testIfParentTokenRefreshSucceedThenReturnResult() {
slackService,
notificationService
);
CommonResponse result = userController.refreshUserToken("rT", response);
CommonResponse result = userController.refreshUserToken(new TokenRequest("rT"), response);

// then
LoginDTO loginDTO = new LoginDTO(false, "aT", user.getProvider());
Expand Down Expand Up @@ -638,7 +639,7 @@ public void testIfKidTokenRefreshSucceedThenReturnResult() {
notificationService
);

CommonResponse result = userController.refreshUserToken("rT", response);
CommonResponse result = userController.refreshUserToken(new TokenRequest("rT"), response);

// then
LoginDTO loginDTO = new LoginDTO(true, "aT", 1L, user.getProvider());
Expand Down Expand Up @@ -701,7 +702,7 @@ public void testIfUserTokenRefreshSucceedThenReturnResult() {
notificationService
);

CommonResponse result = userController.refreshUserToken("rT", response);
CommonResponse result = userController.refreshUserToken(new TokenRequest("rT"), response);

// then
LoginDTO loginDTO = new LoginDTO(null, "aT", user.getProvider());
Expand Down