Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Solr的用户名和密码身份验证_Solr_Solrj - Fatal编程技术网

Solr的用户名和密码身份验证

Solr的用户名和密码身份验证,solr,solrj,Solr,Solrj,我正在使用solrj向Solr插入和查询数据 我正在使用以下方法保存/查询数据 SolrClient client = getSolrClient(); final Map<String, String> queryParamMap = new HashMap<>(); queryParamMap.put("q", "*:*"); queryParamMap.put("fl", "id, name"); MapSolrParams queryParams = new Ma

我正在使用
solrj
Solr
插入和查询数据

我正在使用以下方法保存/查询数据

SolrClient client = getSolrClient();
final Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("q", "*:*");
queryParamMap.put("fl", "id, name");
MapSolrParams queryParams = new MapSolrParams(queryParamMap);
final QueryResponse response = client.query("testcore1", queryParams);
final SolrDocumentList documents = response.getResults();
我为Solr添加了用户名和密码身份验证。 我更改了Solr客户端方法并添加了
UsernamePasswordCredentials

private SolrClient getSolrClient() {
        String solrUrl = "http://localhost:8983/solr";
        //return new HttpSolrClient.Builder(solrUrl).build();
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password");
        provider.setCredentials(AuthScope.ANY, credentials);
        HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
        return new HttpSolrClient.Builder().withBaseSolrUrl(solrUrl).withHttpClient(client).build();
}
不过,它显示了以下错误

Exception in thread "main" org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://localhost:8983/solr

如何向这些函数添加身份验证?

尝试使用
HttpSolrClientFactory
,方法如下所示:

私有SolrClient工厂getClientFactory(最终SolrClient客户机,字符串coreName){
凭据凭据=新用户名密码凭据(
环境getProperty(“solr.user”),
环境getProperty(“解决方案密码”)
);
返回新的HttpSolrClientFactory(客户端、coreName、凭据,“基本”);
}
SolrClient=getClientFactory(新的HttpSolrClient(SOLR_URL),coreName).getSolrClient()
final Map queryParamMap=新HashMap();
queryParamMap.put(“q”,“*:*”);
queryParamMap.put(“fl”、“id、名称”);
MapSolrParams queryParams=新的MapSolrParams(queryParamMap);
final QueryResponse response=client.query(“testcore1”,queryParams);
最终解决方案文档列表文档=response.getResults();

希望这对你有帮助

尝试使用
HttpSolrClientFactory
,使方法看起来像:

私有SolrClient工厂getClientFactory(最终SolrClient客户机,字符串coreName){
凭据凭据=新用户名密码凭据(
环境getProperty(“solr.user”),
环境getProperty(“解决方案密码”)
);
返回新的HttpSolrClientFactory(客户端、coreName、凭据,“基本”);
}
SolrClient=getClientFactory(新的HttpSolrClient(SOLR_URL),coreName).getSolrClient()
final Map queryParamMap=新HashMap();
queryParamMap.put(“q”,“*:*”);
queryParamMap.put(“fl”、“id、名称”);
MapSolrParams queryParams=新的MapSolrParams(queryParamMap);
final QueryResponse response=client.query(“testcore1”,queryParams);
最终解决方案文档列表文档=response.getResults();

希望这对你有帮助

我也有同样的问题。对我来说,使用“ConcurrentUpdateSolrClient”解决了这个问题:

    public static SolrClient getUpdateClient() {
    SolrClient solrClient = new ConcurrentUpdateSolrClient.Builder("http://USERNAME:PASSWORD@localhost:8983/solr")
      .build();
    return solrClient;
  }

我也有同样的问题。对我来说,使用“ConcurrentUpdateSolrClient”解决了这个问题:

    public static SolrClient getUpdateClient() {
    SolrClient solrClient = new ConcurrentUpdateSolrClient.Builder("http://USERNAME:PASSWORD@localhost:8983/solr")
      .build();
    return solrClient;
  }