问题: hive安装以后使用dbeaver连接不需要用户名、密码。通过自定义"简单"验证的方式实现用户名密码的登录验证。
JAVA自定义开发:
1. 需要的MAVEN依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 导入hadoop依赖环境 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
2. 实现代码:
import org.apache.commons.lang3.StringUtils;
import org.apache.hive.service.auth.PasswdAuthenticationProvider;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import javax.security.sasl.AuthenticationException;
public class CustomPasswdAuthenticator implements PasswdAuthenticationProvider {
private static final String HIVE_JDBC_PASSWD_AUTH_PREFIX="hive.jdbc_passwd.auth.%s";
private Configuration conf=null;
@Override
public void Authenticate(String userName, String passwd) throws AuthenticationException {
System.out.println("user: "+userName+" try login.");
String passwdConf = getConf().get(String.format(HIVE_JDBC_PASSWD_AUTH_PREFIX, userName));
if (StringUtils.isEmpty(passwdConf)) {
String message = "找不到用户的ACL配置. user:"+userName;
System.out.println(message);
throw new AuthenticationException(message);
}
if(!passwd.equals(passwdConf)){
String message = "用户名和密码不匹配. user:"+userName;
throw new AuthenticationException(message);
}
}
public Configuration getConf() {
if(conf==null){
this.conf=new Configuration(new HiveConf());
}
return conf;
}
public void setConf(Configuration conf) {
this.conf=conf;
}
}
3 将项目打包成jar,之后,将jar包放在hive根目录的lib,可能是:/opt/hive/conf/lib下。如果是CDH的安装方式,jar包放置的位置可以参考:
/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hive/lib
4. 修改配置文件hive-site使自定义的用户名,密码生效
<!--自定义远程连接用户名和密码-->
<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value> <!--默认为none,修改成CUSTOM-->
</property>
<!--指定解析jar包. value是自定义的实现类的全路径-->
<property>
<name>hive.server2.custom.authentication.class</name>
<value>aaa.bbb.CustomPasswdAuthenticator</value>
</property>
<!--设置用户名和密码,如果有多个用户和密码,可以多写几个property -->
<property>
<name>hive.jdbc_passwd.auth.dev_username</name> <!--用户名为最后一个: dev_username >
<value>123456</value> <!--密码-- 123456>
</property>