Java JAX-WS SOAP客户端身份验证

Java JAX-WS SOAP客户端身份验证,java,web-services,soap,wsdl,jax-ws,Java,Web Services,Soap,Wsdl,Jax Ws,我希望你能帮助我 我正在尝试使用JAX-WS创建一个JavaSOAP客户机 我已经使用wsimport导入了所有Web服务函数, 为此,我必须使用-Xauthfile提供一个auth文件,该auth文件包含以下内容: 我已将所有类导入eclipse,并尝试使用以下代码调用MathService add函数: import javax.xml.ws.BindingProvider; import com.ptc.jws.service.org.myorg.mathservice.MathServ

我希望你能帮助我

我正在尝试使用JAX-WS创建一个JavaSOAP客户机

我已经使用wsimport导入了所有Web服务函数, 为此,我必须使用-Xauthfile提供一个auth文件,该auth文件包含以下内容:

我已将所有类导入eclipse,并尝试使用以下代码调用MathService add函数:

import javax.xml.ws.BindingProvider;
import com.ptc.jws.service.org.myorg.mathservice.MathServiceImpl;
import com.ptc.jws.service.org.myorg.mathservice.MathServiceImplService;

public class ClientStart {

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

 MathServiceImplService service = new MathServiceImplService();
 MathServiceImpl port = service.getMathServiceImplPort();

 // Configure service endpoint (override defined one of the WSDL) 
 BindingProvider binding = (BindingProvider) port;
 binding.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://server.com/Windchill/servlet/MathService");


 // Add HTTP Basic Authentification credentials to this request      
 binding.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "username");   
 binding.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");
 port.add(1, 1);

}

}
我已尝试覆盖/不覆盖服务端点

运行代码时,出现以下错误:

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to  access the WSDL at: http://server.com/Windchill/servlet/MathService?wsdl. It failed with: 
Got Server returned HTTP response code: 401 for URL: http://server.com/Windchill/servlet/MathService?wsdl while opening stream from http://server.com/Windchill/servlet/MathService?wsdl.
我也得到了这个错误:

WARNING: WSP0075: Policy assertion "{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}EncryptedParts" was evaluated as "UNKNOWN".
Mar 01, 2016 4:29:13 PM     [com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector]  selectAlternatives
MathServiceImpl.java文件如下所示:

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "MathServiceImplService", targetNamespace = "http://MathService.myorg.org.service.jws.ptc.com/", wsdlLocation = "http://server.com/Windchill/servlet/MathService?wsdl")
public class MathServiceImplService extends Service
{

private final static URL MATHSERVICEIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException MATHSERVICEIMPLSERVICE_EXCEPTION;
private final static QName MATHSERVICEIMPLSERVICE_QNAME = new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplService");

static {
    URL url = null;
    WebServiceException e = null;
    try {
        url = new URL("http://server.com/Windchill/servlet/MathService?wsdl");
    } catch (MalformedURLException ex) {
        e = new WebServiceException(ex);
    }
    MATHSERVICEIMPLSERVICE_WSDL_LOCATION = url;
    MATHSERVICEIMPLSERVICE_EXCEPTION = e;
}

public MathServiceImplService() {
    super(__getWsdlLocation(), MATHSERVICEIMPLSERVICE_QNAME);
}

public MathServiceImplService(WebServiceFeature... features) {
    super(__getWsdlLocation(), MATHSERVICEIMPLSERVICE_QNAME, features);
}

public MathServiceImplService(URL wsdlLocation) {
    super(wsdlLocation, MATHSERVICEIMPLSERVICE_QNAME);
}

public MathServiceImplService(URL wsdlLocation, WebServiceFeature... features) {
    super(wsdlLocation, MATHSERVICEIMPLSERVICE_QNAME, features);
}

public MathServiceImplService(URL wsdlLocation, QName serviceName) {
    super(wsdlLocation, serviceName);
}

public MathServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
    super(wsdlLocation, serviceName, features);
}

/**
 * 
 * @return
 *     returns MathServiceImpl
 */
@WebEndpoint(name = "MathServiceImplPort")
public MathServiceImpl getMathServiceImplPort() {
    return super.getPort(new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplPort"), MathServiceImpl.class);
}

/**
 * 
 * @param features
 *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
 * @return
 *     returns MathServiceImpl
 */
@WebEndpoint(name = "MathServiceImplPort")
public MathServiceImpl getMathServiceImplPort(WebServiceFeature... features) {
    return super.getPort(new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplPort"), MathServiceImpl.class, features);
}

private static URL __getWsdlLocation() {
    if (MATHSERVICEIMPLSERVICE_EXCEPTION!= null) {
        throw MATHSERVICEIMPLSERVICE_EXCEPTION;
    }
    return MATHSERVICEIMPLSERVICE_WSDL_LOCATION;
}

}
任何帮助都将不胜感激!谢谢大家!


Tim

401HTTP响应似乎来自为WSDL定义本身提供服务的应用程序。我认为最好(在可能的情况下)下载WSDL并将其捆绑到应用程序资源中

然后您必须将JAX-WS生成的文件(可能是
MathServiceImplService.java
)中的URL从
http://server.com/xxx.wsdl
到某个URL,如

MathServiceImplService.class.getResource("/path/to/wsdl.wsdl");

我使用
wsimport
生成代码,遇到了同样的问题<代码>验证器适用于我。在初始化服务之前添加以下代码

   Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("<UserName>", "<Password>".toCharArray());
        }
    });

   MathServiceImplService service = new MathServiceImplService();
   ...
Authenticator.setDefault(新验证器(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(“,”.toCharArray());
}
});
MathServiceImplService=新的MathServiceImplService();
...

您好,拉斐尔,谢谢您的回复。我真的很感激。我已将wsdl文件下载到我的应用程序资源中。您能解释一下我需要修改MathServiceImplService.java的哪些部分吗?我已将它添加到原来的问题中。非常感谢苏。请参阅静态初始值设定项,其中
新建URL(“http://server.com/Windchill/servlet/MathService?wsdl");我已将其更改为:“MATHSERVICEIMPLSERVICE\uWSDL\u LOCATION=MATHSERVICEIMPLSERVICE.class.getResource(“MathService.WSDL”);”我还是会犯同样的错误!我在原来的帖子中添加了一些更详细的stacktrace!也许还有更多的问题?警告:WSP0075:策略断言错误?问题是否已解决?同样的问题,对我也适用。比尝试在请求上下文中添加HTTP_请求_头要好。