---
title: "hive 配置自定义用户安全认证"
author: "kingding"
date: "2019-08-13T11:12:47Z"
tags: []
categories: ["行业应用"]
slug: "hive"
draft: false
lastmod: "2019-08-13T11:18:35Z"
id: 2443501
url: "https://kingding.iteye.com/blog/2443501"
---

HiveServer2提供了JDBC链接操作Hive的功能，非常实用，但如果在使用HiveServer2时候，不注意安全控制，将非常危险，因为任何人都可以作为超级用户来操作Hive及HDFS数据。  
比如：在配置HiveServer2的时候，hive.server2.authentication=NONE，表示没有用户认证。

使用beeline，模拟成超级用户hadoop，成功连接到HiveServer2.  
创建数据库lxw1234;  
0: jdbc:hive2://localhost:10000&gt; create database lxw1234;  
No rows affected (0.157 seconds)  
0: jdbc:hive2://localhost:10000&gt;

HDFS上也是以超级用户hadoop创建的目录。  
再执行drop database，同样没问题。  
0: jdbc:hive2://localhost:10000&gt; drop database lxw1234;  
No rows affected (0.142 seconds)  
0: jdbc:hive2://localhost:10000&gt;

如果是以普通用户链接HiveServer2,执行创建数据库，则会报权限错误，因为普通用户没有在

因此，如果使用HiverServer2来提供给用户来链接Hive，必须启用安全认证，也就是hive.server2.authentication的配置。

目前HiveServer2支持多种用户安全认证方式：NONE,NOSASL, KERBEROS, LDAP, PAM ,CUSTOM等等。

本文介绍使用自定义的用户认证方式，即CUSTOM；  
如果将hive.server2.authentication设置成CUSTOM，则需要设置  
hive.server2.custom.authentication.class来指定用于权限认证的类，这个类需要实现  
org.apache.hive.service.auth.PasswdAuthenticationProvider接口。

我们将使用HiveServer2的用户名和密码保存起来，其中，密码以32位小写md5加密来保存，这个数据即可以保存在Hive元数据库中，也可以保存在一个配置文件中。为了方便起见，这里使用配置文件来保存。

首先需要编写用户权限验证的类：

```
 
```

01. package com.lxw1234.hive.auth;
02. import java.io.BufferedReader;
03. import java.io.File;
04. import java.io.FileReader;
05. import java.io.IOException;
06. import java.security.MessageDigest;
07. import java.security.NoSuchAlgorithmException;
08. import javax.security.sasl.AuthenticationException;
09. import org.apache.hadoop.conf.Configuration;
10. import org.apache.hadoop.hive.conf.HiveConf;
11. import org.apache.hive.service.auth.PasswdAuthenticationProvider;
12. public class CustomHiveServer2Auth implements PasswdAuthenticationProvider  {
13. @Override
14. public void Authenticate(String username, String password)
15. throws AuthenticationException {
16. boolean ok = false;
17. String passMd5 = new MD5().md5(password);
18. HiveConf hiveConf = new HiveConf();
19. Configuration conf = new Configuration(hiveConf);
20. String filePath = conf.get("hive.server2.custom.authentication.file");
21. System.out.println("hive.server2.custom.authentication.file \[" + filePath + "] ..");
22. File file = new File(filePath);
23. BufferedReader reader = null;
24. try {
25. reader = new BufferedReader(new FileReader(file));
26. String tempString = null;
27. while ((tempString = reader.readLine()) != null) {
28. String\[] datas = tempString.split(",", -1);
29. if(datas.length != 2) continue;
30. //ok
31. if(datas\[0].equals(username) && datas\[1].equals(passMd5)) {
32. ok = true;
33. break;
34. }
35. }
36. reader.close();
37. } catch (Exception e) {
38. e.printStackTrace();
39. throw new AuthenticationException("read auth config file error, \[" + filePath + "] ..", e);
40. } finally {
41. if (reader != null) {
42. try {
43. reader.close();
44. } catch (IOException e1) {}
45. }
46. }
47. if(ok) {
48. System.out.println("user \[" + username + "] auth check ok .. ");
49. } else {
50. System.out.println("user \[" + username + "] auth check fail .. ");
51. throw new AuthenticationException("user \[" + username + "] auth check fail .. ");
52. }
53. }
54. //MD5加密
55. class MD5 {
56. private MessageDigest digest;
57. private char hexDigits\[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
58. public MD5() {
59. try {
60. digest = MessageDigest.getInstance("MD5");
61. } catch (NoSuchAlgorithmException e) {
62. throw new RuntimeException(e);
63. }
64. }
65. public String md5(String str) {
66. byte\[] btInput = str.getBytes();
67. digest.reset();
68. digest.update(btInput);
69. byte\[] md = digest.digest();
70. // 把密文转换成十六进制的字符串形式
71. int j = md.length;
72. char strChar\[] = new char\[j * 2];
73. int k = 0;
74. for (int i = 0; i &lt; j; i++) {
75. byte byte0 = md\[i];
76. strChar\[k++] = hexDigits\[byte0 &gt;&gt;&gt; 4 & 0xf];
77. strChar\[k++] = hexDigits\[byte0 & 0xf];
78. }
79. return new String(strChar);
80. }
81. }
82. }

将上面的程序打包成HiveServer2Auth.jar，放到$HIVE\_HOME/lib下，

注意：附件中已有打包好的jar  
在hive-site.xml中设置以下参数：  
&lt;property&gt;  
&lt;name&gt;hive.server2.authentication&lt;/name&gt;  
&lt;value&gt;CUSTOM&lt;/value&gt;  
&lt;/property&gt;  
&lt;property&gt;  
&lt;name&gt;hive.server2.custom.authentication.class&lt;/name&gt;  
&lt;value&gt;com.lxw1234.hive.auth.CustomHiveServer2Auth&lt;/value&gt;  
&lt;/property&gt;  
&lt;property&gt;  
&lt;name&gt;hive.server2.custom.authentication.file&lt;/name&gt;  
&lt;value&gt;/usr/local/apache-hive-0.13.1-bin/conf/hive.server2.users.conf&lt;/value&gt;  
&lt;/property&gt;

在$HIVE\_HOME/conf下新建文件hive.server2.users.conf，里面写入内容：  
\[root@dev conf]# cat hive.server2.users.conf  
lxw1234,48d9a656690e1b1bf5ddee4c12d1bbd7  
user,5f4dcc3b5aa765d61d8327deb882cf99

其中，48d9a656690e1b1bf5ddee4c12d1bbd7为lxw1234\_password的md5加密，  
5f4dcc3b5aa765d61d8327deb882cf99为password的md5加密。

接下来，重新启动HiveServer2，使用beeline连接