2011-08-03 44 views
0

WS-Securityを使用するMetroを使用してWebサービスクライアントを作成しようとしています。Java Metroでユーザー名/パスワードを指定する方法は?

私はAxis2の使用している、とのAxis2クライアントにユーザ名/パスワードを指定するには、私が行います

org.apache.axis2.client.ServiceClient sc = stub._getServiceClient(); 
org.apache.axis2.client.Options options = sc.getOptions(); 
options.setUserName("USERNAME"); 
options.setPassword("PASSWORD"); 

は、どのように私はメトロクライアントにユーザ名/パスワードを提供していますか?

答えて

4

あなたは基本的なHTTPヘッダ使用のauthしたい場合は、次のサービスがNTLM(Windows認証)(説明here)を使用している場合

@WebEndpoint(name = "WSHttpBinding_ICustomerService") 
public ICustomerService getWSHttpBindingICustomerService() { 
    WebServiceFeature wsAddressing = new AddressingFeature(true); 

    ICustomerService service = 
     super.getPort(new QName("http://xmlns.example.com/services/Customer", 
       "WSHttpBinding_ICustomerService"), ICustomerService.class, 
       wsAddressing); 

    Map<String, Object> context = ((BindingProvider)service).getRequestContext(); 

    Map<String, List<String>> headers = new HashMap<String, List<String>>(); 
    headers.put("Username", Collections.singletonList("yourusername")); 
    headers.put("Password", Collections.singletonList("yourpassword")); 

    return service; 
} 

を:

@WebEndpoint(name = "WSHttpBinding_ICustomerService") 
public ICustomerService getWSHttpBindingICustomerService() { 
    WebServiceFeature wsAddressing = new AddressingFeature(true); 

    ICustomerService service = 
     super.getPort(new QName("http://xmlns.example.com/services/Customer", 
       "WSHttpBinding_ICustomerService"), ICustomerService.class, 
       wsAddressing); 

    NtlmAuthenticator auth = new NtlmAuthenticator(username, password); 
    Authenticator.setDefault(auth); 

    return service; 
} 

はこれを使用していない自分が、しかし、他の使用を見て:

@WebEndpoint(name = "WSHttpBinding_ICustomerService") 
public ICustomerService getWSHttpBindingICustomerService() { 
    WebServiceFeature wsAddressing = new AddressingFeature(true); 

    ICustomerService service = 
     super.getPort(new QName("http://xmlns.example.com/services/Customer", 
       "WSHttpBinding_ICustomerService"), ICustomerService.class, 
       wsAddressing); 

    Map<String, Object> context = ((BindingProvider)service).getRequestContext(); 

    context.put(BindingProvider.USERNAME_PROPERTY, "yourusername"); 
    context.put(BindingProvider.PASSWORD_PROPERTY, "yourpassword"); 

    return service; 
} 
+0

ありがとう!最初の例の変数ヘッダーは設定されていますが使用されていません。欠けているものがあります。 –

関連する問題