java执行kerberos认证出现的问题以及解决方式

 Runtime.getRuntime().exec();执行cmd命令,且命令涉及到 kinit -kt /etc/xxxxx.keytab xxxx@HADOOP.COM命令

于是偶然出现下面的问题,这种错误的几率很小,大概几百分之一的概率。

百度了一下,说是kinit -kt 并行引起的,所以解决方案就是解决这种并行问题,但是程序必须保证并行,所以代码里写了尝重新执行的方法,将失败率减少到万分之一。

kinit: Failed to store credentials: Internal credentials cache error (filename: /tmp/krb5cc_1645005342) while getting initial credentials
kinit: Failed to store credentials: No credentials cache found (filename: /tmp/krb5cc_1645005342) while getting initial credentials
kinit: Failed to store credentials: Bad format in credentials cache (filename: /tmp/krb5cc_1645005342) while getting initial credentials
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我可以为您提供一个简单的 Java Demo 来连接 Kerberos 认证的 Elasticsearch API。在此之前,请确保您已经配置好了 Kerberos 认证和 Elasticsearch。以下是您可以使用的示例代码: ```java import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xpack.security.authc.Authentication; import org.elasticsearch.xpack.security.authc.AuthenticationException; import org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken; import java.io.IOException; import java.net.InetAddress; import java.util.concurrent.TimeUnit; public class ElasticsearchKerberosDemo { public static void main(String[] args) throws IOException { // Elasticsearch 集群节点的主机名和端口号 HttpHost[] httpHosts = { new HttpHost("localhost", 9200, "http") }; // Kerberos 认证信息 String realm = "EXAMPLE.COM"; String username = "user1"; String password = "password"; // 创建 Kerberos 认证凭证 Authentication auth = new UsernamePasswordToken(username, password); // 创建认证提供程序 CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); // 创建 RestClientBuilder RestClientBuilder builder = RestClient.builder(httpHosts) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }) .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder .setConnectTimeout(5000) .setSocketTimeout(60000)); // 添加 Kerberos 认证 builder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry -> authSchemeRegistry.register("SPNEGO", new KerberosSchemeFactory())); httpClientBuilder.setSPNegoSchemeEnabled(true); return httpClientBuilder; }); // 创建 RestHighLevelClient RestHighLevelClient client = new RestHighLevelClient(builder); // 构建搜索请求 SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("my-index"); searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())); // 执行搜索请求 RequestOptions options = RequestOptions.DEFAULT.toBuilder() .setAuthentication(auth) .build(); client.search(searchRequest, options); // 关闭客户端连接 client.close(); } } ``` 这是一个简单的示例,它使用 Kerberos 认证连接 Elasticsearch,并执行一个简单的搜索请求。您需要将示例代码中的主机名、端口号、认证信息和搜索请求配置更改为您自己的 Elasticsearch 配置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早退的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值