Java 中的基本授权 |阿帕奇CXF
Basic Authorization in Java | Apache CXF
我想在 Java(客户端)中实现 SOAP 请求。
我已经在 SOAP 5.6.1 中创建了一个 SOAP 项目进行测试,一切正常。
请求需要基本授权(带有用户名和密码)。
我通过 Apache CXF 生成代码,现在我需要对我的请求实施基本授权。
我不确定在我的代码上实现它的好方法。 SOAP 5.6.1 有办法生成这个吗?
这是生成的 ImplService。
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);
}
}
这是其中一种实现的示例:
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();
}
}
我认为你的意思是基本身份验证,你可以使用 BindingProvider,例如:
BindingProvider provider = (BindingProvider) client;
Map<String,Object> rc = provider.getRequestContext();
rc.put(BindingProvider.USERNAME_PROPERTY, userName);
rc.put(BindingProvider.PASSWORD_PROPERTY, password);
您可以在此处查看完整示例:https://examples.javacodegeeks.com/enterprise-java/jws/jax-ws-bindingprovider-example/(查看客户端部分)。
我想在 Java(客户端)中实现 SOAP 请求。
我已经在 SOAP 5.6.1 中创建了一个 SOAP 项目进行测试,一切正常。 请求需要基本授权(带有用户名和密码)。
我通过 Apache CXF 生成代码,现在我需要对我的请求实施基本授权。
我不确定在我的代码上实现它的好方法。 SOAP 5.6.1 有办法生成这个吗?
这是生成的 ImplService。
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);
}
}
这是其中一种实现的示例:
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();
}
}
我认为你的意思是基本身份验证,你可以使用 BindingProvider,例如:
BindingProvider provider = (BindingProvider) client;
Map<String,Object> rc = provider.getRequestContext();
rc.put(BindingProvider.USERNAME_PROPERTY, userName);
rc.put(BindingProvider.PASSWORD_PROPERTY, password);
您可以在此处查看完整示例:https://examples.javacodegeeks.com/enterprise-java/jws/jax-ws-bindingprovider-example/(查看客户端部分)。