메인 보내기
This commit is contained in:
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,10 +1,16 @@
|
|||||||
package site.ocr.prd.contorllers;
|
package site.ocr.prd.contorllers;
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.ResponseCookie;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
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.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import site.ocr.prd.components.JwtProvider;
|
||||||
import site.ocr.prd.dto.LoginReqDTO;
|
import site.ocr.prd.dto.LoginReqDTO;
|
||||||
import site.ocr.prd.dto.LoginResDTO;
|
import site.ocr.prd.dto.LoginResDTO;
|
||||||
import site.ocr.prd.dto.UserInfoInqyReqDTO;
|
import site.ocr.prd.dto.UserInfoInqyReqDTO;
|
||||||
@@ -18,13 +24,18 @@ public class LoginController {
|
|||||||
|
|
||||||
//service 선언
|
//service 선언
|
||||||
private LoginService loginService = new LoginService(WebClient.builder());
|
private LoginService loginService = new LoginService(WebClient.builder());
|
||||||
|
//JWT 선언
|
||||||
|
private final JwtProvider jwtProvider;
|
||||||
|
|
||||||
|
public LoginController(JwtProvider provider) {
|
||||||
|
this.jwtProvider = provider;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 프론트에서 카카오로 로그인 요청 후 카카오에서 리다이렉트 해준 인가코드로 토큰 발급 및 사용자정보 조회
|
* 프론트에서 카카오로 로그인 요청 후 카카오에서 리다이렉트 해준 인가코드로 토큰 발급 및 사용자정보 조회
|
||||||
* @param redirectRespn 카카오에서 리다이렉트해준 인가코드
|
* @param redirectRespn 카카오에서 리다이렉트해준 인가코드
|
||||||
*/
|
*/
|
||||||
@GetMapping("/oauth/oauth-kakao-authorize") //kakao에서 get으로 리다이렉트 해줌
|
@GetMapping("/oauth/oauth-kakao-authorize") //kakao에서 get으로 리다이렉트 해줌
|
||||||
public void kakaoLoginRequestDto(HttpServletRequest redirectRespn) {
|
public ResponseEntity kakaoLoginRequestDto(HttpServletRequest redirectRespn) {
|
||||||
String code = redirectRespn.getParameter("code");
|
String code = redirectRespn.getParameter("code");
|
||||||
System.out.println("인가코드 :: " + code);
|
System.out.println("인가코드 :: " + code);
|
||||||
|
|
||||||
@@ -53,12 +64,30 @@ public class LoginController {
|
|||||||
UserInfoInqyReqDTO userInfoInqyRequest = new UserInfoInqyReqDTO();
|
UserInfoInqyReqDTO userInfoInqyRequest = new UserInfoInqyReqDTO();
|
||||||
userInfoInqyRequest.setAccess_token(loginResult.getAccess_token());
|
userInfoInqyRequest.setAccess_token(loginResult.getAccess_token());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 사용자정보 조회
|
||||||
|
* @param access_token 사용자정보 조회용 토큰
|
||||||
|
*/
|
||||||
UserInfoInqyResDTO userInfoInqyResponse = loginService.getUserInfo(userInfoInqyRequest);
|
UserInfoInqyResDTO userInfoInqyResponse = loginService.getUserInfo(userInfoInqyRequest);
|
||||||
System.out.println("사용자정보 :: " + userInfoInqyResponse.toString());
|
System.out.println("사용자정보 :: " + userInfoInqyResponse.toString());
|
||||||
|
|
||||||
String jwt = loginService.createJwtToken(userInfoInqyResponse.getId());
|
/**
|
||||||
|
* 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();
|
||||||
|
|
||||||
|
return ResponseEntity.status(302)
|
||||||
|
.header(HttpHeaders.SET_COOKIE, cookie.toString())
|
||||||
|
.header(HttpHeaders.LOCATION, "http://localhost:3000/main")
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("login/oauth-kakao-token")
|
@GetMapping("login/oauth-kakao-token")
|
||||||
@@ -69,6 +98,4 @@ public class LoginController {
|
|||||||
|
|
||||||
return response.getAccess_token();
|
return response.getAccess_token();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,13 @@
|
|||||||
package site.ocr.prd.services;
|
package site.ocr.prd.services;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties.Jwt;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
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 com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
import io.jsonwebtoken.Jwts;
|
|
||||||
import io.jsonwebtoken.SignatureAlgorithm;
|
|
||||||
import io.jsonwebtoken.security.Keys;
|
|
||||||
import site.ocr.prd.dto.LoginReqDTO;
|
import site.ocr.prd.dto.LoginReqDTO;
|
||||||
import site.ocr.prd.dto.LoginResDTO;
|
import site.ocr.prd.dto.LoginResDTO;
|
||||||
import site.ocr.prd.dto.UserInfoInqyReqDTO;
|
import site.ocr.prd.dto.UserInfoInqyReqDTO;
|
||||||
@@ -26,10 +18,7 @@ public class LoginService {
|
|||||||
|
|
||||||
//webclient builder
|
//webclient builder
|
||||||
private final WebClient webClient;
|
private final WebClient webClient;
|
||||||
//jwt용 secret key
|
|
||||||
private final String secretKey = "very_secret_key";
|
|
||||||
//jwt토큰 유지 시간 = 1시간
|
|
||||||
private final long expireMillies = 1000 * 60 * 60;
|
|
||||||
|
|
||||||
|
|
||||||
public LoginService(WebClient.Builder builder) {
|
public LoginService(WebClient.Builder builder) {
|
||||||
@@ -76,18 +65,4 @@ public class LoginService {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String createJwtToken(String id) {
|
|
||||||
String result = Jwts.builder()
|
|
||||||
.setSubject(id)
|
|
||||||
.setIssuedAt(new Date())
|
|
||||||
.setExpiration(new Date(System.currentTimeMillis() + expireMillies))
|
|
||||||
.signWith(Keys.hmacShaKeyFor(secretKey.getBytes()),
|
|
||||||
SignatureAlgorithm.HS256)
|
|
||||||
.compact();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,3 +3,5 @@ spring:
|
|||||||
profiles:
|
profiles:
|
||||||
active:
|
active:
|
||||||
- DEV
|
- DEV
|
||||||
|
jwt:
|
||||||
|
secret: tOrtq6iI5i2Zjs83qRVzdhyd3D4WXjmcsNZ9Gljhr+dz7cUtLWlkD9shYdpgALCdXplEGJDJoyBeTCfY5Fwb3Q==
|
||||||
Reference in New Issue
Block a user