我在spring boot中创建了我的应用程序,并使用solr作为数据库。在使用spring-data-solr连接solr的应用程序中,需要使用spring-data-solr从应用程序实现对solr的基本验证。
我没有看到任何例子。有没有办法实现它?
@Configuration
@EnableSolrRepositories(basePackages = { "com.test.org" })
public class SolrAuth {
@Value("${solr.username}")
private String username;
@Value("${solr.password}")
private String password;
@Bean
SolrTemplate solrTemplate() {
return new SolrTemplate(solrClientFactory());
}
@Bean
SolrClientFactory solrClientFactory() {
Credentials credentials = new UsernamePasswordCredentials("username", "password");
return new HttpSolrClientFactory(solrServer(), credentials, "BASIC");
}
@Bean
SolrClient solrServer() {
return new HttpSolrClient.Builder("http://localhost:8983/solr").build();
}
}我想使用spring-data-solr库通过Basic Auth连接到solr。
发布于 2020-04-20 22:01:44
这段代码非常适合我。
@Configuration
@EnableSolrRepositories(basePackages = { "com.test.org" })
public class SolrAuth {
@Value("${solr.username}")
private String username;
@Value("${solr.password}")
private String password;
@Value("${solr.url}")
private String solrUrl;
@Bean
public SolrClient solrClient() {
return new HttpSolrClient(solrUrl);
}
@Bean
public SolrTemplate solrTemplate(SolrClient client) throws SolrException {
return new SolrTemplate(client);
}
@Bean
public SolrClientFactory solrClientFactory(final SolrClient solrClient){
Credentials credentials = new UsernamePasswordCredentials(username, password);
return new HttpSolrClientFactory(solrClient, credentials, "BASIC");
}
}发布于 2020-05-13 21:14:02
看起来您正在将username设置为" username“,将password设置为" password”,而不是使用类字段username和password。尝试删除注释,使您的凭据行看起来如下所示:
Credentials credentials =新的UsernamePasswordCredentials(用户名,密码);
https://stackoverflow.com/questions/57861179
复制相似问题