[OAuth 2.0] OAuth Client 테이블 설계(비밀 클라이언트 기준)

테이블 설계

 

1. oauth_clients

  • 이 테이블은 클라이언트를 식별하기 위한 테이블

 

2. oauth_client_redirect_uris

 

3. oauth_client_scopes

  • 이 테이블은 클라이언트 권한

 

더미 데이터 SQL

insert into oauth_clients (client_id, is_active, client_secret, client_name)
values ('test-client', true, '{noop}secret', 'Test Client');

insert into oauth_client_redirect_uris (client_id, redirect_uri)
values ('test-client', '<http://localhost:8081/callback>');

insert into oauth_client_scopes (client_id, scope)
values ('test-client', 'read'),
       ('test-client', 'write');

 

JPA로 테이블 만들기

1. OauthClients

@Entity
@Table(name = "oauth_clients")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class OauthClients {
    @Id
    @Column(name = "client_id", length = 100)
    private String clientId;

    @Column(name = "client_secret", nullable = false, length = 200)
    private String clientSecret;

    @Column(name = "client_name", nullable = false, length = 100)
    private String clientName;

    @Column(name = "is_active", nullable = false)
    private boolean active = true;

    public OauthClients(String clientId, String clientSecret, String clientName) {
        this.clientId = clientId;
        this.clientSecret = clientSecret;
        this.clientName = clientName;
    }
}

 

2. OauthClientRedirectUri

@Entity
@Table(
        name = "oauth_client_redirect_uris",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = {"client_id", "redirect_uri"})
        }
)
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class OauthClientRedirectUri {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long redirectUrisId;

    @ManyToOne
    @JoinColumn(name = "client_id", nullable = false)
    private OauthClients client;

    @Column(name = "redirect_uri", nullable = false, columnDefinition = "text")
    private String redirectUri;

    public OauthClientRedirectUri(OauthClients client, String redirectUri) {
        this.client = client;
        this.redirectUri = redirectUri;
    }
}
  • columnDefinition → 길이 제한 없는 문자열

 

3. OauthClientScopes

@Entity
@Table(
        name = "oauth_client_scopes",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = {"client_id", "scope"})
        }
)
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class OauthClientScopes {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long scopesId;

    @ManyToOne
    @JoinColumn(name = "client_id", nullable = false)
    private OauthClients client;

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

    public OauthClientScopes(OauthClients client, String scope) {
        this.client = client;
        this.scope = scope;
    }
}