我有一个服务(API),它连接到Solr来获取数据并作为响应发送。到目前为止,我们在Solr上没有任何类型的安全层,所以我的API工作得很好。但事情发生了变化,所有核心都添加了安全层来进行身份验证。我在API中连接到核心时遇到了问题。以下是我的连接Java类:
@Configuration
@EnableConfigurationProperties
public class SolrConfigs {
private static final Logger LOG = LoggerFactory.getLogger(SolrConfigs.class);
@Value("${solr.core.FirstCore}")
private String coreFirstCore;
@Value("${solr.core.SecondCore}")
private String coreSecondCore;
@Value("${solr.core.ThirdCore}")
private String coreThirdCore;
@Bean
@ConfigurationProperties(prefix = "solr.client")
public SolrClient solrClient(@Value("${solr.client.baseURL}") final String solrBaseUrl) {
LOG.info("Connecting to solr : {}", solrBaseUrl);
final HttpSolrClient client = new HttpSolrClient(solrBaseUrl);
return client;
}
@Bean
public MulticoreSolrClientFactory multicoreSolrClientFactory(final SolrClient solrClient) {
final MulticoreSolrClientFactory factory = new MulticoreSolrClientFactory(solrClient, Arrays.asList(coreFirstCore, coreSecondCore, coreThirdCore));
return factory;
}
@Bean(name = "solrFirstCoreTemplate")
public SolrTemplate solrFirstCoreTemplate(MulticoreSolrClientFactory multicoreSolrClientFactory) {
final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory);
template.setSolrCore(coreFirstCore);
return template;
}
@Bean(name = "solrSecondCoreTemplate")
public SolrTemplate solrSecondCoreTemplate(final MulticoreSolrClientFactory multicoreSolrClientFactory) {
final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory);
template.setSolrCore(coreSecondCore);
return template;
}
@Bean(name = "solrThirdCoreTemplate")
public SolrTemplate solrThirdCoreTemplate(final MulticoreSolrClientFactory multicoreSolrClientFactory) {
final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory);
template.setSolrCore(coreThirdCore);
return template;
}
}
有没有办法把凭证传递给每个核心呢?我试过这样的方法
@Bean
@ConfigurationProperties(prefix = "solr.client")
public SolrClient solrClient(@Value("${solr.client.baseURL}") final String solrBaseUrl) {
LOG.info("Connecting to solr : {}", solrBaseUrl);
final HttpSolrClient client = new HttpSolrClient(solrBaseUrl);
return client;
}
@Bean(name = "solrFirstCoreTemplate")
public SolrTemplate solrFirstCoreTemplate(SolrClient solrClient) {
Credentials credentials = new UsernamePasswordCredentials(username, password);
final SolrTemplate template = new SolrTemplate(new HttpSolrClientFactory(solrClient, coreFirstCore, credentials , "BASIC"));
template.setSolrCore(coreFirstCore);
return template;
}
@Bean(name = "solrSecondCoreTemplate")
public SolrTemplate solrSecondCoreTemplate(SolrClient solrClient) {
Credentials credentials = new UsernamePasswordCredentials(username, password);
final SolrTemplate template = new SolrTemplate(new HttpSolrClientFactory(solrClient, coreSecondCore, credentials , "BASIC"));
template.setSolrCore(coreSecondCore);
return template;
}
但没有成功。
2条答案
按热度按时间jqjz2hbq1#
您可以使用以下代码进行身份验证...
@豆子
公共SolrClient工厂solrClient工厂(最终SolrClient solrClient){
凭证=新凭证UsernamePasswordCredentials(“测试”,“测试”);
返回新的HttpSolrClientFactory(solrClient,“集合,”凭证,“BASIC”);
}
sczxawaw2#
因为认可的答案对我不起作用,所以我将与您分享我的解决方案。
如果您有任何与base64相关错误,请添加此依赖项
在我的例子中,我首先测试Solr服务器是否使用身份验证。
Solr主机是http://localhost:8983/solr/,我使用的是Spring Data Solr