常用工具类---SQL工具,HTTP工具

本文涉及的产品
Serverless 应用引擎 SAE,800核*时 1600GiB*时
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
云原生网关 MSE Higress,422元/月
简介: SQL工具,HTTP工具,两个实用小工具~~~

是否包含sql关键字

public static boolean sqlValidate(String str) {
    if (null == str || "".equals(str)) {
        return false;
    }
    str = str.toLowerCase();// 统一转为小写
    String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|*|%|chr|mid|master|truncate|"
        + "char|declare|sitename|net user|xp_cmdshell|;|or|-|+|,|like'|and|exec|execute|insert|create|drop|"
        + "table|from|grant|use|group_concat|column_name|"
        + "information_schema.columns|table_schema|union|where|select|delete|update|order|by|count|*|"
        + "chr|mid|master|truncate|char|declare|or|;|-|--|+|,|like|//|/|%|#";// 过滤掉的sql关键字,可以手动添加
    String[] badStrs = badStr.split("\\|");
    for (int i = 0; i < badStrs.length; i++) {
        if (str.indexOf(badStrs[i]) >= 0) {
            return true;
        }
    }
    return false;
}

HTTP-GET带header请求

// authorization为授权验证,若无授权验证可删除
private static JSONObject sendGet(String url, String authorization) throws IOException {
    CloseableHttpClient cHttpClient = HttpClients.createDefault();
    HttpGet get = new HttpGet(url);
    get.addHeader("Content-Type", "application/json");
    get.addHeader("Authorization", authorization);
    CloseableHttpResponse response = cHttpClient.execute(get);
    logger.info("Http Comunication end ! code --> " + response.getStatusLine().getStatusCode());
    HttpEntity entity = response.getEntity();
    String responseContent = EntityUtils.toString(entity, "UTF-8");
    logger.info("URL=" + url + ",response=" + responseContent);
    response.close();
    cHttpClient.close();
    return JSONObject.parseObject(responseContent);
}

HTTP-POST带header请求

private static JSONObject sendPost(String businessUrl, JSONObject sendMsgBody, String accessToken) throws IOException {
    CloseableHttpClient cHttpClient = HttpClients.createDefault();
    HttpPost post = new HttpPost(businessUrl);
    post.addHeader("Content-Type", "application/json");
    post.addHeader("Authorization", "Bearer " + accessToken);
    post.setEntity(new StringEntity(sendMsgBody.toString(), "UTF-8"));
    CloseableHttpResponse response = cHttpClient.execute(post);
    logger.info("Http Comunication end ! code --> " + response.getStatusLine().getStatusCode());
    HttpEntity entity = response.getEntity();
    String responseContent = EntityUtils.toString(entity, "UTF-8");
    logger.info("URL=" + businessUrl + ",response=" + responseContent);
    response.close();
    cHttpClient.close();
    return JSONObject.parseObject(responseContent);
}

HTTPS-POST带header请求

 //设置链接超时和请求超时等参数,否则会长期停止或者崩溃
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();


public static String sendHttpsPost(String url, JSONObject params) {
    String responseContent = null;
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse httpResponse = null;
    try {
        HttpPost httpPost = new HttpPost(url);
        // header
        httpPost.addHeader("AppKey", SystemConstants.APP_KEY);
        httpPost.addHeader("Secret", SystemConstants.SECRET);
        // body
        httpPost.setEntity(new StringEntity(params.toString(), "UTF-8"));
        httpClient = HttpClients.custom().setSSLSocketFactory(createSslConnSocketFactory()).setDefaultRequestConfig(requestConfig).build();
        httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            responseContent = EntityUtils.toString(httpEntity, "UTF-8");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if(null != httpResponse) {
                httpResponse.close();
            }
            if (null != httpClient) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return responseContent;
}

 /**
     * 创建SSL安全连接
     * @return SSLConnectionSocketFactory
     */
private static SSLConnectionSocketFactory createSslConnSocketFactory() {
    SSLConnectionSocketFactory sslsf = null;
    try {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
        sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }

            @Override
            public void verify(String host, SSLSocket ssl) {
            }

            @Override
            public void verify(String host, X509Certificate cert) {
            }

            @Override
            public void verify(String host, String[] cns, String[] subjectAlts) {
            }
        });
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }
    return sslsf;
}


相关文章
|
1天前
|
SQL 数据可视化 安全
微软SQL Server可视化工具与技巧
微软SQL Server不仅提供了强大的数据库管理功能,还集成了多种可视化工具,帮助用户更直观地理解和管理数据
|
7天前
|
SQL 数据可视化 关系型数据库
【数据库工具】DBeaver:一款免费的通用数据库工具和 SQL 客户端
【数据库工具】DBeaver:一款免费的通用数据库工具和 SQL 客户端
23 1
|
2月前
|
SQL 数据管理 关系型数据库
《SQL转换秘籍:Vanna+Qwen双剑合璧,轻松实现私有模型转换》——揭秘如何利用Vanna和Qwen这两款神级工具,让你的SQL数据管理和转换如虎添翼!
【8月更文挑战第17天】Vanna与Qwen是两款优秀的开源数据库管理工具,助力用户高效管理及转换SQL数据。先安装Vanna和Qwen,随后在Vanna中创建并编辑私有模型,定义表结构等。完成模型构建后,导出为SQL文件。接着,在Qwen中导入此文件,并根据目标数据库类型(如MySQL)转换SQL语句。例如,生成创建`users`表的SQL代码。这两款工具显著提升了数据库管理工作流程的便捷性与效率。
124 1
|
2月前
|
SQL 数据处理 数据库
|
2月前
|
SQL 存储 监控
|
2月前
|
Java 开发者 前端开发
Struts 2、Spring MVC、Play Framework 上演巅峰之战,Web 开发的未来何去何从?
【8月更文挑战第31天】在Web应用开发中,Struts 2框架因强大功能和灵活配置备受青睐,但开发者常遇配置错误、类型转换失败、标签属性设置不当及异常处理等问题。本文通过实例解析常见难题与解决方案,如配置文件中遗漏`result`元素致页面跳转失败、日期格式不匹配需自定义转换器、`&lt;s:checkbox&gt;`标签缺少`label`属性致显示不全及Action中未捕获异常影响用户体验等,助您有效应对挑战。
77 0
|
2月前
|
SQL 存储 数据处理
SQL中的运算符:数据操作的核心工具
【8月更文挑战第31天】
89 0
|
2月前
|
SQL 数据挖掘 关系型数据库
|
2月前
|
SQL 数据处理 数据库
SQL正则表达式应用:文本数据处理的强大工具——深入探讨数据验证、模式搜索、字符替换等核心功能及性能优化和兼容性问题
【8月更文挑战第31天】SQL正则表达式是数据库管理和应用开发中处理文本数据的强大工具,支持数据验证、模式搜索和字符替换等功能。本文通过问答形式介绍了其基本概念、使用方法及注意事项,帮助读者掌握这一重要技能,提升文本数据处理效率。尽管功能强大,但在不同数据库系统中可能存在兼容性问题,需谨慎使用以优化性能。
41 0