Directorio de artículos
1. Descripción del sitio web oficial
Configuración de autenticación/seguridad
HiveServer2 es compatible con Anónimo (sin autenticación) con y sin SASL, Kerberos (GSSAPI), paso a través de LDAP, autenticación personalizada conectable y módulos de autenticación conectables (PAM, compatible con Hive 0.13 en adelante).
Modo de autenticación:
hive.server2.authentication: modo de autenticación, predeterminado NINGUNO. Las opciones son NINGUNA (usa SASL simple), NOSASL, KERBEROS, LDAP, PAM y CUSTOM.
Establezca lo siguiente para el modo PERSONALIZADO:
hive.server2.custom.authentication.class: clase de autenticación personalizada que implementa la interfaz org.apache.hive.service.auth.PasswdAuthenticationProvider.
官网:Configuración de HiveServer2-Autenticación/Configuración de seguridad
dos, darse cuenta
Darse cuenta de la lógica:
- Implementar la interfaz org.apache.hive.service.auth.PasswdAuthenticationProvider, empaquetarla y colocarla en >${hive_home}/lib
- Configurar hive-site.xml en cada instalación de hiveserver
- reiniciar servidor hive
Clase de implementación:
la lógica es muy simple, lea la configuración de hive.jdbc_passwd.auth e identifique el usuario y la contraseña. Se lanza una excepción si no hay configuración o si la coincidencia no tiene éxito.
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hive.service.auth.PasswdAuthenticationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.security.sasl.AuthenticationException;
/**
* @Description TODO
* @Author lianggao
* @Date 2023/6/12 下午3:45
*/
public class CustomPasswdAuthenticator implements PasswdAuthenticationProvider {
private Logger LOG = LoggerFactory.getLogger(CustomPasswdAuthenticator.class);
private static final String HIVE_JDBC_PASSWD_AUTH_PREFIX = "hive.jdbc_passwd.auth.%s";
private Configuration conf = null;
public CustomPasswdAuthenticator() {
}
public void Authenticate(String userName, String passwd) throws AuthenticationException {
LOG.info("user: " + userName + " try login.");
String passwdConf = this.getConf().get(String.format("hive.jdbc_passwd.auth.%s", userName));
String message;
if (passwdConf == null) {
message = "user's ACL configration is not found. user:" + userName;
LOG.info(message);
throw new AuthenticationException(message);
} else if (!passwd.equals(passwdConf)) {
message = "user name and password is mismatch. user:" + userName;
throw new AuthenticationException(message);
}
}
public Configuration getConf() {
if (this.conf == null) {
this.conf = new Configuration(new HiveConf());
}
return this.conf;
}
public void setConf(Configuration conf) {
this.conf = conf;
}
}
Hive-site.xml modificado
<!--自定义远程连接用户名和密码-->
<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value><!--默认为none,修改成CUSTOM-->
</property>
<!--指定解析jar包-->
<property>
<name>hive.server2.custom.authentication.class</name>
<value>org.apache.hadoop.hive.contrib.auth.CustomPasswdAuthenticator</value>
</property>
<!--设置用户名和密码-->
<property>
<name>hive.jdbc_passwd.auth.usename1111</name><!--用户名识别为:usename1111-->
<value>pswd1111</value><!--密码-->
</property>
reiniciar servidor hive
jps
kill -9 hive_server_pid
nohup ./hive --service hiveserver2 >> /tmp/hiveserver2.log 2>&1
Conecte la prueba de colmena a través de beeline:
beeline -u jdbc:hive2://hostname:10000 -n usename1111 -p pswd1111
3. La segunda realización
El nombre de usuario y la contraseña se pueden escribir en la clase de implementación por adelantado, de modo que la configuración de hive-site.xml sea más concisa
package hive.test;
import java.util.Hashtable;
import javax.security.sasl.AuthenticationException;
import org.apache.hive.service.auth.PasswdAuthenticationProvider;
/*
javac -cp $HIVE_HOME/lib/hive-service-0.11-mapr.jar SampleAuthenticator.java -d .
jar cf sampleauth.jar hive
cp sampleauth.jar $HIVE_HOME/lib/.
*/
public class SampleAuthenticator implements PasswdAuthenticationProvider {
Hashtable<String, String> store = null;
public SampleAuthenticator () {
store = new Hashtable<String, String>();
store.put("user1", "passwd1");
store.put("user2", "passwd2");
}
@Override
public void Authenticate(String user, String password)
throws AuthenticationException {
String storedPasswd = store.get(user);
if (storedPasswd != null && storedPasswd.equals(password))
return;
throw new AuthenticationException("SampleAuthenticator: Error validating user");
}
}
colmena-sitio.xml
<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value>
</property>
<property>
<name>hive.server2.custom.authentication.class</name>
<value>hive.test.SampleAuthenticator</value>
</property>
Referencia:
https://docs.ezmeral.hpe.com/datafabric-customer-managed/72/Hive/HiveServer2-CustomAuth.html