码迷,mamicode.com
首页 > Web开发 > 详细

elasticsearch httpclient认证机制

时间:2017-09-21 13:20:45      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:继承   搜索   new   class   .com   pcl   dal   rri   cep   

最近公司单位搬迁,所有的服务都停止了,我负责的elasticsearch不知道怎么回事,一直不能运行呢,因此,我一直在负责调试工作。经过两天的调试工作,我发现新的服务器增加了httpclient认证机制,经过几次研究,发现服务器的新增了如下内容:

 http.basic.log: false
 http.basic.user: "hett"
 http.basic.password: "****"

因此,每次在进行ik拆分词的时候会有提示信息就是:http没有认证,因此,在加载http链接之间加入提前认证机制,在初始化bean的时候就开始认证,代码做如下修改:

public class ElasticsearchServiceImpl implements IElasticsearchService , InitializingBean

改类继承初始化bean的认证


实现父类的方法:

     @Override
    public void afterPropertiesSet() throws Exception {
        try {
            Properties props = PropertiesLoaderUtils
                    .loadAllProperties("********");
            String authUser = StringUtil
                    .null2Str(props.getProperty("username"));
            String authPwd = StringUtil.null2Str(props.getProperty("password"));
            credentialContext = HttpClientContext.create();
            // 认证提供者
            CredentialsProvider credsProvider = new BasicCredentialsProvider();

            credsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(authUser, authPwd));

            AuthCache authCache = new BasicAuthCache();
            // 提前填充认证信息缓存到上下文中,这样,以这个上下文执行的方法,就会使用抢先认证。可能会出错
            credentialContext.setAuthCache(authCache);
            credentialContext.setCredentialsProvider(credsProvider);
        } catch (Exception ex) {
            logger.warn("read elasticsearch credential error", ex);
        }
    }

再次访问的时候提示如下:

技术分享

技术分享

 

debug抛出的信息是认证通过

经过几天的折腾终于完成了搜索标签的之类的服务啦
好开心啊

elasticsearch httpclient认证机制

标签:继承   搜索   new   class   .com   pcl   dal   rri   cep   

原文地址:http://www.cnblogs.com/youran-he/p/7562870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!