Kalebere Gaurav
Kalebere Gaurav

Reputation: 69

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.

Upvotes: 0

Views: 871

Answers (2)

elouanesbg
elouanesbg

Reputation: 66

because the approved answer didn't work for me, I will share my solution with you.

@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();

}

and add this dependency if you have any error related to base64

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

in my case, I test first if the Solr server uses authentification or not.

the Solr host is http://localhost:8983/solr/ and I am using spring data Solr

Upvotes: 0

Vikash
Vikash

Reputation: 663

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");
}

Upvotes: 2

Related Questions