jwt start
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
package site.ocr.prd.contorllers;
|
package site.ocr.prd.contorllers;
|
||||||
|
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 org.springframework.web.bind.annotation.RestController;
|
|
||||||
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;
|
||||||
@@ -14,7 +13,7 @@ import site.ocr.prd.services.LoginService;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
@Controller
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
|
|
||||||
//service 선언
|
//service 선언
|
||||||
@@ -57,6 +56,7 @@ public class LoginController {
|
|||||||
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());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
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;
|
||||||
@@ -10,6 +13,9 @@ 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 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;
|
||||||
@@ -18,7 +24,13 @@ import site.ocr.prd.dto.UserInfoInqyResDTO;
|
|||||||
@Service
|
@Service
|
||||||
public class LoginService {
|
public class LoginService {
|
||||||
|
|
||||||
|
//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) {
|
||||||
this.webClient = builder.build();
|
this.webClient = builder.build();
|
||||||
@@ -65,5 +77,17 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user