JAX-WSによるアプリケーション認証
JAX-WSで認証を処理する一般的な方法の1つは、クライアントがSOAPリクエストヘッダに添付してサーバに送信し、サーバがSOAPドキュメントを解析し、提供された「ユーザ名」と「パスワード」を取得するという “要求ヘッダーから取得し、データベースからの検証、またはどのような方法でも好む。
この記事では、上記のJAX-WS ** アプリケーションレベル認証を実装する方法を説明します。
アイデア…
Map<String, Object> req__ctx = ((BindingProvider)port).getRequestContext();
req__ctx.put(BindingProvider.ENDPOINT__ADDRESS__PROPERTY, WS__URL);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("mkyong"));
headers.put("Password", Collections.singletonList("password"));
req__ctx.put(MessageContext.HTTP__REQUEST__HEADERS, headers);
-
Webサービスサーバー** サイトで、 `WebServiceContext`を介して要求ヘッダーパラメーターを取得します。
@Resource
WebServiceContext wsctx;
@Override
public String method() {
MessageContext mctx = wsctx.getMessageContext();
//get detail from request headers
Map http__headers = (Map) mctx.get(MessageContext.HTTP__REQUEST__HEADERS);
List userList = (List) http__headers.get("Username");
List passList = (List) http__headers.get("Password");
//...
つまり、配備されたJAX-WSはアプリケーションレベルの認証がサポートされています。
JAX-WSでの認証の例
完全な例を参照してください。
1. Webサービスサーバ
アプリケーションレベルでの認証を処理する簡単なJAX-WS hello worldの例を作成します。
File:HelloWorld.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 HelloWorld{
@WebMethod String getHelloWorldAsString();
}
HelloWorldImpl.java
package com.mkyong.ws;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
//Service Implementation Bean
@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Resource
WebServiceContext wsctx;
@Override
public String getHelloWorldAsString() {
MessageContext mctx = wsctx.getMessageContext();
//get detail from request headers
Map http__headers = (Map) mctx.get(MessageContext.HTTP__REQUEST__HEADERS);
List userList = (List) http__headers.get("Username");
List passList = (List) http__headers.get("Password");
String username = "";
String password = "";
if(userList!=null){
//get username
username = userList.get(0).toString();
}
if(passList!=null){
//get password
password = passList.get(0).toString();
}
//Should validate username and password with database
if (username.equals("mkyong") && password.equals("password")){
return "Hello World JAX-WS - Valid User!";
}else{
return "Unknown User!";
}
}
}
2.エンドポイントパブリッシャー
上記URLのWebサービスをデプロイするエンドポイント・パブリッシャを作成します。
ファイル:Hello World Publisher.java
package com.mkyong.endpoint;
import javax.xml.ws.Endpoint;
import com.mkyong.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloWorldPublisher{
public static void main(String[]args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}
3. WebServiceクライアント
認証のために「username」と「password」を送信するWebサービスクライアントを作成します。
ファイル:Hello World Client.java
package com.mkyong.client;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import com.mkyong.ws.HelloWorld;
public class HelloWorldClient{
private static final String WS__URL = "http://localhost:9999/ws/hello?wsdl";
public static void main(String[]args) throws Exception {
URL url = new URL(WS__URL);
QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** UserName & Password ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** / Map<String, Object> req__ctx = ((BindingProvider)hello).getRequestContext();
req__ctx.put(BindingProvider.ENDPOINT__ADDRESS__PROPERTY, WS__URL);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("mkyong"));
headers.put("Password", Collections.singletonList("password"));
req__ctx.put(MessageContext.HTTP__REQUEST__HEADERS, headers);
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** /
System.out.println(hello.getHelloWorldAsString());
}
}
出力
Hello World JAX-WS - Valid User!
4. SOAPトラフィックの追跡
上から順に、クライアントとサーバーの間のSOAPエンベロープの流れを示します。
{空} 1。クライアント送信要求、ユーザー名「
mkyong
」およびパスワード「
password
」がSOAPエンベロープに含まれています。
POST/ws/hello?wsdl HTTP/1.1
Password: password
Username: mkyong
SOAPAction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, ** ; q=.2, ** /** ; q=.2
Content-Type: text/xml; charset=utf-8
User-Agent: Java/1.6.0__13
Host: localhost:8888
Connection: keep-alive
Content-Length: 178
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getHelloWorldAsString xmlns:ns2="http://ws.mkyong.com/"/>
</S:Body>
</S:Envelope>
{空} 2。サーバーは正常な応答を返します。
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getHelloWorldAsStringResponse xmlns:ns2="http://ws.mkyong.com/">
<return>Hello World JAX-WS - Valid User!</return>
</ns2:getHelloWorldAsStringResponse>
</S:Body>
</S:Envelope>
完了しました。