Solr 4 with basic authentication

血红的双手。 提交于 2019-12-01 21:26:15

i had the same problem when implementing partial documents update. i solved the problem by implementing PreemptiveAuthInterceptor. see below code

PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager(
                        SchemeRegistryFactory.createDefault());
                cxMgr.setMaxTotal(100);
                cxMgr.setDefaultMaxPerRoute(20);

                DefaultHttpClient httpclient = new DefaultHttpClient(cxMgr);
                httpclient.addRequestInterceptor(
                        new PreemptiveAuthInterceptor(), 0);
                httpclient.getCredentialsProvider().setCredentials(
                        AuthScope.ANY,
                        new UsernamePasswordCredentials(solrDto.getUsername(),
                                solrDto.getPassword()));

                HttpSolrServer solrServerInstance = new HttpSolrServer(solrDto.getUrl(),
                        httpclient);
                solrServerInstance.setRequestWriter(new BinaryRequestWriter());
                solrServerInstance.setAllowCompression(true);

You also need:

    private class PreemptiveAuthInterceptor implements HttpRequestInterceptor {

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

                // If no auth scheme avaialble yet, try to initialize it
                // preemptively
                if (authState.getAuthScheme() == null) {
                    CredentialsProvider credsProvider = (CredentialsProvider) context
                            .getAttribute(ClientContext.CREDS_PROVIDER);
                    HttpHost targetHost = (HttpHost) context
                            .getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                    Credentials creds = credsProvider.getCredentials(new AuthScope(
                            targetHost.getHostName(), targetHost.getPort()));
                    if (creds == null)
                        throw new HttpException(
                                "No credentials for preemptive authentication");
                    authState.setAuthScheme(new BasicScheme());
                    authState.setCredentials(creds);
                }

            }

        }

According to the Solr Security - SolrJ section on Solr Wiki you should be able to do the following:

 public static void main(String[] args) throws SolrServerException, IOException {

     HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr/");

     HttpClientUtil.setBasicAuth(server.getHttpClient(), USERNAME, PASSWORD);

     SolrInputDocument document = new SolrInputDocument();
     document.addField("id",123213);
     server.add(document);
     server.commit();

 }
ThmHarsh

You need to add the JAR solr-solrj-4.0.0.jar for HttpClientUtil.

Then use the below code:

HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/"+url);     
HttpClientUtil.setBasicAuth((DefaultHttpClient) solrServer.getHttpClient(), "USERNAME", "PASSWORD");

That worked for me on Jdk 1.6 and tomcat 6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!