java api连接kerberos认证的es 生成一个demo,要求使用最新的java api
时间: 2024-02-17 14:05:23 浏览: 188
好的,我可以为您提供一个 Java API 连接 Kerberos 认证的 Elasticsearch 的示例代码。
首先,您需要在 Maven 项目中添加以下 Elasticsearch 和 Kerberos 相关的依赖:
```
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.15.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>com.sun.security.auth.module</groupId>
<artifactId>jaas</artifactId>
<version>1.8.0_212</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<version>3.3.1</version>
</dependency>
```
接下来,您需要创建一个 `RestHighLevelClient` 对象并为其配置 Kerberos 认证,示例代码如下:
```
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.sniff.Sniffer;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken;
import org.elasticsearch.xpack.security.authc.AuthenticationToken;
import org.elasticsearch.xpack.security.authc.support.DefaultAuthenticationFailureHandler;
import org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken;
import org.elasticsearch.xpack.security.transport.netty4.SecurityNetty4Transport;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.security.PrivilegedAction;
import java.util.concurrent.TimeUnit;
public class ElasticsearchKerberosClient {
private final String clusterName;
private final String[] nodeIps;
private final String realm;
private final String username;
private final String password;
private final String serviceName;
private final String keytabPath;
private RestHighLevelClient client;
private Sniffer sniffer;
public ElasticsearchKerberosClient(String clusterName, String[] nodeIps, String realm, String username, String password, String serviceName, String keytabPath) {
this.clusterName = clusterName;
this.nodeIps = nodeIps;
this.realm = realm;
this.username = username;
this.password = password;
this.serviceName = serviceName;
this.keytabPath = keytabPath;
}
public void init() throws Exception {
final Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(username, keytabPath);
final Settings settings = Settings.builder()
.put("cluster.name", clusterName)
.put("xpack.security.user", username + ":" + password)
.put(SecurityNetty4Transport.SSL_KEYSTORE_PASSWORD_SETTING.getKey(), password)
.put(SecurityNetty4Transport.SSL_TRUSTSTORE_PASSWORD_SETTING.getKey(), password)
.put(SecurityNetty4Transport.SSL_KEYSTORE_PATH_SETTING.getKey(), "path/to/keystore.jks")
.put(SecurityNetty4Transport.SSL_TRUSTSTORE_PATH_SETTING.getKey(), "path/to/truststore.jks")
.put(SecurityNetty4Transport.SSL_KEYSTORE_TYPE_SETTING.getKey(), "JKS")
.put(SecurityNetty4Transport.SSL_TRUSTSTORE_TYPE_SETTING.getKey(), "JKS")
.put(SecurityNetty4Transport.SSL_HTTP_ENABLED_SETTING.getKey(), true)
.put(SecurityNetty4Transport.SSL_TRANSPORT_ENABLED_SETTING.getKey(), true)
.put(SecurityNetty4Transport.SSL_KEYSTORE_KEY_PASSWORD_SETTING.getKey(), password)
.put("xpack.security.transport.ssl.verification_mode", "certificate")
.put("xpack.security.transport.ssl.enabled", true)
.put("xpack.security.authc.token.enabled", true)
.put("xpack.security.authc.realms.kerberos.krb5.files", "/etc/krb5.conf")
.put("xpack.security.authc.realms.kerberos.krb5.service_name", serviceName)
.put("xpack.security.authc.realms.kerberos.acceptor_principal", "HTTP/_HOST@REALM")
.build();
final RestClient restClient = RestClient.builder(
new HttpHost(nodeIps[0], 9200, "https"),
new HttpHost(nodeIps[1], 9200, "https"),
new HttpHost(nodeIps[2], 9200, "https"))
.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder;
})
.setRequestConfigCallback(requestConfigBuilder ->
requestConfigBuilder.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(1))
.setConnectionRequestTimeout((int) TimeUnit.MINUTES.toMillis(1))
.setSocketTimeout((int) TimeUnit.MINUTES.toMillis(1)))
.build();
final ThreadContext threadContext = new ThreadContext(settings);
final DefaultAuthenticationFailureHandler failureHandler = new DefaultAuthenticationFailureHandler(settings, threadContext);
final Sniffer sniffer = Sniffer.builder(restClient)
.setSniffIntervalMillis(30000)
.setFailureListener(new Sniffer.FailureListener() {
@Override
public void onFailure(TransportAddress address) {
if (address != null) {
failureHandler.authenticationFailed(address.toString(), null);
}
}
})
.build();
this.sniffer = sniffer;
final RestHighLevelClient client = new RestHighLevelClient(restClient) {
@Override
public AuthenticationToken authenticate(AuthenticationToken token) {
if (token instanceof UsernamePasswordToken) {
final UsernamePasswordToken upToken = (UsernamePasswordToken) token;
final String upTokenUsername = upToken.username();
final String upTokenPassword = new String(upToken.credentials().clone());
return new UsernamePasswordToken(upTokenUsername, upTokenPassword.toCharArray());
} else {
return token;
}
}
};
this.client = client;
}
public void close() {
try {
if (client != null) {
client.close();
}
if (sniffer != null) {
sniffer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public RestHighLevelClient getClient() {
return client;
}
public static void main(String[] args) throws Exception {
final String clusterName = "elasticsearch";
final String[] nodeIps = {"127.0.0.1", "127.0.0.2", "127.0.0.3"};
final String realm = "YOUR-REALM.COM";
final String username = "YOUR-USERNAME";
final String password = "YOUR-PASSWORD";
final String serviceName = "elasticsearch";
final String keytabPath = "/path/to/keytab";
final ElasticsearchKerberosClient esClient = new ElasticsearchKerberosClient(clusterName, nodeIps, realm, username, password, serviceName, keytabPath);
esClient.init();
final RestHighLevelClient client = esClient.getClient();
// TODO: 使用 client 对象进行 Elasticsearch 查询操作
esClient.close();
}
}
```
请根据您的实际情况,修改示例代码中的参数和配置。
阅读全文