I have WSDL
. I need to make HTTP
basic (preemptive) authentication.
What to do?
I tried :
Authenticator myAuth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "pass".toCharArray());
}
};
Authenticator.setDefault(myAuth);
But it does not work: Caused by:
java.io.IOException: Server returned HTTP response code: 401 for URL ..
P.S. I use Apache CXF 2.6.2 and JBoss 5.0.1
Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.
You will have to make sure that you create an appropriate directory structure for your project and add the earlier shown hello. wsdl file to the specified folder. The wsdl2java plugin will compile this wsdl and create Apache CXF classes in a pre-defined folder.
In Apache Camel, the Camel CXF component is the key to integrating routes with Web services. You can use the Camel CXF component to create a CXF endpoint, which can be used in either of the following ways: Consumer — (at the start of a route) represents a Web service instance, which integrates with the route.
http-conf:conduit ElementSpecifies the HTTP connection properties such as timeouts, keep-alive requests, content types, etc. Specifies the the parameters for configuring the basic authentication method that the endpoint uses preemptively.
What you specified for your authentication is not enough. You should do something like this:
private YourService proxy;
public YourServiceWrapper() {
try {
final String username = "username";
final String password = "password";
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username,
password.toCharArray());
}
});
URL url = new URL("http://yourserviceurl/YourService?WSDL");
QName qname = new QName("http://targetnamespace/of/your/wsdl", "YourServiceNameInWsdl");
Service service = Service.create(url, qname);
proxy = service.getPort(YourService.class);
Map<String, Object> requestContext = ((BindingProvider) proxy).getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString());
requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
} catch (Exception e) {
LOGGER.error("Error occurred in web service client initialization", e);
}
}
Properties:
?WSDL
extension.WSDL
file. Second: your service name from WSDL
.Then you will be able to call your web service methods like this:
proxy.whatEverMethod();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With