【问题标题】:JAX-WS SOAP Client AuthenticationJAX-WS SOAP 客户端身份验证
【发布时间】:2016-03-01 15:54:43
【问题描述】:

我希望你能帮助我。

我正在尝试使用 JAX-WS 创建 Java SOAP 客户端。

我已经使用 wsimport 导入了所有的 Web 服务功能, 为此,我必须使用 -Xauthfile 提供一个身份验证文件,身份验证文件包含以下内容:

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);

}

}

我尝试过覆盖/不覆盖服务端点

当我运行代码时,我收到以下错误:

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;
}

}

任何帮助将不胜感激!谢谢!

提姆

【问题讨论】:

  • 问题解决了吗?

标签: java web-services soap wsdl jax-ws


【解决方案1】:

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

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

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

【讨论】:

  • 您好拉斐尔,感谢您的回复。对此,我真的非常感激。我已将 wsdl 文件下载到我的应用程序资源中。你能解释一下我需要修改 MathServiceImplService.java 的哪些部分吗?我已将其添加到原始问题中。非常感谢。蒂姆
  • 查看new URL("http://server.com/Windchill/servlet/MathService?wsdl");的静态初始化器
  • 我已将其更改为:'MATHSERVICEIMPLSERVICE_WSDL_LOCATION = MathServiceImplService.class.getResource("MathService.wsdl");'我仍然得到同样的错误!我在原帖中添加了一些更详细的堆栈跟踪!
  • 也许还有更多问题?警告:WSP0075:策略断言错误?
【解决方案2】:

我使用wsimport 生成代码并面临同样的问题。 Authenticator 为我工作。在初始化服务之前添加以下代码。

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

   MathServiceImplService service = new MathServiceImplService();
   ...

【讨论】:

  • 同样的问题,也适用于我。比尝试在请求上下文中添加 HTTP_REQUEST_HEADERS 更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-02
  • 1970-01-01
相关资源
最近更新 更多