Skip to content

Commit

Permalink
#46 - Feat: OpenBankingAccessToken 엔티티 설계, 오픈뱅킹 토큰 발급 후 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
ahah525 committed Dec 23, 2022
1 parent 4686dc3 commit 3fee762
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.example.mutbooks.app.openBanking.dto;

import com.example.mutbooks.app.openBanking.entity.OpenBankingAccessToken;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@SuperBuilder
Expand All @@ -16,4 +19,27 @@ public class OpenBankingAccessTokenDto {
private String expiresIn; // Access Token 만료 기간(초)
private String scope; // Access Token 권한 범위(고정값: oob)
private String clientUseCode; // 이용기관코드

public static OpenBankingAccessToken toEntity(OpenBankingAccessTokenDto accessTokenDto) {
LocalDateTime expireAt = LocalDateTime.now()
.plusSeconds(Integer.parseInt(accessTokenDto.getExpiresIn()));
return OpenBankingAccessToken.builder()
.accessToken(accessTokenDto.getAccessToken())
.tokenType(accessTokenDto.getTokenType())
.expiresIn(accessTokenDto.getExpiresIn())
.expireAt(expireAt)
.scope(accessTokenDto.getScope())
.clientUseCode(accessTokenDto.getClientUseCode())
.build();
}

public static OpenBankingAccessTokenDto toOpenBankingAccessTokenDto(OpenBankingAccessToken accessToken) {
return OpenBankingAccessTokenDto.builder()
.accessToken(accessToken.getAccessToken())
.tokenType(accessToken.getTokenType())
.expiresIn(accessToken.getExpiresIn())
.scope(accessToken.getScope())
.clientUseCode(accessToken.getClientUseCode())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.mutbooks.app.openBanking.entity;

import com.example.mutbooks.app.base.entity.BaseEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.persistence.Column;
import javax.persistence.Entity;
import java.time.LocalDateTime;

@Entity
@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
public class OpenBankingAccessToken extends BaseEntity {
@Column(nullable = false, length = 400) // 디폴트 길이는 255인데 토큰은 최대 400자이므로 꼭 설정해야함
private String accessToken; // 오픈뱅킹에서 발행된 Access Token
private String tokenType; // Access Token 유형(고정값: Bearer)
private String expiresIn; // Access Token 만료 기간(초)
private LocalDateTime expireAt; // Access Token 만료일시
private String scope; // Access Token 권한 범위(고정값: oob)
private String clientUseCode; // 이용기관코드
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.mutbooks.app.openBanking.repository;

import com.example.mutbooks.app.openBanking.entity.OpenBankingAccessToken;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDateTime;
import java.util.Optional;

public interface OpenBankingRepository extends JpaRepository<OpenBankingAccessToken, Long> {
// 만료기한이 현재일시 이후인 accessToken 1개 조회
Optional<OpenBankingAccessToken> findFirstByExpireAtAfter(LocalDateTime dateTime);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
package com.example.mutbooks.app.openBanking.service;

import com.example.mutbooks.app.openBanking.dto.OpenBankingAccessTokenDto;
import com.example.mutbooks.app.openBanking.entity.OpenBankingAccessToken;
import com.example.mutbooks.app.openBanking.repository.OpenBankingRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;


@Service
@RequiredArgsConstructor
@Slf4j
public class OpenBankingService {
private final RestTemplate restTemplate;
// private final OpenBankingRepository openBankingRepository;
private final OpenBankingRepository openBankingRepository;

@Value("${openapi.openBanking.client_id}")
String clientId;
private String clientId;

@Value("${openapi.openBanking.client_secret}")
String clientSecret;
private String clientSecret;

@Transactional
public OpenBankingAccessTokenDto genAccessToken() {
// 만료되지 않은 AccessToken 이 있는지 검사
OpenBankingAccessToken accessToken = openBankingRepository.findFirstByExpireAtAfter(LocalDateTime.now()).orElse(null);
if(accessToken != null) {
return OpenBankingAccessTokenDto.toOpenBankingAccessTokenDto(accessToken);
}
// 없으면 새로 발급
String url = "https://testapi.openbanking.or.kr/oauth/2.0/token";
// header 생성
Expand All @@ -43,10 +50,18 @@ public OpenBankingAccessTokenDto genAccessToken() {
// header + body 로 request 생성
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
// post 요청 보내기(url, request(헤더 + 파라미터 정보), response 타입)
OpenBankingAccessTokenDto accessTokenDto = restTemplate.postForObject(
ResponseEntity<OpenBankingAccessTokenDto> responseEntity = restTemplate.postForEntity(
url,
request,
OpenBankingAccessTokenDto.class);
return accessTokenDto;
OpenBankingAccessTokenDto newAccessTokenDto = responseEntity.getBody();
// 토큰발급 성공시 DB 에 토큰 저장
if(responseEntity.getStatusCode() == HttpStatus.OK) {
if(newAccessTokenDto.getAccessToken() != null) {
OpenBankingAccessToken newAccessToken = OpenBankingAccessTokenDto.toEntity(newAccessTokenDto);
openBankingRepository.save(newAccessToken);
}
}
return newAccessTokenDto;
}
}
}

0 comments on commit 3fee762

Please sign in to comment.