设为首页 加入收藏

TOP

java与es8实战之五:SpringBoot应用中操作es8(带安全检查:https、账号密码、API Key)(五)
2023-09-09 10:25:59 】 浏览:124
Tags:java es8 SpringBoot 应用中 安全检 https API Key
agementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; @ConfigurationProperties(prefix = "elasticsearch") //配置的前缀 @Configuration @Slf4j public class ClientConfig { @Setter private String hosts; @Setter private String username; @Setter private String passwd; @Setter private String apikey; /** * 解析配置的字符串,转为HttpHost对象数组 * @return */ private HttpHost[] toHttpHost() { if (!StringUtils.hasLength(hosts)) { throw new RuntimeException("invalid elasticsearch configuration"); } String[] hostArray = hosts.split(","); HttpHost[] httpHosts = new HttpHost[hostArray.length]; HttpHost httpHost; for (int i = 0; i < hostArray.length; i++) { String[] strings = hostArray[i].split(":"); httpHost = new HttpHost(strings[0], Integer.parseInt(strings[1]), "https"); httpHosts[i] = httpHost; } return httpHosts; } @Bean public ElasticsearchClient clientByPasswd() throws Exception { ElasticsearchTransport transport = getElasticsearchTransport(username, passwd, toHttpHost()); return new ElasticsearchClient(transport); } private static SSLContext buildSSLContext() { ClassPathResource resource = new ClassPathResource("es01.crt"); SSLContext sslContext = null; try { CertificateFactory factory = CertificateFactory.getInstance("X.509"); Certificate trustedCa; try (InputStream is = resource.getInputStream()) { trustedCa = factory.generateCertificate(is); } KeyStore trustStore = KeyStore.getInstance("pkcs12"); trustStore.load(null, null); trustStore.setCertificateEntry("ca", trustedCa); SSLContextBuilder sslContextBuilder = SSLContexts.custom() .loadTrustMaterial(trustStore, null); sslContext = sslContextBuilder.build(); } catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) { log.error("ES连接认证失败", e); } return sslContext; } private static ElasticsearchTransport getElasticsearchTransport(String username, String passwd, HttpHost...hosts) { // 账号密码的配置 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, passwd)); // 自签证书的设置,并且还包含了账号密码 HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder .setSSLContext(buildSSLContext()) .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .setDefaultCredentialsProvider(credentialsProvider); // 用builder创建RestClient对象 RestClient client = RestClient .builder(hosts) .setHttpClientConfigCallback(callback) .build(); return new RestClientTransport(client, new JacksonJsonpMapper()); } private static ElasticsearchTransport getElasticsearchTransport(String apiKey, HttpHost...hosts) { // 将ApiKey放入header中 Header[] headers = new Header[] {new BasicHeader("Authorization", "ApiKey " + apiKey)}; // es自签证书的设置 HttpClientConfigCallback callback = httpAsyncClientBuilder -> h
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SPI机制是如何规避双亲委派机制的.. 下一篇Prototype Pattern —— Creation..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目