Базовая аутентификация WSDL Java - PullRequest
0 голосов
/ 29 октября 2018

Я знаю, что есть много вопросов о Basic Authentication WSDL в Java, я нашел множество решений и, похоже, не работает. У меня есть этот веб-сервис

@WebServiceClient(name = "WSMrrtService", targetNamespace = "http://www.asycuda.org", wsdlLocation = "http://www.linktowsdl.com?wsdl")
public class WSMrrtService
extends Service
{

private final static URL WSMRRTSERVICE_WSDL_LOCATION;
private final static WebServiceException WSMRRTSERVICE_EXCEPTION;
private final static QName WSMRRTSERVICE_QNAME = new QName("http://www.asycuda.org", "WSMrrtService");

static {
    URL url = null;
    WebServiceException e = null;
    try {
        url = new URL("http://www.linktowsdl.com/WSMrrt?wsdl");
    } catch (MalformedURLException ex) {
        e = new WebServiceException(ex);
    }
    WSMRRTSERVICE_WSDL_LOCATION = url;
    WSMRRTSERVICE_EXCEPTION = e;
}

public WSMrrtService() {
    super(__getWsdlLocation(), WSMRRTSERVICE_QNAME);
}

public WSMrrtService(WebServiceFeature... features) {
    super(__getWsdlLocation(), WSMRRTSERVICE_QNAME, features);
}

public WSMrrtService(URL wsdlLocation) {
    super(wsdlLocation, WSMRRTSERVICE_QNAME);
}

public WSMrrtService(URL wsdlLocation, WebServiceFeature... features) {
    super(wsdlLocation, WSMRRTSERVICE_QNAME, features);
}

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

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

/**
 * 
 * @return
 *     returns WSMrrt
 */
@WebEndpoint(name = "WSMrrtServicePort")
public WSMrrt getWSMrrtServicePort() {
    return super.getPort(new QName("http://www.asycuda.org", "WSMrrtServicePort"), WSMrrt.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 WSMrrt
 */
@WebEndpoint(name = "WSMrrtServicePort")
public WSMrrt getWSMrrtServicePort(WebServiceFeature... features) {
    return super.getPort(new QName("http://www.asycuda.org", "WSMrrtServicePort"), WSMrrt.class, features);
}

private static URL __getWsdlLocation() {
    if (WSMRRTSERVICE_EXCEPTION!= null) {
        throw WSMRRTSERVICE_EXCEPTION;
    }
    return WSMRRTSERVICE_WSDL_LOCATION;
}

И я называю это как

    private static MrrtResult mrrtStore_1(org.asycuda.MrrtInfo mrrtInfo) {
    org.asycuda.WSMrrtService service = new org.asycuda.WSMrrtService();
    org.asycuda.WSMrrt port = service.getWSMrrtServicePort();
    return port.mrrtStore(mrrtInfo);
}

За исключением того, что всегда дает мне эту ошибку

Exception in thread "JavaFX Application Thread" com.sun.xml.internal.ws.client.ClientTransportException: The server sent HTTP status code 401: Unauthorized

Так что я попробовал много решений в сети, среди них это

   private static MrrtResult mrrtStore(org.asycuda.MrrtInfo mrrtInfo) {
    org.asycuda.WSMrrtService service = new org.asycuda.WSMrrtService();
    org.asycuda.WSMrrt port = service.getWSMrrtServicePort();

    /*Default Authentication 
    Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("myuser", "mypassword".toCharArray());
    }
    });
    */

    //Header authentication
     Map<String, List<String>> credentials = new HashMap<String,List<String>>();
    credentials.put("username", Collections.singletonList("myuser"));
    credentials.put("password", Collections.singletonList("mypassword"));
    ((BindingProvider)port).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, credentials);
    /*

    //JAX-WS Authentication
    Map<String, Object> reqContext = ((BindingProvider) port).getRequestContext();
    reqContext.put(BindingProvider.USERNAME_PROPERTY, "myuser");
    reqContext.put(BindingProvider.PASSWORD_PROPERTY, "mypassword");        
    */
    return port.mrrtStore(mrrtInfo);
}

Я пытался header authentication, default authentication и JAX-WS аутентификация и, кажется, не работает все дают мне то же самое 401 error

Я даже попробовал SOAPUI default Authentication и получил ту же ошибку, может кто-нибудь помочь мне выяснить, где я иду не так?

...