欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java HDFS Kerberos 认证

程序员文章站 2022-03-24 11:41:24
...

Kerberos 认证

代码如下:

package com.gridsum.datasocket;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;

public class Kerberos {


    public static final String USER_KEY = "[email protected]";
    public static final String KEY_TAB_PATH = "test.keytab";

    static Configuration conf = new Configuration();

    static {
        System.setProperty("java.security.krb5.conf", "D:\\krb5.conf");
        conf.set("hadoop.security.authentication", "kerberos");
        conf.set("fs.defaultFS", "hdfs://192.168.20.1:8020");

        try {
            UserGroupInformation.setConfiguration(conf);
            UserGroupInformation.loginUserFromKeytab(USER_KEY, KEY_TAB_PATH);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        RemoteIterator<LocatedFileStatus> it = fs.listFiles(new Path("/user/"), true);
        while (it.hasNext()){
            LocatedFileStatus next = it.next();
            System.out.println(next.getPath().getName());

        }
    }
}

主要是3行

        System.setProperty("java.security.krb5.conf", "D:\\krb5.conf");//配置krb5.conf的路径
        conf.set("hadoop.security.authentication", "kerberos"); //配置认证方式,有kerberos和simple两种
        conf.set("fs.defaultFS", "hdfs://192.168.20.1:8020");//namenode的地址和端口

System.setProperty("java.security.krb5.conf", "D:\\krb5.conf") 这一行必须要有,否则会报以下错误

java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalArgumentException: Can't get Kerberos realm
    at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:65)
    at org.apache.hadoop.security.UserGroupInformation.initialize(UserGroupInformation.java:277)
    at org.apache.hadoop.security.UserGroupInformation.setConfiguration(UserGroupInformation.java:313)
    at com.gridsum.datasocket.Kerberos.<clinit>(Kerberos.java:26)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.hadoop.security.authentication.util.KerberosUtil.getDefaultRealm(KerberosUtil.java:84)
    at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:63)
    ... 3 more
Caused by: KrbException: Cannot locate default realm
    at sun.security.krb5.Config.getDefaultRealm(Config.java:1029)
    ... 9 more
Exception in thread "main"