JAX-WS SOAP 客户端身份验证
JAX-WS SOAP Client Authentication
我希望你能帮助我。
我正在尝试使用 JAX-WS 创建 Java SOAP 客户端。
我已经使用 wsimport 导入了所有 Web 服务功能,
为此,我必须使用 -Xauthfile 提供一个 auth 文件,该 auth 文件包含以下内容:
http://username:password@server.com:80/Windchill/servlet/MathService?wsdl
我已将所有 类 导入到 eclipse 中,我正在尝试使用以下代码调用 MathService 添加函数:
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);
}
}
我已经尝试 with/without 覆盖服务端点
当我 运行 代码时,出现以下错误:
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;
}
}
如有任何帮助,我们将不胜感激!谢谢!
蒂姆
401 HTTP 响应似乎来自为 WSDL 定义本身提供服务的应用程序。我认为最好(如果可能)下载 WSDL 并将其捆绑在应用程序资源中。
然后您必须将 JAX-WS 生成的文件(可能 MathServiceImplService.java
)中的 URL 从 http://server.com/xxx.wsdl
更改为某些 URL,例如
MathServiceImplService.class.getResource("/path/to/wsdl.wsdl");
我用wsimport
生成代码,遇到了同样的问题。 Authenticator
适合我。在初始化服务之前添加以下代码。
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("<UserName>", "<Password>".toCharArray());
}
});
MathServiceImplService service = new MathServiceImplService();
...
我希望你能帮助我。
我正在尝试使用 JAX-WS 创建 Java SOAP 客户端。
我已经使用 wsimport 导入了所有 Web 服务功能, 为此,我必须使用 -Xauthfile 提供一个 auth 文件,该 auth 文件包含以下内容:
http://username:password@server.com:80/Windchill/servlet/MathService?wsdl
我已将所有 类 导入到 eclipse 中,我正在尝试使用以下代码调用 MathService 添加函数:
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);
}
}
我已经尝试 with/without 覆盖服务端点
当我 运行 代码时,出现以下错误:
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;
}
}
如有任何帮助,我们将不胜感激!谢谢!
蒂姆
401 HTTP 响应似乎来自为 WSDL 定义本身提供服务的应用程序。我认为最好(如果可能)下载 WSDL 并将其捆绑在应用程序资源中。
然后您必须将 JAX-WS 生成的文件(可能 MathServiceImplService.java
)中的 URL 从 http://server.com/xxx.wsdl
更改为某些 URL,例如
MathServiceImplService.class.getResource("/path/to/wsdl.wsdl");
我用wsimport
生成代码,遇到了同样的问题。 Authenticator
适合我。在初始化服务之前添加以下代码。
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("<UserName>", "<Password>".toCharArray());
}
});
MathServiceImplService service = new MathServiceImplService();
...