2013-05-22 1 views
1

CXF에서 로컬 wsdl 파일을 사용하여 생성 한 클라이언트가 있습니다. 클라이언트가 OK를 연결하면 웹 서버에서 401 오류가 발생합니다.선점 형 HTTP 인증을 위해 CXF 생성 클라이언트를 어떻게 구성합니까?

내가 만난 문제는 클라이언트에서 선매 (preemptive) 인증을 올바르게 구성 할 수 없다는 것입니다.

저는 여러 가지 시도를 해 보았습니다. 웹에있는 대부분의 예제는 Spring에 초점을 맞추는 것처럼 보입니다.

나는 클라이언트의 주요 부분을 포함하고 있습니다. 아무도 나에게 이것이 어떻게 구성되어야하는지에 대한 예를들 수 있다면, 나는 그것을 고맙게 생각할 것이다. 내가 멋진 것을 찾는 것이 아니라는 점에 유의하십시오. 서비스를 인증하고 호출 할 수 있어야합니다.

public final class ServiceNowSoap_ServiceNowSoap_Client { 

private static final QName SERVICE_NAME = new QName(
     "http://www.service-now.com/foo", 
     "ServiceNow_foo"); 

private ServiceNowSoap_ServiceNowSoap_Client() { 
} 

public static void main(String args[]) throws java.lang.Exception { 
    URL wsdlURL = ServiceNowCmdbCiComm.WSDL_LOCATION; 
    if (args.length > 0 && args[0] != null && !"".equals(args[0])) { 
     File wsdlFile = new File(args[0]); 
     try { 
      if (wsdlFile.exists()) { 
       wsdlURL = wsdlFile.toURI().toURL(); 
      } else { 
       wsdlURL = new URL(args[0]); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
    } 

    ServiceNowFoo ss = new ServiceNowFoo(wsdlURL, 
      SERVICE_NAME); 
    ServiceNowSoap port = ss.getServiceNowSoap(); 

    { 
     System.out.println("Invoking deleteRecord..."); 
     java.lang.String _deleteRecord_sysId = ""; 
     java.lang.String _deleteRecord__return = port 
       .deleteRecord(_deleteRecord_sysId); 
     System.out.println("deleteRecord.result=" + _deleteRecord__return); 

    } 
    System.exit(0); 
} 

} 

답변

4

그래, 알아 냈어. 아주 간단합니다. 희망에 따라 몇 분 정도의 시간을 절약 할 수 있습니다.

import java.io.File; 
import java.net.MalformedURLException; 
import java.net.URL; 
import javax.xml.namespace.QName; 

import org.apache.cxf.endpoint.Client; 
import org.apache.cxf.frontend.ClientProxy; 
import org.apache.cxf.transport.http.HTTPConduit; 

private static final QName SERVICE_NAME = new QName(
     "http://www.service-now.com/foo", 
     "ServiceNow_foo"); 

private ServiceNowSoap_ServiceNowSoap_Client() { 
} 

public static void main(String args[]) throws java.lang.Exception { 
    URL wsdlURL = ServiceNowFoo.WSDL_LOCATION; 
    if (args.length > 0 && args[0] != null && !"".equals(args[0])) { 
     File wsdlFile = new File(args[0]); 
     try { 
      if (wsdlFile.exists()) { 
       wsdlURL = wsdlFile.toURI().toURL(); 
      } else { 
       wsdlURL = new URL(args[0]); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
    } 

    ServiceNowFoo ss = new ServiceNowFoo(wsdlURL, 
      SERVICE_NAME); 

    ServiceNowSoap port = ss.getServiceNowSoap(); 

    Client client = ClientProxy.getClient(port); 
    HTTPConduit http = (HTTPConduit) client.getConduit(); 
    http.getAuthorization().setUserName("theusername"); 
    http.getAuthorization().setPassword("thepassword"); 
0

인터셉터를 사용할 수도 있습니다. 인터셉터를 사용하면 얻을 수있는 이점 중 하나는 모든 클라이언트에 첨부 할 수 있으므로 선제 적 인증 방식이 능률화됩니다. 이 작품 않는, 어떻게 웹 서비스를 호출 한 후

How do i modify HTTP headers for a JAX-WS response in CXF?

0

안녕 친구가 구성한 인증 부분 : 를보세요?

Client client = ClientProxy.getClient(port); 
HTTPConduit http = (HTTPConduit) client.getConduit(); 
http.getAuthorization().setUserName("theusername");    
http.getAuthorization().setPassword("thepassword");   
4

또 다른 aproach는 다음과 같습니다

import javax.xml.ws.BindingProvider; 

public class CxfClientExample { 

    public static void main(String[] args) throws Exception { 
     String endPointAddress = "http://www.service-now.com/foo"; 
     ServiceNowFoo service = new ServiceNowFoo(); 
     ServiceNowFooPortType port = service.getServiceNowFoo(); 

     BindingProvider bindingProvider = (BindingProvider) port; 
     bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endPointAddress); 
     bindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "theusername"); 
     bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "thepassword"); 

     String deleteRecord_return = port.deleteRecord(""); 
     System.out.println("deleteRecord.result=" + deleteRecord_return);  
    } 
} 
관련 문제