对于solr 3.5中的基本身份验证,我使用以下代码
String url = "http://192.168.192.11:8080/solr/FormResponses";
CommonsHttpSolrServer server = new CommonsHttpSolrServer( url );
String username = "user";
String password = "user123";
Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
server.getHttpClient().getState().setCredentials(AuthScope.ANY, defaultcreds);
server.getHttpClient().getParams().setAuthenticationPreemptive(true);
在solr 4.0中CommonsHttpSolrServer
不可用,所以我想用它替换它
HttpSolrServer
。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:7)
按如下方式更改代码:
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
String url = "http://192.168.192.11:8080/solr/FormResponses";
HttpSolrServer server = new HttpSolrServer( url );
DefaultHttpClient client = (DefaultHttpClient) server.getHttpClient();
UsernamePasswordCredentials defaultcreds = new UsernamePasswordCredentials("user", "user123");
client.getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds);
对于HttpClient 4中的server.getHttpClient().getParams().setAuthenticationPreemptive(true)
,您可以使用here所述的解决方案。
答案 1 :(得分:2)
最后,我找到了自己的答案,
String url = "http://192.168.192.11:8080/solr/FormResponses";
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials("user", "user123"));
SolrServer solrServer = new HttpSolrServer(url, httpclient);
答案 2 :(得分:1)
您需要为HttpClientUtil添加JAR solr-solrj-4.0.0.jar。
然后使用以下代码:
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/"+url);
HttpClientUtil.setBasicAuth((DefaultHttpClient) solrServer.getHttpClient(),
"USERNAME", "PASSWORD");
这对我有用。
答案 3 :(得分:1)
这是对我有用的唯一方法:
String url = "192.168.192.11:8080/solr/FormResponses";
String username = "user";
String password = "user123";
HttpSolrServer server = new HttpSolrServer("http://" + username + ":" + password + "@" + url);