認証なしでアクセス可能なページの設定
JavaScriptやCSS、画像など認証不要でアクセス可能なURLを指定する方法について。
cssや画像を取得できるようにする
mvc-config.xmlに以下の設定を追加する。
<mvc:resources mapping="/css/**" location="/css/" />
Spring Security でアクセス制限する場合、誰でもアクセス可能なページは、security-config.xml で設定する。
たとえば、アプリケーションコンテキストルートに対応するページは誰でもアクセス可能にするべきなので、以下のように設定する。
<sec:http pattern="/" security="none"/>
以下は security-config.xml の全体。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver" />
<beans:property name="url" value="jdbc:hsqldb:hsql://localhost/exam" />
<beans:property name="username" value="sa" />
<beans:property name="password" value="" />
</beans:bean>
<beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder" />
<sec:http pattern="/" security="none"/>
<sec:http pattern="/index.jsp" security="none"/>
<sec:http auto-config="true">
<intercept-url pattern="/**" access="ROLE_USER" />
</sec:http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="taro" password="abcd" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
Tomcatサーバーにアプリケーションコンテキストを追加できない場合
Tomcatサーバーを右クリックして「追加および除去」を選択し、ダイアログの中に動作させたいアプリケーションコンテキスト名が表示されていない場合は、以下の設定を確認する。
- プロジェクトを右クリックし、プロパティを開く。
- ダイアログの左で「プロジェクト・ファセット」を選択する。
- 「動的Webモジュール」の「バージョン」を「2.5」に変更する。
- 「動的Webモジュール」のチェックボックスをチェックする。
- ダイアログで「OK」する。
データベースを使った認証
現状では spring-security.xml に書いたユーザー名とパスワードで認証しているが、これをデータベースを使った認証に変更する。
USERSテーブルの準備
HSQLDBのデータベースマネージャを起動し、以下のSQLでテーブルを作成する。
CREATE TABLE users(
user_id bigint NOT NULL identity,
user_name varchar(50) NOT NULL,
password varchar(50) NOT NULL,
enabled tinyint NOT NULL,
role varchar(100) NOT NULL
);
テスト用のユーザーをUSERSテーブルに追加する
以下のSQLでユーザーを追加する。
INSERT INTO users ( user_name , password , enabled, role)
VALUES ( 'taro', 'abcd' , 1, 'ROLE_USER' );
spring-security.xmlの変更
spring-securoty.xml の以下の箇所でユーザー名とパスワードを指定している。
<authentication-manager>
<authentication-provider>
<user-service>
<user name="taro" password="abcd" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
この部分をデータベースのUSERSテーブルから取得するようにするために、以下のように修正する。
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="SELECT user_name, password, enabled FROM users WHERE user_name = ?"
authorities-by-username-query="SELECT user_name, role FROM users WHERE user_name = ?"
/>
</authentication-provider>
</authentication-manager>