Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does java access and configure kerberos

Shulou Source: shulou.com Published: 2022-06-02 04:06:08 10月03日 Update

This article introduces the knowledge of "how to access and configure kerberos by java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Prepare for

1.1 generate the keytab file of the principal hive/hive to the specified directory / var/keytab/hive.keytab

[root@fan102] # kadmin.local-Q "xst-k / var/keytab/hive.keytab hive/hive@HADOOP.COM"

1.2 View keytab content

[root@fan102 ~] # cd var/keytab

[root@fan102 keytab] # klist-e-k hive.keytab

Keytab name: FILE:hive.keytab

KVNO Principal

3 hive/hive@HADOOP.COM (aes128-cts-hmac-sha1-96)

3 hive/hive@HADOOP.COM (des3-cbc-sha1)

3 hive/hive@HADOOP.COM (arcfour-hmac)

3 hive/hive@HADOOP.COM (camellia256-cts-cmac)

3 hive/hive@HADOOP.COM (camellia128-cts-cmac)

3 hive/hive@HADOOP.COM (des-hmac-sha1)

3 hive/hive@HADOOP.COM (des-cbc-md5)

1.3 verify that the login is successful

[root@fan102 ~] # kinit-kt / var/keytab/hive.keytab hive/hive@HADOOP.COM

[root@fan102 ~] # klist

Ticket cache: FILE:/tmp/krb5cc_0

Default principal: hive/hive@HADOOP.COM

Valid starting Expires Service principal

07/14/2020 14:08:26 07/15/2020 14:08:26 krbtgt/HADOOP.COM@HADOOP.COM

Renew until 07/21/2020 14:08:26

1.4 copy the kerberos configuration file and keytab file to the Windows local (my path is: d:\ keytab)

[root@fan102 ~] # cat / etc/krb5.conf

[root@fan102 ~] # cat / var/keytab/hive.keytab

1.5 check the contents of the IP location under realms in the krb5.conf file. If it is the server name, replace it with the IP address of the server, which is originally IP.

The address does not need to be modified

[realms]

HADOOP.COM = {

Kdc = IP

Admin_server = IP

}

2.java connection part

2.1 dependency (my version is 2.6)

Org.apache.hadoop

Hadoop-common

2.6.0

Org.apache.hive

Hive-jdbc

1.1.1

2.2Code (replace it with your own IP at IP)

Import org.apache.hadoop.conf.Configuration

Import org.apache.hadoop.security.UserGroupInformation

Import java.sql.*

Public class KerberosTest {

Private static String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver"

Private static String CONNECTION_URL =

"jdbc:hive2://IP:10000/;principal=hive/fan102@HADOOP.COM"

Static {

Try {

Class.forName (JDBC_DRIVER)

} catch (ClassNotFoundException e) {

E.printStackTrace ()

}

}

Public static void main (String [] args) throws Exception {

Class.forName (JDBC_DRIVER)

/ / Log in to the Kerberos account

System.setProperty ("java.security.krb5.conf", "D:\\ keytab\\ krb5.conf")

Configuration configuration = new Configuration ()

Configuration.set ("hadoop.security.authentication", "Kerberos")

UserGroupInformation.setConfiguration (configuration)

UserGroupInformation.loginUserFromKeytab ("hive/hive@HADOOP.COM"

"D:\\ keytab\\ hive.keytab")

Connection connection = null

ResultSet rs = null

PreparedStatement ps = null

Try {

Connection = DriverManager.getConnection (CONNECTION_URL)

Ps = connection.prepareStatement ("show databases")

Rs = ps.executeQuery ()

ResultSetMetaData metaData = null

While (rs.next ()) {

MetaData = rs.getMetaData ()

System.err.println (rs.getString (1))

}

System.err.println (metaData.getColumnName (1))

} catch (Exception e) {

E.printStackTrace ()

}

}

}

3 results

3.1 java query results

3.2 on Linux, beeline verification results

Note: 1.3 there is no exception in the step operation.

[root@fan102 ~] # beeline-u "jdbc:hive2://fan102:10000/;principal=hive/fan102@HADOOP.COM"

> show databases

This is the end of the content of "how to access and configure kerberos for java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Content file configuration result address more server knowledge service login authentication utility success learning next subject code location original dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information MySQL Linux Huawei