首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用spring-data-solr为solr添加基本身份验证?

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

Stack Overflow用户
提问于 2019-09-10 05:05:05
回答 2查看 335关注 0票数 0

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

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

代码语言:javascript
复制
@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。

EN

回答 2

Stack Overflow用户

发布于 2020-04-20 22:01:44

这段代码非常适合我。

代码语言:javascript
复制
@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");
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-05-13 21:14:02

看起来您正在将username设置为" username“,将password设置为" password”,而不是使用类字段username和password。尝试删除注释,使您的凭据行看起来如下所示:

Credentials credentials =新的UsernamePasswordCredentials(用户名,密码);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57861179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档