トッカンソフトウェア

Spring Security データベースログイン認証

前回はSpring Securityが持つ一番簡易な形でログイン認証機能を行いました。
今回はログイン情報をテーブルより取得してログイン認証を行ってみます。データベースはPostgreSQLを使用します。

PostgreSQLはこちらを参照下さい。

前回から手を入れるファイルのみ記述するので、記述がないファイルは前回を参照してください。


m_userテーブル

PostgeSQLのところでサンプルに作ったテーブルですが、そのまま今回使います。
				
create table public.m_user (
  id character varying(8) not null
  , name character varying(16)
  , primary key (id)
);


			

pom.xml

PostgreSQLのDBアクセス関連を追加しています。
				
<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>springMVC</groupId>
	<artifactId>springMVC</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<properties>
		<java.version>1.8</java.version>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<maven.compiler.source>${java.version}</maven.compiler.source>
	</properties>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<release>17</release>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.3</version>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.3.24</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-core</artifactId>
			<version>5.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>5.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-config</artifactId>
			<version>5.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>5.3.24</version>
		</dependency>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>42.5.1</version>
		</dependency>
	</dependencies>
</project>

			

applicationContext.xml

PosgreSQLの接続情報を追加しています。
				
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

	<context:component-scan base-package="spring.test" />

	<mvc:annotation-driven />
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="org.postgresql.Driver" />
		<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
		<property name="username" value="postgres" />
		<property name="password" value="ps" />
	</bean>
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>


			

WebSecurityConfig.java

PostgreSQLよりログイン情報を取得する処理に変更しています。
				
package spring.test;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
public class WebSecurityConfig {

	@Autowired
	private DataSource dataSource;

	/**
	 * テスト用PasswordEncoder 暗号化なし
	 * 
	 * @return PasswordEncoder
	 */
	 
	@Bean
	public PasswordEncoder passwordEncoder() {
		return NoOpPasswordEncoder.getInstance();
	}

	@Autowired
	public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

		String authoritySql = "select id as username, 'USER' as authority from m_user "
						 + " where id = ?";
		String passwordSql = "select id as username, name as password, true as enabled from m_user "
						 + " where id = ?";

		auth.jdbcAuthentication().dataSource(dataSource).authoritiesByUsernameQuery(authoritySql)
				.usersByUsernameQuery(passwordSql);
	}
}


			
本来は、権限もデータベースより取得すべきですが、サンプルでm_userテーブルしか用意しなかったので、権限を無理やり取得するようにしています。

username、authority、password、enabledは項目名を上記にする必要があります。

PasswordEncoderはNoOpPasswordEncoderを使っており、これは暗号化なしです。
動作確認以外は、BCryptPasswordEncoderなど暗号化を行うエンコーダを使ってください。


ページのトップへ戻る