webservice访问增加权限认证Authorization

对方给了一个WSDL文件,我生成了JAVA代码,创建了一个测试类。执行报错401.

public class TestApplicatin {

    private final static QName serviceName = new QName("xxxx", "MyService");
    
    public static void main(String[] args) throws Exception {
        MyService factory = new MyService();
        
        //增加Authorization
        URL wsdlUrl = factory.getWSDLDocumentLocation();
        Service service = Service.create(wsdlUrl, serviceName);
        MyInterface port = service.getPort(MyInterface.class);
        Client client = ClientProxy.getClient(port);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
        authorizationPolicy.setUserName("username");
        authorizationPolicy.setPassword("password");
        authorizationPolicy.setAuthorizationType("Basic");
        http.setAuthorization(authorizationPolicy);
        
        // 传参
        MyInterface myInterface = factory.getHTTPPort();
        Bean1 dt = new Bean1();
        Bean1.REQUEST s = new REQUEST();
        Bean_item1 eq = new Bean_item1();
        eq.setREQSYSEVRI("D");
        eq.setREQTARSYS("S4DCLNT300");
        eq.setREQSRCSYS("HUAXIN");
        eq.setREQSENDTIME("20211223151856");
        s.baseinfo = eq;
        String count = "5";
        s.message = new MESSAGE();
        s.message.setCOUNT(count);
        dt.setREQUEST(s);
        //调用接口
        Bean2 sp = myInterface.getBean2(dt);
        String id = sp.response.baseinforsp.getRSPREQTRACEID();
        System.out.println("id:"+id);
    }
}

还缺少什么相关代码? 请指导一下

把报错贴出来


2021-12-24 11:53:34.952 [main] WARN  o.a.cxf.phase.PhaseInterceptorChain - Interceptor for {urn:.i040.mmgetbarcode}MyService#{urn:i040.mmgetbarcode}MyInterface has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:518)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:427)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:328)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:281)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:139)
    at com.sun.proxy.$Proxy32.siI040MMGETBARCODEOUT(Unknown Source)
    at com.dhc.ilead.main.ws.TestApplication.main(TestApplication.java:61)
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '401: Unauthorized' when communicating with http://podevxxxx
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1609)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1616)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1560)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1361)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:658)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    ... 9 common frames omitted
Exception in thread "main" javax.xml.ws.WebServiceException: Could not send Message.
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:150)
    at com.sun.proxy.$Proxy32.siI040MMGETBARCODEOUT(Unknown Source)
    at com.dhc.ilead.main.ws.TestApplication.main(TestApplication.java:61)
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '401: Unauthorized' when communicating with http://podevxxxx
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1609)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1616)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1560)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1361)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:658)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:518)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:427)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:328)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:281)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:139)
    ... 2 more