elasticsearch httpclient认证机制

转载自http://www.cnblogs.com/youran-he/p/7562870.html

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

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

猜你喜欢

转载自blog.csdn.net/haiyang4988/article/details/78053109