zoukankan      html  css  js  c++  java
  • elasticsearch httpclient认证机制

    最近公司单位搬迁,所有的服务都停止了,我负责的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抛出的信息是认证通过

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

  • 相关阅读:
    《那些年,我们拿下FPGA》做笔记
    三种初始化
    hdu4417 Super Mario 树阵离线/划分树
    【设计模式】文章摘要 查找联系人控件
    STL set
    阐述 QUEST CENTRAL FOR DB2 八罪
    使用线程执行堆栈StackTraceElement设计Android日志模块
    苹果iOS苹果公司的手机用户都有权索赔
    Android 4.4 沉浸式透明状态栏与导航栏
    常见的几种RuntimeException
  • 原文地址:https://www.cnblogs.com/youran-he/p/7562870.html
Copyright © 2011-2022 走看看