javasolrsolrjsolr4spring-data-solr

Authenticating SolrCore using Spring Data Solr


I have an Service(API) which connects to solr to fetch data and sends as response.Till now we didn't had any type of security layer on Solr so my API was working fine.But things changed and all core's were added with sceurity layer for authentication. I am facing problem while connecting to Core in my API. Following is my Java class for connection :

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

}

Is there any way to pass on credentials to each core ? I tried someting like this

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

It didnt work out.


Solution

  • you can use below code for the authentication...

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