본문 바로가기
Back-End/Spring Security

Spring Security Authentication (2) JDBC Authentication

by sharekim 2021. 3. 16.

Spring Security’s JdbcDaoImpl implements UserDetailsService to provide support for username/password based authentication that is retrieved using JDBC. 

스프링 시큐리티의 jdbcDaoImpl은 JDBC의 username/password 인증을 지원하기 위해 UserDetailService를 인터페이스로 구현한다. 

JdbcUserDetailsManager extends JdbcDaoImpl to provide management of UserDetails through the UserDetailsManager interface.

JdbcUserDetailsManager 는 JdbcDaoImpl을 상속 받는데, JdbcDaoImpl은 UserDetailsManager 인터페이스를 통해 UserDetail 관리를 제공한다

 

아래 세 가지 내용에 대해서 설명할 것이다.

  • The Default Schema used by Spring Security JDBC Authentication
    스프링 시큐리티 JDBC인증에 사용되는 기본 스키마 구조
  • Setting up a DataSource : DataSource 셋팅법
  • JdbcUserDetailsManager Bean

1. Default Schema

Spring Security provides default queries for JDBC based authentication. This section provides the corresponding default schemas used with the default queries. You will need to adjust the schema to match any customizations to the queries and the database dialect you are using.

스프링 시큐리티는 JDBC인증에 필요한 기본 쿼리들을 제공한다. 여기서는 기본쿼리에 해당하는 기본적인 스키마들을 제시한다. 입맛에 맞게 수정해서 써라.

#User Schema

JdbcDaoImpl requires tables to load the password, account status (enabled or disabled) and a list of authorities (roles) for the user. The default schema required can be found below.

JdbcDaoImpl은 비밀번호와 계정상태, 권한을 필요로 하며 기본 스키마는 아래와 같다.

 

Example 65. Default User Schema (MY-SQL)

create table users(
    username varchar_ignorecase(50) not null primary key,
    password varchar_ignorecase(500) not null,
    enabled boolean not null
);

create table authorities (
    username varchar_ignorecase(50) not null,
    authority varchar_ignorecase(50) not null,
    constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities (username,authority);

Example 66. Default User Schema for Oracle Databases

CREATE TABLE USERS (
    USERNAME NVARCHAR2(128) PRIMARY KEY,
    PASSWORD NVARCHAR2(128) NOT NULL,
    ENABLED CHAR(1) CHECK (ENABLED IN ('Y','N') ) NOT NULL
);


CREATE TABLE AUTHORITIES (
    USERNAME NVARCHAR2(128) NOT NULL,
    AUTHORITY NVARCHAR2(128) NOT NULL
);
ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_UNIQUE UNIQUE (USERNAME, AUTHORITY);
ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_FK1 FOREIGN KEY (USERNAME) REFERENCES USERS (USERNAME) ENABLE;

#Group Schema

어플리케이션이 레버리징 그룹이면 아래 그룹 스키마를 사용해라

 

Example 67. Default Group Schema

create table groups (
    id bigint generated by default as identity(start with 0) primary key,
    group_name varchar_ignorecase(50) not null
);

create table group_authorities (
    group_id bigint not null,
    authority varchar(50) not null,
    constraint fk_group_authorities_group foreign key(group_id) references groups(id)
);

create table group_members (
    id bigint generated by default as identity(start with 0) primary key,
    username varchar(50) not null,
    group_id bigint not null,
    constraint fk_group_members_group foreign key(group_id) references groups(id)
);

위 스키마는 아래와 같이 저장한다.

org/springframework/security/core/userdetails/jdbc/users.ddl.

2. Setting up a DataSource

Before we configure JdbcUserDetailsManager, we must create a DataSource. In our example, we will setup an embedded DataSource that is initialized with the default user schema.

JdbcUserDetailsManager를 설정하기 전에 DataSource를 반드시 만들어야 한다. 예시로 embedded DatSource를 셋팅해보자.

 

XML로 작성하면, 

<jdbc:embedded-database>
    <jdbc:script location="classpath:org/springframework/security/core/userdetails/jdbc/users.ddl"/>
</jdbc:embedded-database>

 

In a production environment, you will want to ensure you setup a connection to an external database.

생산적인 환경에서 외부 DB와의 연결 셋팅을 확실히 하고 싶을 것이다.

 

3. JdbcUserDetailsManager Bean

password를 password로 bcrypt encode 한 예시이다. (패스워드에다가 password 라는 패스워드를 셋팅했다는 이야기)

패스워드 저장방법에 대해서는 PasswordEncoder에서 자세히 소개하고 있으니 궁금하면 보시라.

<jdbc-user-service>
    <user name="user"
        password="{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW"
        authorities="ROLE_USER" />
    <user name="admin"
        password="{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW"
        authorities="ROLE_USER,ROLE_ADMIN" />
</jdbc-user-service>

 

 

 

여기까지가 문서 내용인데 이어지는 UserDetails를 읽고 구현해야 사용할 수 있을 것 같다.

다음 포스팅에서는 UserDetails에 대해서 알아보겠다.

 

 

 

* 모든 자료의 출처는 Spring.io 입니다.

댓글