We are currently deploying Solr in war mode(Yes, recommendation is not war.
But this is something I can't change now. Planned for future). I am setting
authentication for solr. As Solr provided basic authentication is not
working in Solr 6.4.2, I am setting up digest authentication in tomcat for
Solr. I am able to login into Solr admin application using credentials.

Now from my Java application, when I try to run a query, which will delete
documents in a core, it's throwing following error.

org.apache.http.client.NonRepeatableRequestException: Cannot retry request
with a non-repeatable request entity

I can see in HttpSolrClient, we are setting only basic authentication. But,
I am using Digest auth. Did anyone faced this error before??

This is my code:

public static void main(String[] args) throws ClassNotFoundException,
SQLException, InterruptedException, IOException, SolrServerException {
        HttpSolrClient solrClient = getSolrHttpClient("solr",
"testpassword");

        try {
            solrClient.deleteByQuery("account", "*:*");
            solrClient.commit("account");
        } catch (final SolrServerException | IOException exn) {
            throw new IllegalStateException(exn);
        }
}

private static HttpSolrClient getSolrHttpClient(final String userName, final
String password) {

        final HttpSolrClient solrClient = new HttpSolrClient.Builder()
                .withBaseSolrUrl("http://localhost:9000/solr/index.html";)
                .withHttpClient(getHttpClientWithSolrAuth(userName,
password))
                .build();

        return solrClient;
    }

    private static HttpClient getHttpClientWithSolrAuth(final String
userName, final String password) {
        final CredentialsProvider provider = new BasicCredentialsProvider();
        final UsernamePasswordCredentials credentials
                = new UsernamePasswordCredentials(userName, password);
        provider.setCredentials(AuthScope.ANY, credentials);


        return HttpClientBuilder.create()
                .addInterceptorFirst(new PreemptiveAuthInterceptor())
                .setDefaultCredentialsProvider(provider)
                .build();

    }


    static class PreemptiveAuthInterceptor implements HttpRequestInterceptor
{

        DigestScheme digestAuth = new DigestScheme();

        PreemptiveAuthInterceptor() {

        }

        @Override
        public void process(final HttpRequest request, final HttpContext
context)
                throws HttpException, IOException {
            final AuthState authState = (AuthState)
context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);

            if (authState != null && authState.getAuthScheme() == null) {
                final CredentialsProvider credsProvider =
(CredentialsProvider)
context.getAttribute(HttpClientContext.CREDS_PROVIDER);
                final HttpHost targetHost = (HttpHost)
context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
                final Credentials creds = credsProvider.getCredentials(new
AuthScope(targetHost.getHostName(), targetHost.getPort(), "Solr",
"DIGEST"));
                if (creds == null) {
                    System.out.println("No credentials for preemptive
authentication");
                }
                digestAuth.overrideParamter("realm", "Solr");
                digestAuth.overrideParamter("nonce", Long.toString(new
Random().nextLong(), 36));
                AuthCache authCache = new BasicAuthCache();
                authCache.put(targetHost, digestAuth);

                // Add AuthCache to the execution context
                HttpClientContext localContext = HttpClientContext.create();
                localContext.setAuthCache(authCache);

                request.addHeader(digestAuth.authenticate(creds, request,
localContext));
            } else {
                System.out.println("authState is null. No preemptive
authentication.");
            }
        }
    }



--
Sent from: http://lucene.472066.n3.nabble.com/Solr-User-f472068.html

Reply via email to