使用 eclipse 进行 Java Web 服务客户端身份验证

标签 java eclipse web-services wsdl

我使用默认的 Eclipse 向导来创建 Web 服务客户端("file">“新建”>“其他”并为 Web 服务客户端选择一个向导)。类已生成,我可以通过如下代码使用服务方法:

import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.olsn.ws.ShipmentService_PortType;
import com.olsn.ws.ShipmentService_ServiceLocator;

public class wsClient {
 public static void main(String[] args) throws ServiceException, RemoteException {

             //To load the web service and to create the client
             ShipmentService_ServiceLocator WS = new ShipmentService_ServiceLocator();              
             ShipmentService_PortType client = WS.getShipmentServicePort();

             //To get Close status for Issue
             System.out.println(client.getCloseDesc("35557"));

             //To get the list of NetP projects
             for (int i=1; i<client.getProjects().length;i++)
                 {System.out.println(client.getProjects()[i].toString());}

           }

     }

现在的问题是引入了用户名和密码,我不知道如何修改我的代码来访问服务器,我试图添加身份 validator :

import java.net.Authenticator;
import java.net.PasswordAuthentication;

Authenticator myAuth = new Authenticator() 
{
    @Override
    protected PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication("user", "password".toCharArray());
    }
};

Authenticator.setDefault(myAuth);

或者更改生成的代理类的_initShipmentServiceProxy()方法

  private void _initShipmentServiceProxy() {
    try {
      shipmentService_PortType = (new com.olsn.ws.ShipmentService_ServiceLocator()).getShipmentServicePort();
      if (shipmentService_PortType != null) {
        if (_endpoint != null)
        {
          ((javax.xml.rpc.Stub)shipmentService_PortType)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);
          **((javax.xml.rpc.Stub)shipmentService_PortType)._setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY, "user");
          ((javax.xml.rpc.Stub)shipmentService_PortType)._setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY, "password");**
        }  
        else
          _endpoint = (String)((javax.xml.rpc.Stub)shipmentService_PortType)._getProperty("javax.xml.rpc.service.endpoint.address");
      }

    }
    catch (javax.xml.rpc.ServiceException serviceException) {}
  }

但我继续收到这些错误:

org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
Exception in thread "main" AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client.Authentication
 faultSubcode: 
 faultString: Access denied to operation getCloseDesc

请告诉我应该更改什么?

最佳答案

根据您的错误,我假设您正在使用Axis,因此Authenticator将无法使用。如果您想对Axis服务执行客户端的基本身份验证,您需要执行如下操作:

private static final Logger LOGGER = LoggerFactory.getLogger(YourClass.class);

private ShipmentService_PortType proxy;

public void init()
        throws Exception {
    ShipmentService_ServiceLocator locator = new ShipmentService_ServiceLocator();
    try {
        LOGGER.debug("Initializing shipment service...");
        proxy = locator.getShipmentServicePort(new URL("yourServiceEndpointURL"));
        ((ShipmentServicePortBindingStub) proxy).setUsername("yourUsername");
        ((ShipmentServicePortBindingStub) proxy).setPassword("yourPassword");
        ((ShipmentServicePortBindingStub) proxy).setTimeout(10000);
        LOGGER.debug("Shipment service successfully initialized.");
    } catch (ServiceException e) {
        LOGGER.error("Error in shipment client initialization", e);
        throw new Exception("Error in shipment client initialization.");
    } catch (MalformedURLException e) {
        LOGGER.error("Error in shipment client initialization", e);
        throw new Exception("Error in shipment client initialization.");
    }
}

只有当这个 init() 方法被调用时,您才能通过以下方式调用客户端的方法:

proxy.getCloseDesc("35557");

关于使用 eclipse 进行 Java Web 服务客户端身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12513841/

相关文章:

java - 如何从 Firebase 存储获取 URL getDownloadURL

java - 关于Spring 3 MVC Controller @RequestMapping

java - 这是什么意思 : Not a v4. 0.0 POM。对于项目 org.codehaus.mojo:rpm-maven-plugin

java - native 内存与 Java 应用程序内存

java - 这个 Eclipse View 是如何制作的?

java - 我正在尝试打印 StringBuffer.toString() 但它不起作用

java - 通过一些调整使用 java 发送邮件

java - cxf 2way ssl not webservice give 无法创建安全的 XMLInputFactory

Java xml dsig DigestValue ( jsr105 Provider apache, oracle )

web-services - 面向服务的架构建议