11.4. 認証
いくつかの利用可能なメソッドを用いてWeb サービスのユーザーを認証する方法については、本章を参照してください。
タスク:Web サービスユーザーの認証
タスクの概要
以下の手順にて、JBossWS を用いて Web サービスユーザーを認証する方法について説明しています。
ステートレスセション Bean へのセキュアなアクセス
@RolesAllowed
、@PermitAll
、@DenyAll
を用いステートレスセッション Bean にセキュアな状態でアクセス認証済みのユーザーにbean クラスおよびビジネスメソッドのいずれかにおいて上記のアノテーションを用いてロール設定が可能です。@Stateless @RolesAllowed("friend") public class EndpointEJB implements EndpointInterface { ... }
POJO エンドポイントのセキュリティ確保
Secure Plain Old Java Object (POJO) エンドポイント (アプリケーションのWEB-INF/web.xml
ファイルで<security-constraint> を定義します)。<auth-constraint> <role-name> 要素は認証が必須かどうかを指定します。 <role-name> 要素にアスタリスクの値を指定することで "not required" に設定することができます。<security-constraint> <web-resource-collection> <web-resource-name>All resources</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>friend</role-name> </auth-constraint> </security-constraint> <security-role> <role-name>friend</role-name> </security-role>
EJB3 エンドポイントに対するセキュリティードメインの定義
@SecurityDomain アノテーションを追加することでセキュリティードメインを宣言します。@Stateless @SecurityDomain("JBossWS") @RolesAllowed("friend") public class EndpointEJB implements EndpointInterface { ... }
- また、
JBOSS_HOME/server/PROFILE/deploy/jbossws.sar/jboss-management.war/WEB-INF/jboss-web.xml
を変更し、セキュリティドメインを指定することもできます。<jboss-web> <security-domain>JBossWS</security-domain> </jboss-web>
注記
セキュリティドメインの詳細情報については、『JBoss セキュリティガイド』を参照してください。POJO エンドポイントに対するセキュリティードメインの定義
JBOSS_HOME/server/PROFILE/deploy/jbossws.sar/jboss-management.war/WEB-INF/jboss-web.xml
を変更し、セキュリティドメインを指定します。<jboss-web> <security-domain>JBossWS</security-domain> </jboss-web>
セキュリティコンテキストの定義
JBOSS_HOME/server/PROFILE/conf/login-config.xml
ファイルでセキュリティコンテキストを定義します。<!-- A template configuration for the JBossWS security domain. This defaults to the UsersRolesLoginModule the same as other and should be changed to a stronger authentication mechanism as required. --> <application-policy name="JBossWS"> <authentication> <login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule" flag="required"> <module-option name="usersProperties">props/jbossws-users.properties</module-option> <module-option name="rolesProperties">props/jbossws-roles.properties</module-option> <module-option name="unauthenticatedIdentity">anonymous</module-option> </login-module> </authentication> </application-policy>
注記
デフォルトのJBOSS_HOME/server/PROFILE/conf/login-config.xml
は、貴社のデプロイメントに適したセキュリティを提供する別のログインモジュールに変更する必要があります。LdapLoginModule を使ったユーザー認証制御の方法については、タスク:LDAP認証の有効化 に従ってください。EJB3 エンドポイントに対して HTTP の基本的な認証を定義
Bean クラスで @WebContext アノテーションを使用します。@Stateless @SecurityDomain("JBossWS") @RolesAllowed("friend") @WebContext(contextRoot="/my-cxt", urlPattern="/*", authMethod="BASIC", transportGuarantee="NONE", secureWSDLAccess=false) public class EndpointEJB implements EndpointInterface { ... }
POJO エンドポイントに対して HTTP の基本的な認証を定義
使用している Web アプリケーションの WEB-INF/web.xml に追加<login-config> <auth-method>BASIC</auth-method> <realm-name>Test Realm</realm-name> </login-config>
クライアントサイド - ユーザー名とパスワードを設定
Web サービスクライアントはjavax.xml.ws.BindingProvider
インターフェースを利用し、ユーザー名やパスワードの組み合わせを設定できます。URL wsdlURL = new File("resources/jaxws/samples/context/WEB-INF/wsdl/TestEndpoint.wsdl").toURL(); QName qname = new QName("http://org.jboss.ws/jaxws/context", "TestEndpointService"); Service service = Service.create(wsdlURL, qname); port = (TestEndpoint)service.getPort(TestEndpoint.class); BindingProvider bp = (BindingProvider)port; bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "jsmith"); bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "PaSSw0rd");
クライアントサイド - WSDL によるセキュリティー保護
java.net.Authenticator を使用して wsdl ファイルへのアクセス時にユーザー名とパスワードを設定します。Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username,password.toCharArray()); } }); Service service = Service.create(wsdlURL, qname);
タスク:LDAP認証の有効化
タスクの概要
このタスクに従い JBossWS アプリケーションに対し Lightweight Directory Access Protocol (LDAP)認証を設定します。『JBoss セキュリティガイド』の記載どおりLdapLoginModuleを使います。
初期設定は タスク:Web サービスユーザーの認証 と同様です。
ステートレスセション Bean へのセキュアなアクセス
@RolesAllowed
、@PermitAll
、@DenyAll
を用いステートレスセッション Bean にセキュアな状態でアクセス認証済みのユーザーにbean クラスおよびビジネスメソッドのいずれかにおいて上記のアノテーションを用いてロール設定が可能です。@Stateless @RolesAllowed("friend") public class EndpointEJB implements EndpointInterface { ... }
POJO エンドポイントのセキュリティ確保
同アプリケーションのWEB-INF/web.xml
ファイルにある<security-constraint> を定義して、Plain Old Java Object (POJO) エンドポイントのセキュリティを確保<auth-constraint> <role-name> 要素は認証が必須かを指定します。 <role-name> 要素にアスタリスク (*) 値を指定することで"not required" を設定することができます。<security-constraint> <web-resource-collection> <web-resource-name>All resources</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>*</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>JbossWS</realm-name> </login-config>
注記
有効な<auth-method> 値の詳細情報は、『JBoss Security Guide』の 「Web コンテンツのセキュリティ制限」の項を参照してください。セキュリティドメインの定義
@SecurityDomain アノテーションを追加することでセキュリティードメインを宣言します。@Stateless @SecurityDomain("JBossWS") @RolesAllowed("friend") public class EndpointEJB implements EndpointInterface { ... }
- また、
JBOSS_HOME/server/PROFILE/deploy/jbossws.sar/jboss-management.war/WEB-INF/jboss-web.xml
を変更し、セキュリティドメインを指定することもできます。<jboss-web> <security-domain>JBossWS</security-domain> </jboss-web>
注記
セキュリティドメインの詳細情報については、『JBoss セキュリティガイド』を参照してください。セキュリティコンテキストの定義
JBOSS_HOME/server/PROFILE/conf/login-config.xml
ファイルでセキュリティコンテキストを定義します。<application-policy name="JBossWS"> <authentication> <login-module code="org.jboss.security.auth.spi.LdapLoginModule" flag="required"> <module-option name="java.naming.factory.initial">com.sun.jndi.ldap.LdapCtxFactory</module-option> <module-option name="java.naming.provider.url">ldap://ldaphost.jboss.org:1389/</module-option> <module-option name="java.naming.security.authentication">simple</module-option> <module-option name="principalDNPrefix">uid=</module-option> <module-option name="principalDNSuffix">,ou=People,dc=jboss,dc=org</module-option> <module-option name="rolesCtxDN">ou=Roles,dc=jboss,dc=org</module-option> <module-option name="uidAttributeID">member</module-option> <module-option name="matchOnUserDN">true</module-option> <module-option name="roleAttributeID">cn</module-option> <module-option name="roleAttributeIsDN">false </module-option> </login-module> </authentication> </application-policy>
注記
LdapLoginModule やその他の利用可能なログインモジュールに関する情報はセキュリティガイドを参照してください。