簡體   English   中英

如何使用 spring-data-solr 為 solr 添加基本身份驗證?

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

我在 Spring Boot 中創建了我的應用程序並使用 solr 作為數據庫。 在應用中使用 spring-data-solr 連接 solr,需要使用 spring-data-solr 從應用中實現對 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-data-solr 庫通過 Basic Auth 連接到 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");
    }
}

看起來您將用戶名設置為“用戶名”,將密碼設置為“密碼”,而不是使用類字段用戶名和密碼。 嘗試刪除注釋,使您的憑據行看起來像:

憑證憑證 = 新用戶名密碼憑證(用戶名,密碼);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM