简体   繁体   中英

How to add basic Auth for solr using spring-data-solr?

I created my application in spring boot and using solr as a database. In application using spring-data-solr to connect the solr and need to implement basic Auth to the solr from the application using spring-data-solr.

I don't see any examples. Is there anyway to implement it?

@Configuration
@EnableSolrRepositories(basePackages = { "com.test.org" })
public class SolrAuth {

@Value("${solr.username}")
private String username;

@Value("${solr.password}")
private String password;

@Bean
SolrTemplate solrTemplate() {
    return new SolrTemplate(solrClientFactory());
}

@Bean
SolrClientFactory solrClientFactory() {
    Credentials credentials = new UsernamePasswordCredentials("username", "password");
    return new HttpSolrClientFactory(solrServer(), credentials, "BASIC");
}

@Bean
SolrClient solrServer() {
    return new HttpSolrClient.Builder("http://localhost:8983/solr").build();
}
}

Want to make a connection to solr through Basic Auth using spring-data-solr library.

This code works perfect for me.

@Configuration
@EnableSolrRepositories(basePackages = { "com.test.org" })
public class SolrAuth {

    @Value("${solr.username}")
    private String username;

    @Value("${solr.password}")
    private String password;

    @Value("${solr.url}")
    private String solrUrl;

    @Bean
    public SolrClient solrClient() {
        return new HttpSolrClient(solrUrl);
    }

    @Bean
    public SolrTemplate solrTemplate(SolrClient client) throws SolrException { 
        return new SolrTemplate(client);
    }   

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

looks like you are setting the username to "username" and password to "password" instead of using the class fields username and password. try removing the comments so your credentials line looks like:

Credentials credentials = new UsernamePasswordCredentials(username, password);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM