JDBC 이용한 인증/인가 처리

생성한 테이블은 사용자를 관리하는 테이블(users)과 관리하는 테이블 2개로 구성되어 있다.

✅ 데이터베이스 테이블 준비

create table users(
    username varchar2(50) not null,
    password varchar2(50) not null,
    enabled char(1) default '1' null,
    constraint pk_users primary key(username)
);

create table authorities(
    username varchar2(50) not null,
    authority varchar2(50) not null,
    constraint fk_authorities_users_username foreign key(username)
        references users(username)
);

insert into users values('user00', '1234', '1');
insert into users values('member00', '1234', '1');
insert into users values('admin00', '1234', '1');

insert into authorities values ('user00', 'ROLE_USER');
insert into authorities values ('member00', 'ROLE_MEMBER');
insert into authorities values ('admin00', 'ROLE_MEMBER');
insert into authorities values ('admin00', 'ROLE_ADMIN');

✅ 환경설정

→ 데이터베이스 관련 라이브러리를 추가한다.

→ 기존 데이터베이스 연결을 위한 라이브러리가 추가되어 있다. (SecurityTest 프로젝트안에는 없기 때문에 새롭게 등록)

✅ 스프링 설정

→ 데이터 소스 설정(기존 설정)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="<http://www.springframework.org/schema/beans>"
	xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>"
	xsi:schemaLocation="<http://www.springframework.org/schema/beans> <https://www.springframework.org/schema/beans/spring-beans.xsd>">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	
		 <!-- dataSource : 데이터베이스와 관련된 정보를 설정한다. -->
	 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
	 	<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
	 	<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
	 	<property name="username" value="dev"/>
	 	<property name="password" value="java"/>
	 </bean>
</beans>

✅ 스프링 시큐리티 설정

→ customPasswordEncoder 빈 등록

<security:authentication-manager> 태그 내에 설정