【问题标题】:Authenticating SolrCore using Spring Data Solr使用 Spring Data Solr 对 SolrCore 进行身份验证
【发布时间】:2018-04-09 13:03:32
【问题描述】:

我有一个服务(API),它连接到 solr 以获取数据并作为响应发送。到目前为止,我们在 Solr 上没有任何类型的安全层,所以我的 API 工作正常。但是事情发生了变化,所有核心都是添加了用于身份验证的安全层。在我的 API 中连接到 Core 时遇到问题。以下是我的 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;
}

没有成功。

【问题讨论】:

  • 每个核心是否有不同的凭据,或者所有核心共享相同的凭据?
  • 实际上我对不同的核心有不同的凭据,但我使用的是管理员凭据,所以我可以访问所有核心

标签: java solr solrj solr4 spring-data-solr


【解决方案1】:

您可以使用以下代码进行身份验证...

@Bean
public SolrClientFactory solrClientFactory(final SolrClient solrClient){
Credentials 凭据 = new UsernamePasswordCredentials("test", "test");
return new HttpSolrClientFactory(solrClient,"collection", credentials,"BASIC");
}

【讨论】:

  • 如果我们连接多个集合怎么办?是否必须提供集合/核心名称?
  • 是的,不同的集合可以有不同的凭据
【解决方案2】:

因为批准的答案对我不起作用,我将与您分享我的解决方案。

@Value("${spring.data.solr.host}") String solrURL;
@Value("${spring.data.solr.user}") String solrUSER;
@Value("${spring.data.solr.pass}") String solrPASS;

@Bean
public SolrClient solrClient(){
    if(solrUSER.isEmpty() || solrUSER.equals("none") || solrPASS.isEmpty() || solrPASS.equals("none"))
        return new HttpSolrClient.Builder(solrURL).build();
    HttpSolrClient solrClient = new HttpSolrClient.Builder(solrURL).build();
    Credentials credentials = new UsernamePasswordCredentials(solrUSER, solrPASS);
    return new HttpSolrClientFactory(solrClient, credentials, "BASIC").getSolrClient();

}

如果您有任何与 base64 相关的错误,请添加此依赖项

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.15</version>
    </dependency>

在我的情况下,我首先测试 Solr 服务器是否使用身份验证。

Solr 主机是 http://localhost:8983/solr/,我使用的是 Spring Data Solr

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-16
    • 2015-11-22
    • 2011-10-17
    • 2012-11-27
    • 2013-12-08
    • 2021-03-30
    • 1970-01-01
    相关资源
    最近更新 更多