JBoss5でユーザー認証(BASIC認証・DIGEST認証・FORM認証)を行う

WEBアプリでサーバーにユーザ認証をまかせるとき、TomcatJBossとではサーバー側の設定が少し異なる。

JBossのバージョン情報:
Name: JBoss SOA-P 5 (development)
Version: 5.3.0.GA
Description: JBoss Enterprise SOA Platform



設定すべきファイルは以下の4つ

JBoss側の設定ファイル
1.sample-ds.xml(sample部は任意)
 datasouce(例えば、データベース)への接続方法を記述
2.login-config.xml
 1.のdatasourceを利用した認証方法を記述
WEBアプリ側の設定ファイル
3.jboss-web.xml
 2.との接続を記述
4.web.xml
 認証方法を記述


JBoss側の設定

1.sample-ds.xml(sample部は任意)
$JBOSS_HOME/server/development/deploysample-ds.xml を作成

<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: mysql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ -->
<!--  Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->

<datasources>
  <local-tx-datasource>
    <jndi-name>sampleDS</jndi-name>
    <connection-url>jdbc:mysql://mysqldb:3306/sample?autoReconnect=true</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>user</user-name>
    <password>pass</password>
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>

jdbc:mysql://mysqldb:3306/sample?autoReconnect=true」はデータベースのurl



2.login-config.xml
$JBOSS_HOME/server/default/conf/login-config.xml に追記

<application-policy name = "sampleAuth">
  <authentication>
    <login-module code = "org.jboss.security.auth.spi.DatabaseServerLoginModule"
      flag = "required">
      <module-option name = "dsJndiName">java:/sampleDS</module-option>
      <module-option name="principalsQuery">
        select password from user_info where user_name=?
      </module-option>
      <module-option name="rolesQuery">
	 select role , 'Roles' from user_info where user_name=?
      </module-option>
    </login-module>
  </authentication>
</application-policy>

user,passのデータベース作成については下部の参考2を参照(参考2中のrolesQueryの文法に誤りがあるので注意)
テーブルの構成に応じてクエリは変更が必要


WEBアプリ側の設定

3.jboss-web.xml
/WEB-INF/jboss-web.xml を作成

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <!-- Use the JaasSecurityMgr other security domain for authentication
      and authorization of secured web content.
     -->
    <security-domain>java:/jaas/sampleAuth</security-domain>
</jboss-web>

sampleAuthは2.で設定したJNDI

4.web.xml
/WEB-INF/web.xml に追記

<security-constraint>
  <web-resource-collection>
    <web-resource-name>Protected Area</web-resource-name>
    <url-pattern>/</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name>user</role-name>
  </auth-constraint>
</security-constraint>


<login-config>
  <auth-method>FORM</auth-method>
  <realm-name>User Form Auth</realm-name>
  <form-login-config>
    <form-login-page>/login.html</form-login-page>
    <form-error-page>/error.html</form-error-page>
  </form-login-config>
</login-config>

<security-role>
  <role-name>user</role-name>
</security-role>

他の認証方式(BASIC認証・DIGEST認証)を利用したい時はauth-methodを変更



参考にしたサイト:
参考1https://access.redhat.com/documentation/ja-JP/JBoss_Enterprise_Application_Platform/5/html/Security_Guide/ch12.html
参考2JBoss各種Tips