
# 访问HBase ThriftServer安全认证
#### 操作场景
HBase把Thrift结合起来可以向外部应用提供HBase服务。在HBase服务安装时可选部署ThriftServer实例，ThriftServer系统可访问HBase的用户，拥有HBase所有NameSpace和表的读、写、执行、创建和管理的权限。访问ThriftServer服务同样需要进行Kerberos认证。HBase实现了两套Thrift Server服务，此处"hbase-thrift-example"为ThriftServer实例服务的调用实现。
#### 前提条件
已获取样例工程运行所需的配置文件及认证文件，详细操作请参见[准备连接HBase集群配置文件](https://support.huaweicloud.com/devg3-mrs/mrs_07_080085.html)。
#### 配置样例代码
- 代码认证
  以下代码在"hbase-thrift-example"样例工程的"com.huawei.bigdata.hbase.examples"包的"TestMain"类中。
  ```
  private static void init() throws IOException {
          // Default load from conf directory
          conf = HBaseConfiguration.create();
          String userdir = TestMain.class.getClassLoader().getResource("conf").getPath() + File.separator;[1]
          //In Linux environment
          //String userdir = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
          conf.addResource(new Path(userdir + "core-site.xml"), false);
          conf.addResource(new Path(userdir + "hdfs-site.xml"), false);
          conf.addResource(new Path(userdir + "hbase-site.xml"), false);
  }
  ```
  \[1\]**userdir**获取的是编译后资源路径下conf目录的路径。
  

- 安全登录
  请根据实际情况，修改"userName"为实际用户名，例如"developuser"。
  ```
  private static void login() throws IOException {
          if (User.isHBaseSecurityEnabled(conf)) {
              userName = " developuser ";
              //In Windows environment
              String userdir = TestMain.class.getClassLoader().getResource("conf").getPath() + File.separator;
              //In Linux environment
              //String userdir = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
              userKeytabFile = userdir + "user.keytab";
              krb5File = userdir + "krb5.conf";
   
              /*
               * if need to connect zk, please provide jaas info about zk. of course,
               * you can do it as below:
               * System.setProperty("java.security.auth.login.config", confDirPath +
               * "jaas.conf"); but the demo can help you more : Note: if this process
               * will connect more than one zk cluster, the demo may be not proper. you
               * can contact us for more help
               */
              LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, userName, userKeytabFile);
              LoginUtil.login(userName, userKeytabFile, krb5File, conf);
          }
  }
  ```
  
- 连接ThriftServer实例
  ```
  try {    
          test = new ThriftSample();    
          test.test("10.120.16.170", THRIFT_PORT, conf);[2]
      } catch (TException | IOException e) {
          LOG.error("Test thrift error", e);
      }
  ```
  \[2\]test.test()传入参数为待访问的ThriftServer实例所在节点ip地址，需根据实际运行集群情况进行修改，且该节点ip需要配置到运行样例代码的本机hosts文件中。
  "THRIFT_PORT"为ThriftServer实例的配置参数"hbase.regionserver.thrift.port"对应的值。ThriftServer实例所在节点IP地址可通过登录FusionInsight Manager，选择"集群 \> 服务 \> HBase \> 实例"获取。
  
 
