简体   繁体   中英

Basic Authorization in Java | Apache CXF

I would like to implement SOAP requests in Java (Client side).

I already created a SOAP project in SOAP 5.6.1 to test and everything works well. The requests need a Basic Authorization (with username and password).

I generate the code through Apache CXF and now I need to implement the Basic Authorization to my requests.

I am not sure about a good way to implement this on my code. There is a way to SOAP 5.6.1 generate this?

This was the ImplService generated.

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.Service;

/**
 * This class was generated by Apache CXF 3.5.0
 * 2022-01-06T12:58:41.736Z
 * Generated source version: 3.5.0
 *
 */
@WebServiceClient(name = "GuiaAcompanhamentoImplService",
        wsdlLocation = "https://qualsiliamb.apambiente.pt/services/egar/GuiaAcompanhamentoWs/v2?wsdl",
        targetNamespace = "http://pt.apa.guiaacompanhamento/v2")
public class GuiaAcompanhamentoImplService extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://pt.apa.guiaacompanhamento/v2", "GuiaAcompanhamentoImplService");
    public final static QName GuiaAcompanhamentoWsPort = new QName("http://pt.apa.guiaacompanhamento/v2", "GuiaAcompanhamentoWsPort");
    static {
        URL url = null;
        try {
            url = new URL("https://qualsiliamb.apambiente.pt/services/egar/GuiaAcompanhamentoWs/v2?wsdl");
        } catch (MalformedURLException e) {
            java.util.logging.Logger.getLogger(GuiaAcompanhamentoImplService.class.getName())
                    .log(java.util.logging.Level.INFO,
                            "Can not initialize the default wsdl from {0}", "https://qualsiliamb.apambiente.pt/services/egar/GuiaAcompanhamentoWs/v2?wsdl");
        }
        WSDL_LOCATION = url;
    }

    public GuiaAcompanhamentoImplService(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

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

    public GuiaAcompanhamentoImplService() {
        super(WSDL_LOCATION, SERVICE);
    }

    public GuiaAcompanhamentoImplService(WebServiceFeature ... features) {
        super(WSDL_LOCATION, SERVICE, features);
    }

    public GuiaAcompanhamentoImplService(URL wsdlLocation, WebServiceFeature ... features) {
        super(wsdlLocation, SERVICE, features);
    }

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




    /**
     *
     * @return
     *     returns GuiaAcompanhamentoWs
     */
    @WebEndpoint(name = "GuiaAcompanhamentoWsPort")
    public GuiaAcompanhamentoWs getGuiaAcompanhamentoWsPort() {
        return super.getPort(GuiaAcompanhamentoWsPort, GuiaAcompanhamentoWs.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 GuiaAcompanhamentoWs
     */
    @WebEndpoint(name = "GuiaAcompanhamentoWsPort")
    public GuiaAcompanhamentoWs getGuiaAcompanhamentoWsPort(WebServiceFeature... features) {
        return super.getPort(GuiaAcompanhamentoWsPort, GuiaAcompanhamentoWs.class, features);
    }

}

And this is an example to one of the implementations:

import com.evox.utils.GuiaAcompanhamentoImplService;
import com.evox.utils.GuiaAcompanhamentoWs;
import com.evox.establishmentsQueries.*;

import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceFeature;

public class ConsultaEstabelecimentosExample {

    static GuiaAcompanhamentoImplService service = new GuiaAcompanhamentoImplService(GuiaAcompanhamentoImplService.WSDL_LOCATION, GuiaAcompanhamentoImplService.SERVICE);
    static GuiaAcompanhamentoWs guiaAcompanhamentoWs = service.getGuiaAcompanhamentoWsPort();


    public static void main(String[] args) throws Exception {

        final ConsultaEstabelecimentosInput consultaEstabelecimentosInput = new ConsultaEstabelecimentosInput();
        final ConsultaEstabelecimentos consultaEstabelecimentos = new ConsultaEstabelecimentos();
        final ConsultaEstabelecimentosOutput consultaEstabelecimentosOutput = new ConsultaEstabelecimentosOutput();
        final ConsultaEstabelecimentosResponse consultaEstabelecimentosResponse = new ConsultaEstabelecimentosResponse();

        consultaEstabelecimentosInput.setTokenCertificacao("xxxxx");
        consultaEstabelecimentosInput.setNif("xxxxx");
        consultaEstabelecimentosInput.setCodigoAPA("xxxx");

        consultaEstabelecimentos.setArg0(consultaEstabelecimentosInput);


        guiaAcompanhamentoWs.consultaEstabelecimentos(consultaEstabelecimentos.getArg0()).getEstabelecimentos();


        }

}

I think you mean Basic Authentication, and you can use BindingProvider , something like:

BindingProvider provider = (BindingProvider) client;
Map<String,Object> rc = provider.getRequestContext();
rc.put(BindingProvider.USERNAME_PROPERTY, userName);
rc.put(BindingProvider.PASSWORD_PROPERTY, password);

You can see a complete example here: https://examples.javacodegeeks.com/enterprise-java/jws/jax-ws-bindingprovider-example/ (look at the client section).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM