From 0d07654e54af246dd417584d5e7f81bfd64bf83b Mon Sep 17 00:00:00 2001 From: lookh <103165859+aiaiaiai1@users.noreply.github.com> Date: Sat, 29 Jun 2024 13:32:26 +0900 Subject: [PATCH] fix: cors and response of login (#3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: cors 설정 * fix: modify response field of login --- .../java/gymmi/controller/AuthController.java | 11 +++++---- src/main/java/gymmi/global/WebMvcConfig.java | 4 +++- .../java/gymmi/response/LoginResponse.java | 23 +++++++++++++++++++ ...TokensResponse.java => TokenResponse.java} | 4 ++-- src/main/java/gymmi/service/AuthService.java | 21 ++++++++++++----- 5 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 src/main/java/gymmi/response/LoginResponse.java rename src/main/java/gymmi/response/{TokensResponse.java => TokenResponse.java} (69%) diff --git a/src/main/java/gymmi/controller/AuthController.java b/src/main/java/gymmi/controller/AuthController.java index 02321a4..14116b0 100644 --- a/src/main/java/gymmi/controller/AuthController.java +++ b/src/main/java/gymmi/controller/AuthController.java @@ -5,7 +5,8 @@ import gymmi.request.LoginRequest; import gymmi.request.RegistrationRequest; import gymmi.request.ReissueRequest; -import gymmi.response.TokensResponse; +import gymmi.response.LoginResponse; +import gymmi.response.TokenResponse; import gymmi.service.AuthService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -27,14 +28,14 @@ public ResponseEntity registerUser(@Validated @RequestBody RegistrationReq } @PostMapping("/auth/welcome") - public ResponseEntity login(@Validated @RequestBody LoginRequest request) { - TokensResponse response = authService.login(request); + public ResponseEntity login(@Validated @RequestBody LoginRequest request) { + LoginResponse response = authService.login(request); return ResponseEntity.ok().body(response); } @PostMapping("/auth/reissue") - public ResponseEntity reissue(@Validated @RequestBody ReissueRequest request) { - TokensResponse response = authService.reissue(request); + public ResponseEntity reissue(@Validated @RequestBody ReissueRequest request) { + TokenResponse response = authService.reissue(request); return ResponseEntity.ok().body(response); } diff --git a/src/main/java/gymmi/global/WebMvcConfig.java b/src/main/java/gymmi/global/WebMvcConfig.java index 123f378..e9e0952 100644 --- a/src/main/java/gymmi/global/WebMvcConfig.java +++ b/src/main/java/gymmi/global/WebMvcConfig.java @@ -20,7 +20,9 @@ public class WebMvcConfig implements WebMvcConfigurer { public void addCorsMappings(final CorsRegistry registry) { registry.addMapping("/**") .allowedMethods(ALLOWED_METHOD_NAMES.split(",")) - .allowedOrigins("*"); + .allowedOriginPatterns("*") + .allowedHeaders("*") + .allowCredentials(true); } @Override diff --git a/src/main/java/gymmi/response/LoginResponse.java b/src/main/java/gymmi/response/LoginResponse.java new file mode 100644 index 0000000..170181f --- /dev/null +++ b/src/main/java/gymmi/response/LoginResponse.java @@ -0,0 +1,23 @@ +package gymmi.response; + +import lombok.Builder; +import lombok.Getter; + +@Getter +public class LoginResponse { + + private final Long userId; + private final String nickname; + private final String profileURL; + private final String accessToken; + private final String refreshToken; + + @Builder + public LoginResponse(Long userId, String nickname, String profileURL, String accessToken, String refreshToken) { + this.userId = userId; + this.nickname = nickname; + this.profileURL = profileURL; + this.accessToken = accessToken; + this.refreshToken = refreshToken; + } +} diff --git a/src/main/java/gymmi/response/TokensResponse.java b/src/main/java/gymmi/response/TokenResponse.java similarity index 69% rename from src/main/java/gymmi/response/TokensResponse.java rename to src/main/java/gymmi/response/TokenResponse.java index 7fd0c19..895eb7c 100644 --- a/src/main/java/gymmi/response/TokensResponse.java +++ b/src/main/java/gymmi/response/TokenResponse.java @@ -3,12 +3,12 @@ import lombok.Getter; @Getter -public class TokensResponse { +public class TokenResponse { private final String accessToken; private final String refreshToken; - public TokensResponse(String accessToken, String refreshToken) { + public TokenResponse(String accessToken, String refreshToken) { this.accessToken = accessToken; this.refreshToken = refreshToken; } diff --git a/src/main/java/gymmi/service/AuthService.java b/src/main/java/gymmi/service/AuthService.java index 663d99b..4ca6745 100644 --- a/src/main/java/gymmi/service/AuthService.java +++ b/src/main/java/gymmi/service/AuthService.java @@ -10,7 +10,8 @@ import gymmi.request.LoginRequest; import gymmi.request.RegistrationRequest; import gymmi.request.ReissueRequest; -import gymmi.response.TokensResponse; +import gymmi.response.LoginResponse; +import gymmi.response.TokenResponse; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -39,18 +40,26 @@ public void registerUser(RegistrationRequest request) { } @Transactional - public TokensResponse login(LoginRequest request) { + public LoginResponse login(LoginRequest request) { User user = userRepository.findByLoginId(request.getLoginId()) .orElseThrow(() -> new NotMatchedException("아이디와 비밀번호를 확인해 주세요.")); if (!user.canAuthenticate(request.getLoginId(), request.getPassword())) { throw new NotMatchedException("아이디와 비밀번호를 확인해 주세요."); } - return generateAndSaveTokensAbout(user); + + TokenResponse tokenResponse = generateAndSaveTokensAbout(user); + return LoginResponse.builder() + .userId(user.getId()) + .nickname(user.getNickname()) + .profileURL("") + .refreshToken(tokenResponse.getRefreshToken()) + .accessToken(tokenResponse.getAccessToken()) + .build(); } @Transactional - public TokensResponse reissue(ReissueRequest request) { + public TokenResponse reissue(ReissueRequest request) { Long userId = tokenProcessor.parseRefreshToken(request.getRefreshToken()); User user = userRepository.getByUserId(userId); Logined logined = loginedRepository.getByUserId(userId); @@ -63,12 +72,12 @@ public TokensResponse reissue(ReissueRequest request) { return generateAndSaveTokensAbout(user); } - private TokensResponse generateAndSaveTokensAbout(User user) { + private TokenResponse generateAndSaveTokensAbout(User user) { String accessToken = tokenProcessor.generateAccessToken(user.getId()); String refreshToken = tokenProcessor.generateRefreshToken(user.getId()); Logined logined = loginedRepository.getByUserId(user.getId()); logined.saveRefreshToken(refreshToken); - return new TokensResponse(accessToken, refreshToken); + return new TokenResponse(accessToken, refreshToken); } @Transactional