通过http协议验证动态刷新token后调用接口回传数据

如题,需求如下:
现要求通过调用接口将hive表中的数据回传,但首先要通过token验证,而且token是一天后过期,refresh_token30天过期,而若想要获取最新token值,则需要调用刷新token的接口并将刷新后的token和refresh_token存入数据库以便后续直接从数据库获取。

流程分析:首先,因为接口需要token验证,所以需要先获取token,而获取token则通过授权码auth_code调用另一接口获取,接口返回token和refresh_token,然后调用刷新token的接口,将refresh_token传入,即可刷新token,并将两token存入数据库。最后使用最新token调用接口将hive表里的数据回传。

1.首先创建工具类:HIVE,HTTP以及MYSQL工具类。(示例均为通用工具类)
代码如下:(因数据库连接每天就连接三次,所以这里没创建连接池)
HttpUtils:

public class HttpUtils {
   
	public static String postJson(String url, Object userEntity,String token) throws ParseException, IOException {
   
		
		RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
        CloseableHttpResponse response = null;
        HttpEntity responseEntity=null;
        String res = "";
        try {
   
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(3600000) //服务器响应超时时间
                    .setConnectTimeout(60000) //连接服务器超时时间
                    .build();
            httpPost.setConfig(requestConfig);

            StringEntity entity = new StringEntity(JSON.toJSONString(userEntity), "utf-8");//也可以直接使用JSONObject
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            httpPost.setHeader("Authorization",token);
            // 由客户端执行(发送)请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            responseEntity = response.getEntity();
            if (responseEntity != null) {
   
            	res= EntityUtils.toString(responseEntity);
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                // 释放资源
                if (httpClient != null) {
   
                    httpClient.close();
                }
                if (response != null) {
   
                    response.close();
                }
            } catch (IOException e) {
   
                e.printStackTrace();
            }
        }
        return res;
    }

public static String get(String url, String token) throws ParseException, IOException {
   
		RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
        CloseableHttpResponse response = null;
        HttpEntity responseEntity=null;
        String res = "";
        try {
   
            HttpGet httpget = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(2000) //服务器响应超时时间
                    .setConnectTimeout(2000) //连接服务器超时时间
                    .build();
            httpget.setConfig(requestConfig);
            httpget.setHeader("Content-Type", "application/json;charset=utf8");
            httpget.setHeader("Authorization",token);
            // 由客户端执行(发送)请求
            response = httpClient.execute(httpget);
            // 从响应模型中获取响应实体
            responseEntity = response.getEntity();
            if (responseEntity != null) {
   
            	res=EntityUtils.toString(responseEntity);
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                // 释放资源
                if (httpClient != null) {
   
                    httpClient.close();
                }
                if (response != null) {
   
                    response.close();
                }
            } catch (IOException e) {
   
                e.printStackTrace();
            }
        }
        return res;
	}
}

HiveUtils:

public class DBUtil {
   

    public  static String url = "jdbc:hive2://IP:端口/数据库";
	public	static String username = "";
	public	static String password = "";

    public  static String pro_url = "jdbc:hive2://IP:端口/数据库";
    public	static String pro_username = "";
    public	static String pro_password = "";

	public	static String driverClassName = "org.apache.hive.jdbc.HiveDriver";

	/**
	 * 获取源数据库连接
	 * @return
	 */
	public static Connection getSourceConn() {
   
		Log log = LogFactory.getLog(DBUtil.class);

		Connection conn=null;
		try {
   
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
   
			log.error("连接创建失败, 请检查[url]:" + url + ", [user]:" + username + ", [password]:" + password + ")");
		}
		return conn;
	}
	
	/**
	 * 获取目标数据库连接
	 * @return
	 */
	public static Connection getTargetConn(String ispro) {
   
		Log log = LogFactory.getLog(DBUtil.class);
		
		Connection conn=null;

		String curr_url;
		String curr_username;
		String curr_password;

		if("pro".equals(ispro)){
   
            curr_url=pro_url;
            curr_username=pro_username;
            curr_password=pro_password;
        
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值