Spring boot 如何使用SpringDataSolr为solr添加基本身份验证?

Spring boot 如何使用SpringDataSolr为solr添加基本身份验证?,spring-boot,solr,Spring Boot,Solr,我在SpringBoot中创建了我的应用程序,并使用solr作为数据库。在应用程序中,使用SpringDataSolr连接solr,需要使用SpringDataSolr从应用程序实现对solr的基本身份验证 我没有看到任何例子。有没有办法实施它 @Configuration @EnableSolrRepositories(basePackages = { "com.test.org" }) public class SolrAuth { @Value("${solr.username}") p

我在SpringBoot中创建了我的应用程序,并使用solr作为数据库。在应用程序中,使用SpringDataSolr连接solr,需要使用SpringDataSolr从应用程序实现对solr的基本身份验证

我没有看到任何例子。有没有办法实施它

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

希望使用spring数据solr库通过基本身份验证与solr建立连接。

此代码非常适合我

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

看起来您正在将用户名设置为“username”,将密码设置为“password”,而不是使用类字段username和password。尝试删除注释,使您的凭据行看起来像:

凭证凭证=新用户名密码凭证(用户名、密码)