JAX-WSによるコンテナ認証 – (Tomcat版)
この記事では、Tomcat 6.0の下でJAX-WS ** でコンテナ認証を実装する方法を説明します。このように、認証はプログラムのように宣言的ではなく、 – webservices/jax-ws/application-authentication-with-jax-ws/[JAX-WSのアプリケーション認証]のように宣言されています。 Tomcatはhttp://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html[security realm]を介してコンテナ認証を実装します。
この記事の最後で、デプロイされたWebサービスは、Tomcatの `conf/tomcat-users.xml`ファイルに格納されている認証データに基づいてユーザーを認証します。
1. WebService
シンプルなJAX-WS、RPCスタイルを作成します。
File:UserProfile.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.RPC) public interface UserProfile{ @WebMethod String getUserName(); }
File:UserProfileImpl.java
package com.mkyong.ws; import javax.jws.WebService; //Service Implementation Bean @WebService(endpointInterface = "com.mkyong.ws.UserProfile") public class UserProfileImpl implements UserProfile{ @Override public String getUserName() { return "getUserName() : returned value"; } }
2. web.xml
セキュリティロール “演算子”を設定し、url “/user”に必要な基本的なhttp認証を行います。後述の `web.xml`ファイルを参照してください。
ファイル:web.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app__2__3.dtd"> <web-app> //... <security-role> <description>Normal operator user</description> <role-name>operator</role-name> </security-role> <security-constraint> <web-resource-collection> <web-resource-name>Operator Roles Security</web-resource-name> <url-pattern>/user</url-pattern> </web-resource-collection> <auth-constraint> <role-name>operator</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> </login-config> <servlet-mapping> <servlet-name>user</servlet-name> <url-pattern>/user</url-pattern> </servlet-mapping> //... </web-app>
<user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint>
この記事を参照してください –
SSLまたはhttps接続をサポートするようにTomcatを作成する
3. Tomcatユーザー
`$ Tomcat/conf/tomcat-users.xml`ファイルに新しいロール、ユーザ名、パスワードを追加してください。この場合、新しいユーザ “mkyong”、 “123456”を追加し、それを “operator”という名前のロールに添付します。
File:$ Tomcat/conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="operator"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="mkyong" password="123456" roles="operator"/> <user name="admin" password="admin" roles="admin,manager"/> </tomcat-users>
4. Tomcat Realm
$ Tomcat/conf/server.xml`ファイルにセキュリティ領域を設定します。この場合、デフォルトの `UserDatabaseRealm`を使って
$ Tomcat/conf/tomcat-users.xml`の認証情報を読み込みます。
File:$ Tomcat/conf/server.xml
<GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml"/> </GlobalNamingResources> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
5. TomcatにJAX-WS Webサービスをデプロイする
この詳細については、webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/[TomcatにJAX-WS Webサービスを配備する方法]を参照してください。
6.テスト
ここで、デプロイされたWebサービスへのアクセスには、ユーザー名とパスワードの認証が必要です(図:+
URL:http://localhost:8080/WebServiceExample/user
7. WebServiceクライアント
デプロイされたWebサービスにアクセスするには、次のように正しいユーザー名とパスワードをバインドします。
UserProfile port = service.getPort(UserProfile.class); BindingProvider bp = (BindingProvider) port; bp.getRequestContext().put(BindingProvider.USERNAME__PROPERTY, "mkyong"); bp.getRequestContext().put(BindingProvider.PASSWORD__PROPERTY, "123456");
File:WsClient.java
package com.mkyong.client; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import javax.xml.ws.Service; import com.mkyong.ws.UserProfile; public class WsClient{ //can't parse wsdl "http://localhost:8080/WebServiceExample/user.wsdl" directly //save it as local file, and parse it private static final String WS__URL = "file:c://user.wsdl"; public static void main(String[]args) throws Exception { URL url = new URL(WS__URL); QName qname = new QName("http://ws.mkyong.com/", "UserProfileImplService"); Service service = Service.create(url, qname); UserProfile port = service.getPort(UserProfile.class); //add username and password for container authentication BindingProvider bp = (BindingProvider) port; bp.getRequestContext().put(BindingProvider.USERNAME__PROPERTY, "mkyong"); bp.getRequestContext().put(BindingProvider.PASSWORD__PROPERTY, "123456"); System.out.println(port.getUserName()); } }
出力
getUserName() : returned value
-
注意** 無効なユーザ名またはパスワードを提供しているクライアントの場合、Tomcatは次の例外を返します。
Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: request requires HTTP authentication: Unauthorized
完了しました。
ソースコードをダウンロードする
ダウンロード – リンク://wp-content/uploads/2010/12/JAX-WS-Container-Authentication-Example.zip[JAX-WS-Container-Authentication-Example.zip](11KB)
リファレンス
-
http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html
[Tomcat realm
JAX-WSによる認証]。
http://www.ryandelaplante.com/2007/06/ssl-and-http-basic-authentication-with.html
[SSL
GlassfishとJAX-WSとのHTTP基本認証]