Authenticate with the SolrJ Client Library

OAuth2 authentication servers provide an authentication service to Managed Search. Each Managed Search REST API call must include an Authorization header that contains a valid OAuth2 access token.

This topic describes how to use the Lucidworks Managed Search SolrJ client library to authenticate apps.

The OAuth2HttpRequestInterceptor implementation in the SolrJ client library simplifies the process of obtaining, using, and refreshing access tokens.

Use the SolrJ client library

The Lucidworks Managed Search SolrJ client library (managed-search-client-java.jar) is available for SolrJ users. You can use the OAuth2HttpRequestInterceptor implementation in the library to obtain OAuth2 access tokens.

To use this library, you must reference the Lucidworks Public Maven Repository (a repository in Artifactory for public Lucidworks artifacts) in your build file. And you must add a dependency there for the SolrJ client library.

Note
SolrJ 8.2.0 is already included as a dependency, so you don’t need to add it as an explicit dependency in your build file.

Gradle users

How to use the SolrJ client library (Gradle)
  1. Add a reference to the Lucidworks Public Maven Repository to build.gradle:

    repositories {
        mavenCentral()
        maven {
            url "https://ci-artifactory.lucidworks.com/artifactory/public-artifacts/"
        }
    }
  2. Add a dependency for the Lucidworks Managed Search SolrJ client library to build.gradle:

    // Add a dependency for the Lucidworks Managed Search SolrJ client library.
    // The client-library dependency includes a Solr-SolrJ 8.2.0 dependency,
    // so you don't need to add that dependency.
    compile group: 'com.lucidworks.cloud', name: 'managed-search-client-java', version: '8.2.0.a'

Maven users

How to use the SolrJ client library (Maven)

Add the Lucidworks' snapshots maven repository and the dependency in pom.xml:

In pom.xml, add a reference to the Lucidworks Public Maven Repository and a dependency for the Lucidworks Managed Search SolrJ client library:

<repositories>
    <repository>
        <id>lucidworks</id>
        <name>Lucidworks Public Maven Repository</name>
        <url>https://ci-artifactory.lucidworks.com/artifactory/public-artifacts/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.lucidworks.cloud</groupId>
        <artifactId>managed-search-client-java</artifactId>
        <version>8.2.0.a</version>
    </dependency>
</dependencies>

Client workflow

The Lucidworks Managed Search SolrJ client library (managed-search-client-java.jar) provides an implementation of the interface HttpRequestInterceptor. The implementation is named OAuth2HttpRequestInterceptor.

The OAuth2HttpRequestInterceptor implementation:

  1. Automatically obtains an OAuth2 access token from the Lucidworks Managed Search Authorization server, using the customerId, clientId and clientSecret issued during the onboarding process.

  2. Automatically adds the access token to an Authorization request header in every request that is sent to the Managed Search service.

Furthermore, the OAuth2HttpRequestInterceptor implementation automatically refreshes the access token every hour before the access token expires.

An OAuth2HttpRequestInterceptorBuilder class is provided so you can configure and use the OAuth2HttpRequestInterceptor easily.

Example

// Information required to access the Managed Search service
String customerId = "acme-corp";
String solrUrl = "https://pg01.us-west1.cloud.lucidworks.com/acme-corp/search-product/solr";
String oauth2ClientId = System.getProperty("OAUTH2_CLIENT_ID");
String oauth2ClientSecret = System.getProperty("OAUTH2_CLIENT_SECRET");

// Create an HTTP request interceptor and start it.
OAuth2HttpRequestInterceptor oauth2HttpRequestInterceptor = new OAuth2HttpRequestInterceptorBuilder(customerId, oauth2ClientId, oauth2ClientSecret).build();
oauth2HttpRequestInterceptor.start();

// Register the HTTP request interceptor with SolrJ.
HttpClientUtil.addRequestInterceptor(oauth2HttpRequestInterceptor);

// Create a cloud Solr client that uses the Solr URL.
try (CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(Collections.singletonList(solrUrl)).build()) {
        // ...
        // Index, commit, and query.
        // ...
} finally {
    // Remove the HTTP request interceptor from the request chain.
    HttpClientUtil.removeRequestInterceptor(oauth2HttpRequestInterceptor);
    // Close the HTTP request interceptor to stop the background token refresh.
    IOUtils.closeQuietly(oauth2HttpRequestInterceptor);
}