使用 Eclipse 的 Java Web 服务客户端身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12513841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:01:02  来源:igfitidea点击:

Java Web Service client authentication using eclipse

javaeclipseweb-serviceswsdl

提问by lenchevsky

I've used default eclipse wizard to create a web service client (File>New>Other and select a wizard for web service client). The classes were generated and I was able to use service methods with code like this:

我使用默认的 Eclipse 向导创建了一个 Web 服务客户端(文件>新建>其他并为 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());}

           }

     }

The problem now is that the username and password were introduced, and I don't know how to modify my code to access the server, I was trying to add authenticator:

现在的问题是引入了用户名和密码,不知道怎么修改我的代码来访问服务器,尝试添加authenticator:

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

or change _initShipmentServiceProxy() method of generated proxy class

或更改生成的代理类的 _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) {}
  }

But I continue getting these errors:

但我继续收到这些错误:

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

Please advise what should I change?

请指教我应该改变什么?

回答by Paulius Matulionis

From your error I assume you are using Axisso the Authenticatorwon't work on it. If you want to perform the client's basic authentication to Axisservice, you need to do something like this:

根据您的错误,我假设您正在使用,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.");
    }
}

And only when this init()method will be called you will be able to call your client's methods through this:

并且只有在init()调用此方法时,您才能通过以下方式调用客户端的方法:

proxy.getCloseDesc("35557");