1)
Add the library jersey-apache-connector-*.jar
to your project. You can find this library in the public maven repository:
https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector
2)
Implement a NTLM feature which will add the credentials to each request:
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
public class NtlmFeature implements Feature {
private static String USER = "your-username";
private static String PASSWORD = "your-password";
private static String WORKSTATION = null;
private static String DOMAIN = null;
@Override
public boolean configure(FeatureContext context) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
NTCredentials ntCredentials = new NTCredentials(USER, PASSWORD, WORKSTATION, DOMAIN);
credentialsProvider.setCredentials(AuthScope.ANY, ntCredentials);
context.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
return true;
}
}
3) Add the tomcat plugin to the project. This plugin holds the Jersey Client Libraries which will be needed in step 4.
- Convert the project as plugin-project (Java Perspective > Right Click on the Project > Configure > Convert to Plugin-Projects ...)
- Open META-INF/MANIFEST.MF
- Go to Tab Dependencies and add the tomcat plugin (simply type tomcat)
4)
Implement the call. In this scenario you must create the rest client on your own and you can't use the Ivy Rest Clients (Ivy.rest().client(..)):
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import ch.ivyteam.ivy.environment.Ivy;
public class NtlmClient {
private static String baseUrl = "http://localhost/ntlmProtectedRessource";
public static void callNtlmProtectedRestService() {
Client client = ClientBuilder.newClient(createConfig());
client.register(new NtlmFeature());
WebTarget target = client.target(baseUrl);
Response response = target.request(MediaType.APPLICATION_JSON_TYPE).get();
Ivy.log().info("HTTP Status Code: " + response.getStatus());
}
private static ClientConfig createConfig() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
return clientConfig;
}
}
Axon.ivy 7.1 and later:
Starting with Axon.ivy 7.1 a Feature called NtlmAuthenticationFeature will be provided, that simplifies the calling of NTLM protected web services.
answered
09.10.2017 at 06:20
Alex Suter ♦♦
3.1k●12●22●47
accept rate:
84%