使用 Spring Data Solr 验证 SolrCore

Authenticating SolrCore using Spring Data Solr

我有一个连接到 solr 以获取数据并作为 response.Till 发送的服务 (API) 现在我们在 Solr 上没有任何类型的安全层所以我的 API 正在工作 fine.But 事情发生了变化,所有核心都添加了用于身份验证的安全层。我在 API 中连接到 Core 时遇到问题。以下是我的 Java class 连接:

@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;
}

没成功。

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

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

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

@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