로그인 구조 변경
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package site.ocr.prd.contorllers;
|
package site.ocr.prd.contorllers;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.ResponseCookie;
|
import org.springframework.http.ResponseCookie;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -23,6 +26,9 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||||||
@Controller
|
@Controller
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
|
|
||||||
|
//Logger 선언
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
|
||||||
|
|
||||||
//service 선언
|
//service 선언
|
||||||
private final LoginService loginService;
|
private final LoginService loginService;
|
||||||
|
|
||||||
@@ -34,9 +40,9 @@ public class LoginController {
|
|||||||
* @param redirectRespn 카카오에서 리다이렉트해준 인가코드
|
* @param redirectRespn 카카오에서 리다이렉트해준 인가코드
|
||||||
*/
|
*/
|
||||||
@GetMapping("/oauth/oauth-kakao-authorize") //kakao에서 get으로 리다이렉트 해줌
|
@GetMapping("/oauth/oauth-kakao-authorize") //kakao에서 get으로 리다이렉트 해줌
|
||||||
public ResponseEntity kakaoLoginRequest(HttpServletRequest redirectRespn) {
|
public ResponseEntity<Map<String, String>> kakaoLoginRequest(HttpServletRequest redirectRespn) {
|
||||||
String code = redirectRespn.getParameter("code");
|
String code = redirectRespn.getParameter("code");
|
||||||
System.out.println("인가코드 :: " + code);
|
logger.info("인가코드 :: " + code);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 카카오에 토큰값 요청
|
* 카카오에 토큰값 요청
|
||||||
@@ -58,7 +64,7 @@ public class LoginController {
|
|||||||
* @param refresh_token 사용자 리프레시 토큰
|
* @param refresh_token 사용자 리프레시 토큰
|
||||||
*/
|
*/
|
||||||
LoginResDTO loginResult = loginService.getToken(loginRequest);
|
LoginResDTO loginResult = loginService.getToken(loginRequest);
|
||||||
System.out.println("결과 :: " + loginResult.toString());
|
logger.info("토큰발급 결과 :: " + loginResult.toString());
|
||||||
|
|
||||||
UserInfoInqyReqDTO userInfoInqyRequest = new UserInfoInqyReqDTO();
|
UserInfoInqyReqDTO userInfoInqyRequest = new UserInfoInqyReqDTO();
|
||||||
userInfoInqyRequest.setAccess_token(loginResult.getAccess_token());
|
userInfoInqyRequest.setAccess_token(loginResult.getAccess_token());
|
||||||
@@ -68,18 +74,19 @@ public class LoginController {
|
|||||||
* @param access_token 사용자정보 조회용 토큰
|
* @param access_token 사용자정보 조회용 토큰
|
||||||
*/
|
*/
|
||||||
UserInfoInqyResDTO userInfoInqyResponse = loginService.getUserInfo(userInfoInqyRequest);
|
UserInfoInqyResDTO userInfoInqyResponse = loginService.getUserInfo(userInfoInqyRequest);
|
||||||
System.out.println("사용자정보 :: " + userInfoInqyResponse.toString());
|
logger.info("사용자정보 :: " + userInfoInqyResponse.toString());
|
||||||
|
|
||||||
ResponseCookie cookie = loginService.createJwtCookie(userInfoInqyResponse.getId());
|
ResponseCookie cookie = loginService.createJwtCookie(userInfoInqyResponse.getId());
|
||||||
|
|
||||||
Map<String, Object> response = new java.util.HashMap<>();
|
Map<String, String> response = new HashMap<>();
|
||||||
response.put("success", true);
|
response.put("success", "true");
|
||||||
response.put("userId", userInfoInqyResponse.getId());
|
response.put("userId", userInfoInqyResponse.getId());
|
||||||
response.put("message", "Login successful");
|
response.put("message", "Login successful");
|
||||||
response.put("userInfo", userInfoInqyResponse);
|
response.put("userInfo", userInfoInqyResponse.getName());
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.status(302)
|
||||||
.header(HttpHeaders.SET_COOKIE, cookie.toString())
|
.header(HttpHeaders.SET_COOKIE, cookie.toString())
|
||||||
|
.header(HttpHeaders.LOCATION, "http://localhost:3000/pages/oauth/callback")
|
||||||
.body(response);
|
.body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package site.ocr.prd.services;
|
|||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseCookie;
|
import org.springframework.http.ResponseCookie;
|
||||||
@@ -20,6 +22,9 @@ import site.ocr.prd.dto.UserInfoInqyResDTO;
|
|||||||
@Service
|
@Service
|
||||||
public class LoginService {
|
public class LoginService {
|
||||||
|
|
||||||
|
//logger 선언
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(LoginService.class);
|
||||||
|
|
||||||
//webclient builder
|
//webclient builder
|
||||||
private final WebClient webClient;
|
private final WebClient webClient;
|
||||||
//JWT provider
|
//JWT provider
|
||||||
@@ -31,7 +36,7 @@ public class LoginService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public LoginResDTO getToken(LoginReqDTO request) {
|
public LoginResDTO getToken(LoginReqDTO request) {
|
||||||
System.out.println("kakao auth code = {}" + request.getCode());
|
logger.info("kakao auth code :: " + request.getCode());
|
||||||
|
|
||||||
LoginResDTO result = webClient.post()
|
LoginResDTO result = webClient.post()
|
||||||
.uri("https://kauth.kakao.com/oauth/token")
|
.uri("https://kauth.kakao.com/oauth/token")
|
||||||
@@ -59,11 +64,11 @@ public class LoginService {
|
|||||||
.block();
|
.block();
|
||||||
|
|
||||||
//사용자ID
|
//사용자ID
|
||||||
|
//jsonnode :: {"id":4438121341,"connected_at":"2025-09-09T03:53:23Z"}
|
||||||
|
logger.info("jsonnode :: " + root.toString());
|
||||||
String id = root.path("id").asText();
|
String id = root.path("id").asText();
|
||||||
String name = root.path("name").asText();
|
String name = root.path("name").asText();
|
||||||
String email = root.path("email").asText();
|
String email = root.path("email").asText();
|
||||||
//jsonnode :: {"id":4438121341,"connected_at":"2025-09-09T03:53:23Z"}
|
|
||||||
System.out.println("jsonnode :: " + root.toString());
|
|
||||||
result.setId(id);
|
result.setId(id);
|
||||||
result.setName(name);
|
result.setName(name);
|
||||||
result.setEmail(email);
|
result.setEmail(email);
|
||||||
|
|||||||
Reference in New Issue
Block a user