Skip to main content

토큰 저장

1. 시작하며

Swit API를 사용하기 위해서는 기본적으로 토큰이 필요 합니다. 자세한 사항은 Swit Developers의 OAuth flow 를 참조해 주세요

2. 토큰의 저장

  • 본 예제에서는 Access token과 Refresh token을 데이터 베이스에 저장하도록 하겠습니다.
  • 먼저 Token을 담을 객체를 작성하겠습니다.

3. 토큰 객체 생성

3.1. SwitTokenEntity

package io.swit.api.model.entity.eai;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Data
@Table(name = "swit_token")
@NoArgsConstructor(access = AccessLevel.PUBLIC)
public class SwitTokenEntity {

    @Id
    @Column(name = "token_id", nullable = false, length = 30)
    private String tokenId = "SWIT";

    @Column(name = "access_token", length = 500, nullable = false)
    private String accessToken;

    @Column(name = "refresh_token", length = 500, nullable = false)
    private String refreshToken;

    @Column(name = "expire_in", nullable = true)
    private Long expireIn;

    @Column(name = "scope", length = 100, nullable = true)
    private String scope;

    @Column(name = "token_type", length = 50, nullable = false)
    private String tokenType;

}

3.2. SwitTokenDto

package io.swit.api.model.dto.eai;

import io.swit.api.model.entity.eai.SwitTokenEntity;
import lombok.Data;

@Data
public class SwitTokenDto {
    /** 토큰 아이디 */
    private String tokenId;

    /** 접근 토큰 */
    private String accessToken;

    /** 재인증 토큰 */
    private String refreshToken;

    /** 만료시간 */
    private Long expireIn;

    /** 인증 점위 */
    private String scope;

    /** 토큰 타입 */
    private String tokenType;



    /**
     * 엔티티 변환
     * @return 스윗 유저 토큰 엔티티
     */
    public SwitTokenEntity toEntity() {
        SwitTokenEntity entity = new SwitTokenEntity();
        entity.setTokenId(tokenId);
        entity.setAccessToken(this.accessToken);
        entity.setRefreshToken(this.refreshToken);
        entity.setExpireIn(this.expireIn);
        entity.setScope(this.scope);
        entity.setTokenType(this.tokenType);
        return entity;
    }
}