java api访问kerberos认证的hbase

去集群机器上拷贝4个文件:

  • core-site.xml (hadoop核心配置)
  • hbase-site.xml (hbase配置)
  • emr.keytab (kerberos密钥信息)
  • krb5.conf (kerberos认证服务器信息)

java代码

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.security.UserGroupInformation;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.io.IOException;

@ConfigurationProperties(prefix = "spring.hbase")
@Configuration
@Data
@Slf4j
public class HbaseConfiguration {
    private String kerberosKeytab;

    private String kerberosUserName;

    private String kerberosConf;

    private String coreSitePath;

    private String hbaseSitePath;

    public org.apache.hadoop.conf.Configuration configuration() throws IOException {
        org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
        System.setProperty("java.security.krb5.conf", kerberosConf);
        System.setProperty("sun.security.krb5.debug", "true");
        conf.set("hadoop.security.authentication", "kerberos");

        conf.addResource(new Path(coreSitePath));
        conf.addResource(new Path(hbaseSitePath));

        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(kerberosUserName, kerberosKeytab);

        return conf;
    }

    @Bean
    @Lazy
    public Admin admin() {
        Admin admin = null;
        try {
            Connection connection = ConnectionFactory.createConnection(configuration());
            admin = connection.getAdmin();
        } catch (IOException e) {
            log.error("hbase admin init error:".concat(e.getMessage()), e);
        }
        return admin;
    }
}

配置文件

spring:
  hbase:
    kerberosKeytab: /home/kerberos/emr-test.keytab
    kerberosUserName: hadoop/10.16.0.74@EMR-G2RIQ4HC
    kerberosConf: /home/kerberos/krb5-test.conf
    coreSitePath: /home/kerberos/core-site-test.xml
    hbaseSitePath: /home/kerberos/hbase-site-test.xml

这里注意下,kerberosUserName属性里是hbase-site.xml里边的hbase.master.hostname与hbase.master.kerberos.principal组合起来的值,不要搞错
hbase.site.xml截图
pom.xml配置,把resource目录下的kerberos文件移动到指定目录,方便在dockerfile文件操作打包镜像,不要直接使用resource下边的文件,程序很容易识别不到,避免出错,少踩坑

    <build>
        <finalName>${project.name}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>kerberos/</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <!--资源管理插件-->
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <!--源文件目录-->
                                    <directory>src/main/resources/kerberos</directory>
                                </resource>
                            </resources>
                            <!--输出的资源目录-->
                            <outputDirectory>${project.build.directory}/kerberos</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

打包后截图

备注:有的人不喜欢用这种xml方式,喜欢用最简配置去搞,从xml里边获取部分参数,直接在代码里边写,这种也没有问题,缺点的话可能缺少某些配置需要重复去测试,浪费时间

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java API连接使用Kerberos认证的Elasticsearch集群,你需要遵循以下步骤: 1. 首先,你需要为你的Java应用程序配置Kerberos认证。这可以通过在你的应用程序中使用JAAS(Java Authentication and Authorization Service)框架来完成。 2. 然后,你需要在Elasticsearch集群中启用Kerberos认证。这可以通过在elasticsearch.yml配置文件中设置以下属性来完成: ``` xpack.security.authc.realms: kerberos.kerb1: type: kerberos order: 0 krb5_file_path: /path/to/krb5.conf keytab_path: /path/to/elasticsearch.keytab ``` 其中,`krb5_file_path`是指向Kerberos配置文件的路径,`keytab_path`是指向Elasticsearch服务的keytab文件路径。 3. 接下来,你需要使用Java API创建一个Elasticsearch客户端,并使用Kerberos认证进行身份验证。以下是一个示例代码: ``` public class KerberosESClient { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); conf.setBoolean("hadoop.security.authentication", true); UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab("your_principal", "/path/to/your/keytab"); Settings settings = Settings.builder() .put("cluster.name", "your_cluster_name") .put("xpack.security.user", "your_username:your_password") .put("client.transport.sniff", true) .build(); TransportClient client = new PreBuiltXPackTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("your_es_host"), 9300)); SearchResponse response = client.prepareSearch().execute().actionGet(); client.close(); } } ``` 在上面的代码中,`UserGroupInformation`类用于从指定的keytab文件中获取Kerberos凭证,然后使用这些凭证创建一个Elasticsearch客户端。`Settings`类用于配置一些连接参数,例如集群名称、节点授权信息等。`TransportClient`类用于实现与Elasticsearch节点的通信,可以使用`prepareSearch`方法发送一个查询请求并获取结果。 希望这个回答能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值