SpringBoot集成HBase-client的票据认证

背景说明:在SpringBoot初使化时加载票据信息,拿着票据信息到HBase服务端进行认证,依赖文件

        
            org.apache.hbase
            hbase-client
            2.1.1
        

        
            org.springframework.data
            spring-data-hadoop-hbase
            2.5.0.RELEASE
        

1.MyServletContextListener 继承ServletContextListener,重写contextInitialized方法

@Configuration
public class MyServletContextListener implements ServletContextListener {
	
	//初始化
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("-------------开始执行指定任务-------------------");
        //加载外部配置文件参数
//		File file = new File(System.getProperty("user.dir")+
        File.separator+"config"+File.separator+"hbase.properties");
		HbaseConfig.load("application.properties");
		try {
			new HBaseAPI().getConnection();// 连接HBase
		} catch (Exception e) {
			System.out.println("连接HBase 异常----------------------------" + "\n"
					+ e.getMessage());
		}
		System.out.println("--------------结束执行指定任务------------------");
	}
	
	//环境初始化
	public void contextDestroyed(ServletContextEvent arg0) {
		System.err.println("销毁程序");
	}
}

2.HbaseConfig类

import org.springframework.core.io.ClassPathResource;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class HbaseConfig {

    public static String QUORUM = "";

	public static String CLIENTPORT = "";

	public static String KEYTAB_PATH = "";

	public static String PRINCAL_NAME = "";

	public static String PRINCAL_SUFFIX = "";

	public static String KRB5_CONF = "";

    public static void load(String path) {
        try {
            ClassPathResource cpr = new ClassPathResource(path);
            Properties prop = new Properties();
            InputStream in;
            try {
                in = new BufferedInputStream(cpr.getInputStream());
                prop.load(in);
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 读取配置
            QUORUM = prop.getProperty("hbase.quorum").trim();
            CLIENTPORT = prop.getProperty("hbase.clientPort").trim();
            KEYTAB_PATH = prop.getProperty("hbase.keytab_path").trim();
            PRINCAL_NAME = prop.getProperty("hbase.princal_name").trim();
            PRINCAL_SUFFIX = prop.getProperty("hbase.princal_suffix").trim();
            KRB5_CONF = prop.getProperty("hbase.krb5_conf").trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.HBaseAPI工具类

import com.hvgroup.chinamobile.service.config.HbaseConfig;
import com.hvgroup.chinamobile.service.constant.ZuJiConstant;
import com.hvgroup.chinamobile.service.vo.ReturnJsonVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class HBaseAPI {
	private static Logger logger = LoggerFactory.getLogger(HBaseAPI.class);
	private static Configuration conf = null;
	private static Connection conn = null;

	/**
	 * 获取全局唯一的Configuration实例
	 * @return
	 */
	static {
		// 连接HBase
		try {
			conn = getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		if(conf == null){
			System.out.println("qu:"+ HbaseConfig.QUORUM);
			conf = HBaseConfiguration.create();
			Configuration.addDefaultResource("core-site.xml");
		    Configuration.addDefaultResource("hbase-site.xml");
		    Configuration.addDefaultResource("hdfs-site.xml");
		    conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
		    System.out.println("ha.zookeeper.acl---->" + conf.get("ha.zookeeper.acl"));
		    System.out.println("fs.hdfs.impl---->" + conf.get("fs.hdfs.impl"));
			conf.set("hbase.zookeeper.quorum", HbaseConfig.QUORUM);
			System.out.println("hbase.zookeeper.quorum---->" + conf.get("hbase.zookeeper.quorum"));
			conf.set("hbase.zookeeper.property.clientPort", HbaseConfig.CLIENTPORT);
			System.out.println("clientPort---->" + conf.get("hbase.zookeeper.property.clientPort"));
			System.out.println("KRB5_CONF-------------->"+HbaseConfig.KRB5_CONF);
			System.setProperty("java.security.krb5.conf", HbaseConfig.KRB5_CONF);
			conf.set("hadoop.security.authentication", "kerberos");
			conf.set("hbase.security.authentication", "kerberos");
			conf.set("hbase.security.authorization", "true");
			conf.set("zookeeper.znode.parent", "/hbase-secure");
			conf.set("hbase.master.kerberos.principal", "hbase/_HOST@"+HbaseConfig.PRINCAL_SUFFIX);
			conf.set("hbase.regionserver.kerberos.principal", "hbase/_HOST@"+HbaseConfig.PRINCAL_SUFFIX);
			System.out.println("hbase.master.kerberos.principal---->"+conf.get("hbase.master.kerberos.principal"));
			System.out.println("KEYTAB_PATH--------->"+HbaseConfig.KEYTAB_PATH);
			System.out.println("PRINCAL_NAME-------->"+HbaseConfig.PRINCAL_NAME);
		}else {
			System.out.println("conf================>"+conf);
			System.out.println("KEYTAB_PATH=========>"+HbaseConfig.KEYTAB_PATH);
			System.out.println("PRINCAL_NAME========>"+HbaseConfig.PRINCAL_NAME);
		}
		
		UserGroupInformation.setConfiguration(conf);
		if (!"".equals(HbaseConfig.KEYTAB_PATH) && !"".equals(HbaseConfig.PRINCAL_NAME)) {

			try {
				UserGroupInformation ugi = UserGroupInformation
						.loginUserFromKeytabAndReturnUGI(HbaseConfig.PRINCAL_NAME, HbaseConfig.KEYTAB_PATH);
				System.out.println("ugi-------------------->"+ugi);
				ugi.doAs(new PrivilegedAction() {
					@Override
					public Object run() {
						try {
							System.out.println("==========createConnection==============");
							conn = ConnectionFactory.createConnection(HBaseConfiguration.create(conf));
							System.out.println("==========createConnection Success==============");
						} catch (IOException e) {
							System.out.println("==========createConnection error==============");
							e.printStackTrace();
						}
						return null;
					}
				});
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else {
			System.out.println("请在配置文件配置kerberos认证相关的princal和keytab!");
		}
		return conn;
	}

    	/**
	 * 查找一行记录
	 */
	public static Map getUseDate(String tableName, String rowKey) throws IOException {
		if (conn == null) {
			System.out.println("tableExist -- HBase 连接为空,重新连接。。。。");
			conn = getConnection();
		}
		Map map = new HashMap();
		Table table = conn.getTable(TableName.valueOf(tableName));
		Get get = new Get(rowKey.getBytes());
		Result rs = table.get(get);
		Cell[] cells = rs.rawCells();
		for (Cell cell : cells) {
			String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
			String quali = Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
			System.out.println(tableName+": quali:"+quali+"== value:"+value);
			if(quali != null){
					map.put(quali.toUpperCase(), value);
			}
		}
		return map;
	}

} 
  

 

你可能感兴趣的:(SpringBoot,client,hbase认证)