简体   繁体   English

如何使用 spring-data-solr 为 solr 添加基本身份验证?

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

I created my application in spring boot and using solr as a database.我在 Spring Boot 中创建了我的应用程序并使用 solr 作为数据库。 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.在应用中使用 spring-data-solr 连接 solr,需要使用 spring-data-solr 从应用中实现对 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.想要使用 spring-data-solr 库通过 Basic Auth 连接到 solr。

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);凭证凭证 = 新用户名密码凭证(用户名,密码);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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