
spring securityでユーザの認証・認可をする方法はコチラでも紹介しましたが、以前の記事ではユーザ情報は設定ファイルに持たせていました。
今回はDBにユーザ情報を持たせておいて、認証と認可をする方法を紹介していきます。
このページでやること
Spring SecurityでDBのユーザ情報を基に認証・認可する方法を紹介します。
Spring Securityの設定
Spring Securityの基本的な設定方法はコチラのページを参考にしてください。
このページではDB接続の部分のみにフォーカスをあてて解説を進めていきます。
spring securityの基本的なひな型プロジェクトの作り方
pom.xml
Spring Securityを使うためdependencyはこの3つを指定して下さい。
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>4.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>4.0.1.RELEASE</version> </dependency>
DB接続するためにこちらも追加してください。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.2.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency>
DBへユーザ情報を登録
ユーザ情報は、ユーザ名・パスワード・権限を登録しておきます。
CREATE TABLE users ( name VARCHAR(255), password VARCHAR(255), authority VARCHAR(255) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO users (name, password, authority) values ('admin', 'admin', 'ROLE_ADMIN'); INSERT INTO users (name, password, authority) values ('user', 'user', 'ROLE_USER');
Spring Securityの設定ファイル
フォーカスされている部分がDBへ接続してユーザ情報と権限情報を取得してチェックしている部分です。
users-by-username-queryでユーザ情報をauthorities-by-username-queryで権限を取得しています。
ユーザ情報を取得するSQLは設定ファイル内へベタ書きしていきます。
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"> <http auto-config="true" > <!-- 認可の設定 --> <intercept-url pattern="/top*" access="hasAnyRole('ROLE_ADMIN', 'ROLE_USER')" /> <intercept-url pattern="/admin*" access="hasRole('ROLE_ADMIN')" /> <intercept-url pattern="/user*" access="hasRole('ROLE_USER')" /> <!-- 権限なし時の遷移先 --> <access-denied-handler error-page="/403" /> <!-- 認証のログイン処理 --> <form-login login-page="/" default-target-url="/top" authentication-failure-url="/error" login-processing-url="/j_spring_security_check"/> <!-- 認証のログアウト処理 --> <logout logout-url="/logout" logout-success-url="/" invalidate-session="true"/> <!-- anonymousユーザのROLE --> <anonymous granted-authority="ROLE_ANONYMOUS" /> </http> <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <beans:property name="driverClassName" value="org.gjt.mm.mysql.Driver" /> <beans:property name="url" value="jdbc:mysql://127.0.0.1:3306/test_db" /> <beans:property name="username" value="root" /> <beans:property name="password" value="root" /> </beans:bean> <authentication-manager alias="authenticationManager"> <authentication-provider> <!-- SQLでユーザ情報取得 --> <jdbc-user-service data-source-ref="dataSource" users-by-username-query=" select name as username, password as password, true as enabled from users where name=?" authorities-by-username-query=" select name as username, authority as authority from users where name=?"/> </authentication-provider> </authentication-manager> </beans:beans>
springとjavaのバージョン
springframeworknのバージョンは、4.0.1を使っています。
javaは、version 8を使っています。
サンプルコード
EclipseでGitHubからCloneしたら「プロジェクトを右クリック > Configure > Convert to maven project」をしてビルド&実行してください。
GitHubでサンプルソースを公開しています。
さいごに
ユーザ情報はほとんどのシステムではspringの設定に持たせるというのはあまり聞いたことがありません。ほとんどの場合はDBに持たせるものではないでしょうか。
spring securityではDBにユーザ情報を持ったとしても簡単な設定ひとつで認証処理が実現できるので便利でオススメです。
それでは!
この記事はお役に立てましたか?
お役に立てたことがあればシェアしていただけると嬉しいです!