人気ブログランキング | 話題のタグを見る
2006年 08月 02日
BASIC認証
BASIC認証が設定されているWEBサービスのAxis2からの呼び出しを確認してみました。
以下のテストでは Axis2 1.0 Release ではなく Nightly Build を使っています。

●versionサービスを呼び出すクライアント
 まずはBASIC認証が設定されていない状態でversionサービスを呼び出す
 クライアントの動作を確認します。
 以下クライアントのソースです。

package test;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class testVersion {

public static void main(String[] args) throws Exception {

Options options = new Options();

options.setTo(new EndpointReference("http://localhost:8080/axis2/services/version"));

ServiceClient serviceClient = new ServiceClient();
serviceClient.setOptions(options);

// Get factory
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://axisversion.sample/xsd", "ns");
// Create request message
OMElement req = fac.createOMElement("getVersion", omNs);
req.setText("hello, how are you ?");

// call service
OMElement res = serviceClient.sendReceive(req);
System.out.println(res.toString());
}
}

 実行結果は以下のようになります。

<ns:getVersionResponse xmlns:ns="http://axisversion.sample/xsd">
<ns:return>Hello I am Axis2 version service , My version is SNAPSHOT Jul 21, 2006 (01:20:55 GMT+00:00)</ns:return>
</ns:getVersionResponse>


●BASIC認証を設定する
 ここで、Webサービス側にBASIC認証を設定します。
 (Tomcat)/webapps/axis2/WEB-INF/web.xml に以下の設定を追加し、Tomcatを再起動します。
 ここでは admin ユーザーでアクセスできるような設定としています。

<!-- Security is active on entire directory -->
<security-constraint>
<display-name>Axis2 Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>/rest/*</url-pattern>
<url-pattern>/servlet/AxisServlet</url-pattern>
<url-pattern>*.jws</url-pattern>
<url-pattern>/services/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>

<!-- Define the Login Configuration for this Application -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Axis2 Application</realm-name>
</login-config>

<!-- Security roles referenced by this web application -->
<security-role>
<description>
The role that is required to log in to the Administration Application
</description>
<role-name>admin</role-name>
</security-role>

 この状態でversionサービスに http://localhost:8080/axis2/rest/version/getVersion
 RESTでアクセスしてみると、下図のようにログインを求められます。
BASIC認証_f0091228_2124887.gif

 adminユーザーでログインするとREST呼び出しが成功します。

●BASIC認証のWebサービスの呼び出し
 ここで、先ほどのversionサービスクライアントを実行すると次のようなエラーとなりました。

2006/08/02 1:56:48 org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
情報: basic authentication scheme selected
2006/08/02 1:56:48 org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
情報: No credentials available for BASIC 'Axis2 Application'@localhost:8080
org.apache.axis2.AxisFault: Axis2 transport error : Unauthorized; nested exception is:
org.apache.axis2.AxisFault: Axis2 transport error : Unauthorized; nested exception is:
org.apache.axis2.AxisFault: Axis2 transport error : Unauthorized; nested exception is:
org.apache.axis2.AxisFault: Axis2 transport error : Unauthorized
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:222)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:651)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:344)
at org.apache.axis2.description.OutInAxisOperationClient.execute(OutInAxisOperation.java:280)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:538)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:472)
at test.testVersion.main(testVersion.java:30)
Caused by: org.apache.axis2.AxisFault: Axis2 transport error : Unauthorized; nested exception is:
org.apache.axis2.AxisFault: Axis2 transport error : Unauthorized
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:307)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:206)
... 6 more
Caused by: org.apache.axis2.AxisFault: Axis2 transport error : Unauthorized
at org.apache.axis2.transport.http.SOAPOverHTTPSender.send(SOAPOverHTTPSender.java:134)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:301)
... 7 more
Exception in thread "main"


●BASIC認証のWebサービスに対応させたクライアント
 ここで、versionサービスのクライアントを以下のように変更すると
 エラーなくサービスを呼び出せることが確認できました。

package test;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;

public class testVersionAuth {

public static void main(String[] args) throws Exception {

Options options = new Options();

options.setTo(new EndpointReference("http://localhost:8080/axis2/services/version"));

// Add basic authentication
HttpTransportProperties.BasicAuthentication basicAuth
= new HttpTransportProperties().new BasicAuthentication();
basicAuth.setUsername("admin");
basicAuth.setPassword("admin");
options.setProperty(HTTPConstants.BASIC_AUTHENTICATION, basicAuth);

ServiceClient serviceClient = new ServiceClient();
serviceClient.setOptions(options);

// Get factory
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://axisversion.sample/xsd", "ns");
// Create request message
OMElement req = fac.createOMElement("getVersion", omNs);
req.setText("hello, how are you ?");

// call service
OMElement res = serviceClient.sendReceive(req);
System.out.println(res.toString());
}
}


●versionクライアントのリクエストを参照
 TCPMONを使ってリクエストを参照してみると、以下のように "Authorization" が
 content header に追加されていることが分かります。
 参考:TCPMON 1.0

POST /axis2/services/version HTTP/1.1
SOAPAction: ""
User-Agent: Axis2
Authorization: Basic YWRtaW46YWRtaW4=
Host: 127.0.0.1:8081
Transfer-Encoding: chunked
Content-Type: text/xml; charset=UTF-8

110
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header />
<soapenv:Body>
<ns:getVersion xmlns:ns="http://axisversion.sample/xsd">hello, how are you ?</ns:getVersion>
</soapenv:Body>
</soapenv:Envelope>0

by est2006 | 2006-08-02 02:20 | Axis2


<< Transfer-Encodi... Apache Axis2 次の... >>