Compare commits
2 Commits
d9211cc12b
...
89d1acf5c9
| Author | SHA1 | Date | |
|---|---|---|---|
| 89d1acf5c9 | |||
| 6a2405a782 |
@@ -1,4 +1,4 @@
|
||||
package site.ocr.prd;
|
||||
package site.ocr.prd.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package site.ocr.prd;
|
||||
package site.ocr.prd.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -1,10 +1,12 @@
|
||||
package site.ocr.prd;
|
||||
package site.ocr.prd.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import site.ocr.prd.SessionLoggingInterceptor;
|
||||
|
||||
@Configuration
|
||||
public class WebMVCConfig implements WebMvcConfigurer {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package site.ocr.prd.contorllers;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
@@ -9,7 +9,6 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import site.ocr.prd.components.JwtProvider;
|
||||
import site.ocr.prd.dto.LoginReqDTO;
|
||||
import site.ocr.prd.dto.LoginResDTO;
|
||||
import site.ocr.prd.dto.UserInfoInqyReqDTO;
|
||||
@@ -26,12 +25,9 @@ public class LoginController {
|
||||
|
||||
//service 선언
|
||||
private final LoginService loginService;
|
||||
//JWT 선언
|
||||
private final JwtProvider jwtProvider;
|
||||
|
||||
public LoginController(LoginService loginService, JwtProvider provider) {
|
||||
public LoginController(LoginService loginService) {
|
||||
this.loginService = loginService;
|
||||
this.jwtProvider = provider;
|
||||
}
|
||||
/**
|
||||
* 프론트에서 카카오로 로그인 요청 후 카카오에서 리다이렉트 해준 인가코드로 토큰 발급 및 사용자정보 조회
|
||||
@@ -74,23 +70,32 @@ public class LoginController {
|
||||
UserInfoInqyResDTO userInfoInqyResponse = loginService.getUserInfo(userInfoInqyRequest);
|
||||
System.out.println("사용자정보 :: " + userInfoInqyResponse.toString());
|
||||
|
||||
/**
|
||||
* jwt 생성
|
||||
* oauth를 통해 받은 토큰을 직접 사용하지 말고 id에 jwt키를 발급해서 사용
|
||||
* cookie에 저장
|
||||
*/
|
||||
String jwt = jwtProvider.createJwtToken(userInfoInqyResponse.getId());
|
||||
ResponseCookie cookie = ResponseCookie.from("accessToken", jwt)
|
||||
.httpOnly(true)
|
||||
.secure(true)
|
||||
.sameSite("Lax")
|
||||
.maxAge(Duration.ofHours(1))
|
||||
.build();
|
||||
ResponseCookie cookie = loginService.createJwtCookie(Long.parseLong(userInfoInqyResponse.getId()));
|
||||
|
||||
return ResponseEntity.status(302)
|
||||
Map<String, Object> response = new java.util.HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("userId", userInfoInqyResponse.getId());
|
||||
response.put("message", "Login successful");
|
||||
response.put("userInfo", userInfoInqyResponse);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.SET_COOKIE, cookie.toString())
|
||||
.header(HttpHeaders.LOCATION, "http://localhost:3000/main")
|
||||
.build();
|
||||
.body(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* JWT 토큰 조회 (프론트에서 로그인 상태 확인용)
|
||||
* @param userId 사용자 ID
|
||||
* @return 로그인 결과 및 JWT 토큰
|
||||
*/
|
||||
@GetMapping("/oauth/get-jwt-token")
|
||||
public ResponseEntity<Map<String, String>> getJwtToken(@RequestParam Long userId) {
|
||||
ResponseCookie cookie = loginService.createJwtCookie(userId);
|
||||
Map<String, String> response = new java.util.HashMap<>();
|
||||
response.put("token", cookie.toString());
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.SET_COOKIE, cookie.toString())
|
||||
.body(response);
|
||||
}
|
||||
|
||||
@GetMapping("login/oauth-kakao-token")
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
package site.ocr.prd.services;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import site.ocr.prd.components.JwtProvider;
|
||||
import site.ocr.prd.dto.LoginReqDTO;
|
||||
import site.ocr.prd.dto.LoginResDTO;
|
||||
import site.ocr.prd.dto.UserInfoInqyReqDTO;
|
||||
@@ -18,11 +22,12 @@ public class LoginService {
|
||||
|
||||
//webclient builder
|
||||
private final WebClient webClient;
|
||||
//JWT provider
|
||||
private final JwtProvider jwtProvider;
|
||||
|
||||
|
||||
|
||||
public LoginService(WebClient.Builder builder) {
|
||||
public LoginService(WebClient.Builder builder, JwtProvider jwtProvider) {
|
||||
this.webClient = builder.build();
|
||||
this.jwtProvider = jwtProvider;
|
||||
}
|
||||
|
||||
public LoginResDTO getToken(LoginReqDTO request) {
|
||||
@@ -65,4 +70,19 @@ public class LoginService {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* JWT 토큰을 생성하고 ResponseCookie로 변환
|
||||
* @param userId 사용자 ID
|
||||
* @return ResponseCookie JWT 토큰이 포함된 쿠키
|
||||
*/
|
||||
public ResponseCookie createJwtCookie(Long userId) {
|
||||
String jwt = jwtProvider.createJwtToken(userId);
|
||||
return ResponseCookie.from("accessToken", jwt)
|
||||
.httpOnly(true)
|
||||
.secure(true)
|
||||
.sameSite("Lax")
|
||||
.maxAge(Duration.ofHours(1))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user