Compare commits
17 Commits
030d3c4ceb
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8fe6a32ede | |||
|
|
fee9b12395 | ||
|
|
5fd0c03560 | ||
| 89d1acf5c9 | |||
| 6a2405a782 | |||
|
|
d9211cc12b | ||
| d8af2f71c6 | |||
|
|
074d4c8d63 | ||
| a92d31e371 | |||
|
|
e54c219b76 | ||
| 8d9d75e372 | |||
|
|
692c2392fd | ||
|
|
7e40126da4 | ||
| cb7f468d95 | |||
|
|
b504ac0f93 | ||
|
|
6a0bc5e38d | ||
|
|
d59df7a223 |
2
prd/.gitignore
vendored
2
prd/.gitignore
vendored
@@ -34,4 +34,4 @@ out/
|
|||||||
/.nb-gradle/
|
/.nb-gradle/
|
||||||
|
|
||||||
### VS Code ###
|
### VS Code ###
|
||||||
.vscode/
|
.vscode/*
|
||||||
|
|||||||
0
prd/gradlew
vendored
Normal file → Executable file
0
prd/gradlew
vendored
Normal file → Executable file
37
prd/src/main/java/site/ocr/prd/components/JwtProvider.java
Normal file
37
prd/src/main/java/site/ocr/prd/components/JwtProvider.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package site.ocr.prd.components;
|
||||||
|
|
||||||
|
import java.security.Key;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
import io.jsonwebtoken.io.Decoders;
|
||||||
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class JwtProvider {
|
||||||
|
|
||||||
|
// jwt용 secret key
|
||||||
|
private final Key key;
|
||||||
|
// jwt토큰 유지 시간 = 1시간
|
||||||
|
private final long expireMillies = 1000 * 60 * 60;
|
||||||
|
|
||||||
|
public JwtProvider(@Value("${jwt.secret}")String secrest) {
|
||||||
|
byte[] ketByte = Decoders.BASE64.decode(secrest);
|
||||||
|
this.key = Keys.hmacShaKeyFor(ketByte);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String createJwtToken(String id) {
|
||||||
|
String result = Jwts.builder()
|
||||||
|
.setSubject(id)
|
||||||
|
.setIssuedAt(new Date())
|
||||||
|
.setExpiration(new Date(System.currentTimeMillis() + expireMillies))
|
||||||
|
.signWith(key, SignatureAlgorithm.HS256)
|
||||||
|
.compact();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package site.ocr.prd;
|
package site.ocr.prd.config;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -22,11 +22,14 @@ public class SecurityConfig {
|
|||||||
configuration.setAllowedOrigins(List.of("http://localhost:3000"));
|
configuration.setAllowedOrigins(List.of("http://localhost:3000"));
|
||||||
configuration.setAllowedMethods(List.of("GET","POST", "PUT","DELETE","OPTION"));
|
configuration.setAllowedMethods(List.of("GET","POST", "PUT","DELETE","OPTION"));
|
||||||
configuration.setAllowedHeaders(List.of("*"));
|
configuration.setAllowedHeaders(List.of("*"));
|
||||||
|
configuration.setAllowCredentials(true);
|
||||||
return configuration;
|
return configuration;
|
||||||
};
|
};
|
||||||
cors.configurationSource(configurationSource);
|
cors.configurationSource(configurationSource);
|
||||||
})
|
})
|
||||||
.csrf(AbstractHttpConfigurer::disable); //대체 언제 이런 문법이 생겼냐;
|
.csrf(AbstractHttpConfigurer::disable) //대체 언제 이런 문법이 생겼냐;
|
||||||
|
.authorizeHttpRequests(auth -> auth
|
||||||
|
.anyRequest().permitAll()); //외부 요청은 필터가 다 거르는 형태였기때문에 허용 해줘야함
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
package site.ocr.prd;
|
package site.ocr.prd.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@@ -13,4 +14,8 @@ public class WebClientConfig {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package site.ocr.prd;
|
package site.ocr.prd.config;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
import site.ocr.prd.SessionLoggingInterceptor;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebMVCConfig implements WebMvcConfigurer {
|
public class WebMVCConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
@@ -1,18 +1,50 @@
|
|||||||
package site.ocr.prd.contorllers;
|
package site.ocr.prd.contorllers;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestPart;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class ImgController {
|
public class ImgController {
|
||||||
|
|
||||||
@PostMapping("img/get-ocr")
|
private final RestTemplate restTemplate;
|
||||||
public String postGetImgOcr(@RequestBody String entity) {
|
|
||||||
//TODO: process POST request
|
|
||||||
|
|
||||||
return entity;
|
@Autowired
|
||||||
|
public ImgController(RestTemplate restTemplate) {
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("img/get-img")
|
||||||
|
public ResponseEntity<String> postGetImg(@RequestPart("image") MultipartFile image) {
|
||||||
|
System.out.println("image :: " + image.getResource().getFilename());
|
||||||
|
|
||||||
|
//헤더 설정(이미지)
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||||
|
|
||||||
|
//바디에 이미지 파일 추가
|
||||||
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
body.add("image", image.getResource());
|
||||||
|
|
||||||
|
//요청 엔티티 생성
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||||
|
|
||||||
|
//요청 전송
|
||||||
|
ResponseEntity<String> response =
|
||||||
|
restTemplate.postForEntity("http://127.0.0.1:9002/ocr",
|
||||||
|
requestEntity, String.class);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(response.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,112 @@
|
|||||||
package site.ocr.prd.contorllers;
|
package site.ocr.prd.contorllers;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import java.util.Map;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.slf4j.Logger;
|
||||||
|
import java.util.HashMap;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import site.ocr.prd.dto.LoginRequestDto;
|
|
||||||
import site.ocr.prd.dto.LoginResponseDto;
|
|
||||||
import site.ocr.prd.services.LoginService;
|
|
||||||
|
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.ResponseCookie;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import site.ocr.prd.dto.LoginReqDTO;
|
||||||
|
import site.ocr.prd.dto.LoginResDTO;
|
||||||
|
import site.ocr.prd.dto.UserInfoInqyReqDTO;
|
||||||
|
import site.ocr.prd.dto.UserInfoInqyResDTO;
|
||||||
|
import site.ocr.prd.dto.UserInfoResDTO;
|
||||||
|
import site.ocr.prd.services.LoginService;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
|
|
||||||
|
@Controller
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
|
|
||||||
|
//Logger 선언
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
|
||||||
|
|
||||||
//service 선언
|
//service 선언
|
||||||
private LoginService loginService = new LoginService(WebClient.builder());
|
private final LoginService loginService;
|
||||||
|
|
||||||
@GetMapping("login/oauth-kakao-authorize") //kakao에서 get으로 요청 보냄
|
public LoginController(LoginService loginService) {
|
||||||
public void kakaoLoginRequestDto(HttpServletRequest request) {
|
this.loginService = loginService;
|
||||||
String accessToken = request.getParameter("code");
|
}
|
||||||
System.out.println(accessToken);
|
/**
|
||||||
LoginRequestDto requestDTO = new LoginRequestDto();
|
* 프론트에서 카카오로 로그인 요청 후 카카오에서 리다이렉트 해준 인가코드로 토큰 발급 및 사용자정보 조회
|
||||||
requestDTO.setGrant_type("authorization_code");
|
* @param redirectRespn 카카오에서 리다이렉트해준 인가코드
|
||||||
requestDTO.setClient_id("a1d6afef2d4508a10a498b7069f67496");
|
*/
|
||||||
requestDTO.setRedirect_uri("http://localhost:9001/login/oauth-kakao-authorize");
|
@GetMapping("/oauth/oauth-kakao-authorize") //kakao에서 get으로 리다이렉트 해줌
|
||||||
requestDTO.setCode(accessToken);
|
public ResponseEntity<Map<String, String>> kakaoLoginRequest(HttpServletRequest redirectRespn) {
|
||||||
|
String code = redirectRespn.getParameter("code");
|
||||||
|
logger.info("인가코드 :: " + code);
|
||||||
|
|
||||||
LoginResponseDto result = loginService.getToken(requestDTO);
|
/**
|
||||||
System.out.println(result.toString());
|
* 카카오에 토큰값 요청
|
||||||
|
* requestDTO
|
||||||
|
* @param grant_type
|
||||||
|
* @param client_id 카카오콘솔의 app key
|
||||||
|
* @param code 인가코드
|
||||||
|
*/
|
||||||
|
LoginReqDTO loginRequest = new LoginReqDTO();
|
||||||
|
loginRequest.setGrant_type("authorization_code");
|
||||||
|
loginRequest.setClient_id("a1d6afef2d4508a10a498b7069f67496");
|
||||||
|
loginRequest.setRedirect_uri("http://localhost:9001/oauth/oauth-kakao-authorize");
|
||||||
|
loginRequest.setCode(code);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 카카오로부터 받은 토큰값
|
||||||
|
* @param access_token 사용자정보 조회용 토큰
|
||||||
|
* @param expires_in 토큰 유효시간(초)
|
||||||
|
* @param refresh_token 사용자 리프레시 토큰
|
||||||
|
*/
|
||||||
|
LoginResDTO loginResult = loginService.getToken(loginRequest);
|
||||||
|
logger.info("토큰발급 결과 :: " + loginResult.toString());
|
||||||
|
|
||||||
|
UserInfoInqyReqDTO userInfoInqyRequest = new UserInfoInqyReqDTO();
|
||||||
|
userInfoInqyRequest.setAccess_token(loginResult.getAccess_token());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 사용자정보 조회
|
||||||
|
* @param access_token 사용자정보 조회용 토큰
|
||||||
|
*/
|
||||||
|
UserInfoInqyResDTO userInfoInqyResponse = loginService.getUserInfo(userInfoInqyRequest);
|
||||||
|
logger.info("사용자정보 :: " + userInfoInqyResponse.toString());
|
||||||
|
|
||||||
|
ResponseCookie cookie = loginService.createJwtCookie(userInfoInqyResponse.getId());
|
||||||
|
|
||||||
|
Map<String, String> response = new HashMap<>();
|
||||||
|
response.put("success", "true");
|
||||||
|
response.put("userId", userInfoInqyResponse.getId());
|
||||||
|
response.put("message", "Login successful");
|
||||||
|
response.put("userInfo", userInfoInqyResponse.getName());
|
||||||
|
|
||||||
|
return ResponseEntity.status(302)
|
||||||
|
.header(HttpHeaders.SET_COOKIE, cookie.toString())
|
||||||
|
.header(HttpHeaders.LOCATION, "http://localhost:3000/pages/oauth/callback")
|
||||||
|
.body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("login/oauth-kakao-token")
|
/**
|
||||||
public String kakaoLoginResponseDto(@RequestBody LoginResponseDto response) {
|
* JWT 토큰 조회 (프론트에서 로그인 상태 확인용)
|
||||||
|
* @param userId 사용자 ID
|
||||||
|
* @return 로그인 결과 및 JWT 토큰
|
||||||
|
*/
|
||||||
|
@GetMapping("/oauth/get-jwt-token")
|
||||||
|
public ResponseEntity<Map<String, String>> getJwtToken(@RequestParam String 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")
|
||||||
|
public String kakaoLoginResponseDto(@RequestBody LoginResDTO response) {
|
||||||
//TODO: process POST request
|
//TODO: process POST request
|
||||||
System.out.println("response :: ");
|
System.out.println("response :: ");
|
||||||
System.out.println(response.getAccess_token());
|
System.out.println(response.getAccess_token());
|
||||||
@@ -43,5 +114,11 @@ public class LoginController {
|
|||||||
return response.getAccess_token();
|
return response.getAccess_token();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("login/get-user-info")
|
||||||
|
public ResponseEntity<UserInfoResDTO> getUserInfo(@RequestParam HttpServletRequest request) {
|
||||||
|
UserInfoResDTO result = new UserInfoResDTO();
|
||||||
|
|
||||||
|
return ResponseEntity.status(200).build();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,13 +9,17 @@ import lombok.ToString;
|
|||||||
@ToString
|
@ToString
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class LoginRequestDto {
|
public class LoginReqDTO {
|
||||||
|
|
||||||
@JsonProperty("grant_type")
|
@JsonProperty("grant_type")
|
||||||
String grant_type; //authorization_code
|
String grant_type; //authorization_code
|
||||||
|
|
||||||
@JsonProperty("client_id")
|
@JsonProperty("client_id")
|
||||||
String client_id; //앱 REST API 키
|
String client_id; //앱 REST API 키
|
||||||
|
|
||||||
@JsonProperty("redirect_uri")
|
@JsonProperty("redirect_uri")
|
||||||
String redirect_uri; //인가코드가 리다이렉트된 uri
|
String redirect_uri; //인가코드가 리다이렉트된 uri
|
||||||
|
|
||||||
|
@JsonProperty("code")
|
||||||
String code; //인가코드 요청으로 얻은 인가코드
|
String code; //인가코드 요청으로 얻은 인가코드
|
||||||
}
|
}
|
||||||
@@ -9,19 +9,25 @@ import lombok.ToString;
|
|||||||
@ToString
|
@ToString
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class LoginResponseDto {
|
public class LoginResDTO {
|
||||||
|
|
||||||
@JsonProperty("token_type")
|
@JsonProperty("token_type")
|
||||||
String token_type; //토큰타입, bearer 고정
|
String token_type; //토큰타입, bearer 고정
|
||||||
|
|
||||||
@JsonProperty("access_token")
|
@JsonProperty("access_token")
|
||||||
String access_token; //사용자 엑세스 토큰 값
|
String access_token; //사용자 엑세스 토큰 값
|
||||||
|
|
||||||
@JsonProperty("id_token")
|
@JsonProperty("id_token")
|
||||||
String id_token; //ID 토큰값, openID connect가 활성화된 경우만 발급
|
String id_token; //ID 토큰값, openID connect가 활성화된 경우만 발급
|
||||||
|
|
||||||
@JsonProperty("expires_in")
|
@JsonProperty("expires_in")
|
||||||
Integer expires_in; //엑세스토큰과 id토큰의 만료시간(초))
|
Integer expires_in; //엑세스토큰과 id토큰의 만료시간(초))
|
||||||
|
|
||||||
@JsonProperty("refresh_token")
|
@JsonProperty("refresh_token")
|
||||||
String refresh_token; //사용자 리프레시 토큰 값
|
String refresh_token; //사용자 리프레시 토큰 값
|
||||||
|
|
||||||
@JsonProperty("refresh_token_expires_in")
|
@JsonProperty("refresh_token_expires_in")
|
||||||
Integer refresh_token_expires_in; //리프레시 토큰 만료 시간(초)
|
Integer refresh_token_expires_in; //리프레시 토큰 만료 시간(초)
|
||||||
|
|
||||||
String scope; //인증된 사용자의 정보조회 범위, 여러개일 경우 공백으로 구분
|
String scope; //인증된 사용자의 정보조회 범위, 여러개일 경우 공백으로 구분
|
||||||
}
|
}
|
||||||
16
prd/src/main/java/site/ocr/prd/dto/UserInfoInqyReqDTO.java
Normal file
16
prd/src/main/java/site/ocr/prd/dto/UserInfoInqyReqDTO.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package site.ocr.prd.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserInfoInqyReqDTO {
|
||||||
|
|
||||||
|
@JsonProperty("access_token")
|
||||||
|
String access_token;
|
||||||
|
}
|
||||||
17
prd/src/main/java/site/ocr/prd/dto/UserInfoInqyResDTO.java
Normal file
17
prd/src/main/java/site/ocr/prd/dto/UserInfoInqyResDTO.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package site.ocr.prd.dto;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserInfoInqyResDTO {
|
||||||
|
String id;
|
||||||
|
String name;
|
||||||
|
String email;
|
||||||
|
String age_range;
|
||||||
|
String birthday;
|
||||||
|
String gender;
|
||||||
|
}
|
||||||
13
prd/src/main/java/site/ocr/prd/dto/UserInfoResDTO.java
Normal file
13
prd/src/main/java/site/ocr/prd/dto/UserInfoResDTO.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package site.ocr.prd.dto;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserInfoResDTO {
|
||||||
|
String userId;
|
||||||
|
String userName;
|
||||||
|
}
|
||||||
@@ -1,39 +1,93 @@
|
|||||||
package site.ocr.prd.services;
|
package site.ocr.prd.services;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseCookie;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.reactive.function.BodyInserters;
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
|
|
||||||
import site.ocr.prd.dto.LoginRequestDto;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import site.ocr.prd.dto.LoginResponseDto;
|
|
||||||
|
import site.ocr.prd.components.JwtProvider;
|
||||||
|
import site.ocr.prd.dto.LoginReqDTO;
|
||||||
|
import site.ocr.prd.dto.LoginResDTO;
|
||||||
|
import site.ocr.prd.dto.UserInfoInqyReqDTO;
|
||||||
|
import site.ocr.prd.dto.UserInfoInqyResDTO;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class LoginService {
|
public class LoginService {
|
||||||
|
|
||||||
private final WebClient webClient;
|
//logger 선언
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(LoginService.class);
|
||||||
|
|
||||||
public LoginService(WebClient.Builder builder) {
|
//webclient builder
|
||||||
|
private final WebClient webClient;
|
||||||
|
//JWT provider
|
||||||
|
private final JwtProvider jwtProvider;
|
||||||
|
|
||||||
|
public LoginService(WebClient.Builder builder, JwtProvider jwtProvider) {
|
||||||
this.webClient = builder.build();
|
this.webClient = builder.build();
|
||||||
|
this.jwtProvider = jwtProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoginResponseDto getToken(LoginRequestDto request) {
|
public LoginResDTO getToken(LoginReqDTO request) {
|
||||||
LoginResponseDto result = webClient.post()
|
logger.info("kakao auth code :: " + request.getCode());
|
||||||
|
|
||||||
|
LoginResDTO result = webClient.post()
|
||||||
.uri("https://kauth.kakao.com/oauth/token")
|
.uri("https://kauth.kakao.com/oauth/token")
|
||||||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=utf-8")
|
||||||
.body(BodyInserters.fromFormData("grant_type", "authorization_code")
|
.body(BodyInserters.fromFormData("grant_type", "authorization_code")
|
||||||
.with("client_id", "a1d6afef2d4508a10a498b7069f67496")
|
.with("client_id", "a1d6afef2d4508a10a498b7069f67496")
|
||||||
.with("redirect_uri", "http://localhost:9001/login/oauth-kakao-authorize")
|
.with("redirect_uri", "http://localhost:9001/oauth/oauth-kakao-authorize")
|
||||||
.with("code", request.getCode()))
|
.with("code", request.getCode()))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(LoginResponseDto.class)
|
.bodyToMono(LoginResDTO.class)
|
||||||
//.bodyToMono(String.class)
|
|
||||||
.block();
|
.block();
|
||||||
//System.out.println("요청 ::: " + request.toString());
|
|
||||||
//System.out.println("성공여부 ::: " + "result");
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UserInfoInqyResDTO getUserInfo(UserInfoInqyReqDTO request) {
|
||||||
|
|
||||||
|
// 응답 DTO
|
||||||
|
UserInfoInqyResDTO result = new UserInfoInqyResDTO();
|
||||||
|
|
||||||
|
JsonNode root = webClient.get()
|
||||||
|
.uri("https://kapi.kakao.com/v2/user/me")
|
||||||
|
.header(HttpHeaders.AUTHORIZATION, "Bearer " + request.getAccess_token())
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(JsonNode.class)
|
||||||
|
.block();
|
||||||
|
|
||||||
|
//사용자ID
|
||||||
|
//jsonnode :: {"id":4438121341,"connected_at":"2025-09-09T03:53:23Z"}
|
||||||
|
logger.info("jsonnode :: " + root.toString());
|
||||||
|
String id = root.path("id").asText();
|
||||||
|
String name = root.path("name").asText();
|
||||||
|
String email = root.path("email").asText();
|
||||||
|
result.setId(id);
|
||||||
|
result.setName(name);
|
||||||
|
result.setEmail(email);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JWT 토큰을 생성하고 ResponseCookie로 변환
|
||||||
|
* @param userId 사용자 ID
|
||||||
|
* @return ResponseCookie JWT 토큰이 포함된 쿠키
|
||||||
|
*/
|
||||||
|
public ResponseCookie createJwtCookie(String userId) {
|
||||||
|
String jwt = jwtProvider.createJwtToken(userId);
|
||||||
|
return ResponseCookie.from("accessToken", jwt)
|
||||||
|
.httpOnly(true)
|
||||||
|
.secure(true)
|
||||||
|
.sameSite("Lax")
|
||||||
|
.maxAge(Duration.ofHours(1))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,3 +8,4 @@ server:
|
|||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
root: debug
|
root: debug
|
||||||
|
org.springframework.security: DEBUG
|
||||||
|
|||||||
@@ -3,3 +3,5 @@ spring:
|
|||||||
profiles:
|
profiles:
|
||||||
active:
|
active:
|
||||||
- DEV
|
- DEV
|
||||||
|
jwt:
|
||||||
|
secret: tOrtq6iI5i2Zjs83qRVzdhyd3D4WXjmcsNZ9Gljhr+dz7cUtLWlkD9shYdpgALCdXplEGJDJoyBeTCfY5Fwb3Q==
|
||||||
Reference in New Issue
Block a user