简体   繁体   中英

Connect to a solr server with credential with solrj

I'm trying to connect to a solr server using this code:

SolrServer solr = new HttpSolrServer("http://localhost:8080/solr-all");
SolrQuery query = new SolrQuery();

query.setQuery("*:*");
query.setRows(10);
query.setStart(0);

QueryResponse response = solr.query(query);
System.out.println(response);

but i get this error:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Server at http://localhost:8080/solr-all returned non ok status:401, message:Unauthorized

i really don't know how to set the username and password, i found many exaples on the web using htttpclient but none of them worked for me.

To set credentials:

httpclient.getCredentialsProvider().setCredentials(
                    AuthScope.ANY,
                    new UsernamePasswordCredentials(username, password));
solrServer = new HttpSolrServer(solrUrl, httpclient);//with credentials

Check similar discussion:

Solr 4 with basic authentication

Could you please check Slf4j is available at runtime ?

 Failed to load class "org.slf4j.impl.StaticLoggerBinder"

Means your code needs this class at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM