fix: 微信公众号微服务添加及修改。
This commit is contained in:
@@ -133,6 +133,18 @@
|
||||
<artifactId>core</artifactId>
|
||||
<version>3.4.1</version> <!-- 请使用最新版本 -->
|
||||
</dependency>
|
||||
<!--hutool-->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.1.0</version> <!-- 请使用最新版本 -->
|
||||
</dependency>
|
||||
<!--mybatis plus extension,包含了mybatis plus core-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-extension</artifactId>
|
||||
<version>3.5.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.ruoyi.common.core.sensitive;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 脱敏注解
|
||||
*
|
||||
* @author JooLun
|
||||
* @Author: https://www.cnblogs.com/xiluonanfeng/p/10183926.html
|
||||
**/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@JacksonAnnotationsInside
|
||||
@JsonSerialize(using = SensitiveSerialize.class)
|
||||
public @interface Sensitive {
|
||||
|
||||
/**
|
||||
* 脱敏数据类型
|
||||
*/
|
||||
SensitiveTypeEnum type();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.ruoyi.common.core.sensitive;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.BeanProperty;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
||||
import com.ruoyi.common.core.utils.SensitiveUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author JooLun
|
||||
* @Author: https://www.cnblogs.com/xiluonanfeng/p/10183926.html
|
||||
* 脱敏序列化
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SensitiveSerialize extends JsonSerializer<String> implements ContextualSerializer {
|
||||
|
||||
private SensitiveTypeEnum type;
|
||||
|
||||
@Override
|
||||
public void serialize(final String originStr, final JsonGenerator jsonGenerator,
|
||||
final SerializerProvider serializerProvider) throws IOException {
|
||||
switch (type) {
|
||||
case CHINESE_NAME:
|
||||
jsonGenerator.writeString(SensitiveUtils.chineseName(originStr));
|
||||
break;
|
||||
case MOBILE_PHONE:
|
||||
jsonGenerator.writeString(SensitiveUtils.mobilePhone(originStr));
|
||||
break;
|
||||
case EMAIL:
|
||||
jsonGenerator.writeString(SensitiveUtils.email(originStr));
|
||||
break;
|
||||
case PASSWORD:
|
||||
jsonGenerator.writeString(SensitiveUtils.password(originStr));
|
||||
break;
|
||||
case KEY:
|
||||
jsonGenerator.writeString(SensitiveUtils.key(originStr));
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("未定义的敏感信息枚举类" + type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonSerializer<?> createContextual(final SerializerProvider serializerProvider, final BeanProperty beanProperty) throws JsonMappingException {
|
||||
if (beanProperty != null) {
|
||||
if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
|
||||
Sensitive sensitive = beanProperty.getAnnotation(Sensitive.class);
|
||||
if (sensitive == null) {
|
||||
sensitive = beanProperty.getContextAnnotation(Sensitive.class);
|
||||
}
|
||||
if (sensitive != null) {
|
||||
return new SensitiveSerialize(sensitive.type());
|
||||
}
|
||||
}
|
||||
return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
|
||||
}
|
||||
return serializerProvider.findNullValueSerializer(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.ruoyi.common.core.sensitive;
|
||||
|
||||
/**
|
||||
* 敏感信息枚举类
|
||||
*
|
||||
* @author JooLun
|
||||
* @Author: https://www.cnblogs.com/xiluonanfeng/p/10183926.html
|
||||
**/
|
||||
public enum SensitiveTypeEnum {
|
||||
|
||||
/**
|
||||
* 用户名, 李*天, 张*
|
||||
*/
|
||||
CHINESE_NAME,
|
||||
/**
|
||||
* 手机号, 185****1653
|
||||
*/
|
||||
MOBILE_PHONE,
|
||||
/**
|
||||
* 电子邮件, r*****o@qq.com
|
||||
*/
|
||||
EMAIL,
|
||||
/**
|
||||
* 密码, ******
|
||||
*/
|
||||
PASSWORD,
|
||||
/**
|
||||
* 密钥, 最后三位其他都是***
|
||||
*/
|
||||
KEY
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.ruoyi.common.core.typehandler;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* 存储到数据库, 将LONG数组转换成字符串;
|
||||
* 从数据库获取数据, 将字符串转为LONG数组.
|
||||
*/
|
||||
@MappedTypes({Long[].class})
|
||||
@MappedJdbcTypes({JdbcType.VARCHAR})
|
||||
public class ArrayLongTypeHandler extends BaseTypeHandler<Long[]> {
|
||||
|
||||
private static Long[] l = new Long[]{};
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i,
|
||||
Long[] parameter, JdbcType jdbcType) throws SQLException {
|
||||
ps.setString(i, JSONUtil.toJsonStr(parameter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long[] getNullableResult(ResultSet rs, String columnName)
|
||||
throws SQLException {
|
||||
return JSONUtil.parseArray(rs.getString(columnName)).toArray(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long[] getNullableResult(ResultSet rs, int columnIndex)
|
||||
throws SQLException {
|
||||
return JSONUtil.parseArray(rs.getString(columnIndex)).toArray(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long[] getNullableResult(CallableStatement cs, int columnIndex)
|
||||
throws SQLException {
|
||||
return JSONUtil.parseArray(cs.getString(columnIndex)).toArray(l);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.ruoyi.common.core.typehandler;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* 存储到数据库, 将String数组转换成字符串;
|
||||
* 从数据库获取数据, 将字符串转为LONG数组.
|
||||
*/
|
||||
@MappedTypes({String[].class})
|
||||
@MappedJdbcTypes({JdbcType.VARCHAR})
|
||||
public class ArrayStringTypeHandler extends BaseTypeHandler<String[]> {
|
||||
|
||||
private static String[] l = new String[]{};
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i,
|
||||
String[] parameter, JdbcType jdbcType) throws SQLException {
|
||||
ps.setString(i, JSONUtil.toJsonStr(parameter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getNullableResult(ResultSet rs, String columnName)
|
||||
throws SQLException {
|
||||
return JSONUtil.parseArray(rs.getString(columnName)).toArray(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getNullableResult(ResultSet rs, int columnIndex)
|
||||
throws SQLException {
|
||||
return JSONUtil.parseArray(rs.getString(columnIndex)).toArray(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getNullableResult(CallableStatement cs, int columnIndex)
|
||||
throws SQLException {
|
||||
return JSONUtil.parseArray(cs.getString(columnIndex)).toArray(l);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.ruoyi.common.core.typehandler;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* 存储到数据库, 将JSON对象转换成字符串;
|
||||
* 从数据库获取数据, 将字符串转为JSON对象.
|
||||
*/
|
||||
@MappedTypes({JSONObject.class})
|
||||
@MappedJdbcTypes({JdbcType.VARCHAR})
|
||||
public class JsonTypeHandler extends BaseTypeHandler<JSONObject> {
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter,
|
||||
JdbcType jdbcType) throws SQLException {
|
||||
|
||||
ps.setString(i, JSONUtil.toJsonStr(parameter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getNullableResult(ResultSet rs, String columnName)
|
||||
throws SQLException {
|
||||
|
||||
return JSONUtil.parseObj(rs.getString(columnName)).toBean(JSONObject.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
|
||||
return JSONUtil.parseObj(rs.getString(columnIndex)).toBean(JSONObject.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getNullableResult(CallableStatement cs, int columnIndex)
|
||||
throws SQLException {
|
||||
|
||||
return JSONUtil.parseObj(cs.getString(columnIndex)).toBean(JSONObject.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.ruoyi.common.core.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 数据脱敏工具类
|
||||
*
|
||||
* @Author: JooLun
|
||||
* @Author: https://www.cnblogs.com/xiluonanfeng/p/10183926.html
|
||||
* @Date: 2021/7/19 16:21
|
||||
*/
|
||||
public class SensitiveUtils {
|
||||
|
||||
/**
|
||||
* 默认填充字符
|
||||
*/
|
||||
public static final String DEFAULT_PAD_STR = "*";
|
||||
|
||||
/**
|
||||
* 数据脱敏
|
||||
*
|
||||
*/
|
||||
public static String process(String data) {
|
||||
return process(data, 2, 1, DEFAULT_PAD_STR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据脱敏
|
||||
*
|
||||
*/
|
||||
public static String process(String data, Integer leftLen, Integer rightLen) {
|
||||
return process(data, leftLen, rightLen, DEFAULT_PAD_STR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对字符串进行脱敏操作
|
||||
* @param originStr 原始字符串
|
||||
* @param prefixNoMaskLen 左侧需要保留几位明文字段
|
||||
* @param suffixNoMaskLen 右侧需要保留几位明文字段
|
||||
* @param maskStr 用于遮罩的字符串, 如'*'
|
||||
* @return 脱敏后结果
|
||||
*/
|
||||
public static String process(String originStr, int prefixNoMaskLen, int suffixNoMaskLen, String maskStr) {
|
||||
if (originStr == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0, n = originStr.length(); i < n; i++) {
|
||||
if (i < prefixNoMaskLen) {
|
||||
sb.append(originStr.charAt(i));
|
||||
continue;
|
||||
}
|
||||
if (i > (n - suffixNoMaskLen - 1)) {
|
||||
sb.append(originStr.charAt(i));
|
||||
continue;
|
||||
}
|
||||
sb.append(maskStr);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 中文姓名只显示最后一个汉字
|
||||
* @param fullName 姓名
|
||||
* @return
|
||||
*/
|
||||
public static String chineseName(String fullName) {
|
||||
if (fullName == null) {
|
||||
return null;
|
||||
}
|
||||
return process(fullName, 0, 1, DEFAULT_PAD_STR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号码前三位,后四位,如186****2356
|
||||
* @param num 手机号码
|
||||
* @return
|
||||
*/
|
||||
public static String mobilePhone(String num) {
|
||||
return process(num, 0, 4, DEFAULT_PAD_STR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 地址只显示到地区
|
||||
* @param address 地址
|
||||
* @return
|
||||
*/
|
||||
public static String address(String address) {
|
||||
return process(address, 6, 0, DEFAULT_PAD_STR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 电子邮箱 仅显示第一个字母,@后面的地址显示,比如:r**@qq.com
|
||||
* @param email 电子邮箱
|
||||
* @return
|
||||
*/
|
||||
public static String email(String email) {
|
||||
if (email == null) {
|
||||
return null;
|
||||
}
|
||||
int index = StrUtil.indexOf(email, '@');
|
||||
if (index <= 1) {
|
||||
return email;
|
||||
}
|
||||
String preEmail = process(email.substring(0, index), 1, 0, DEFAULT_PAD_STR);
|
||||
return preEmail + email.substring(index);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码的全部字符,如:******
|
||||
* @param password 密码
|
||||
* @return
|
||||
*/
|
||||
public static String password(String password) {
|
||||
if (password == null) {
|
||||
return null;
|
||||
}
|
||||
return "******";
|
||||
}
|
||||
|
||||
/**
|
||||
* 密钥除了最后三位,全部,比如:***klo
|
||||
* @param key 密钥
|
||||
* @return 结果
|
||||
*/
|
||||
public static String key(String key) {
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
int viewLength = 6;
|
||||
StringBuilder tmpKey = new StringBuilder(process(key, 0, 3, DEFAULT_PAD_STR));
|
||||
if (tmpKey.length() > viewLength) {
|
||||
return tmpKey.substring(tmpKey.length() - viewLength);
|
||||
}
|
||||
else if (tmpKey.length() < viewLength) {
|
||||
int buffLength = viewLength - tmpKey.length();
|
||||
for (int i = 0; i < buffLength; i++) {
|
||||
tmpKey.insert(0, DEFAULT_PAD_STR);
|
||||
}
|
||||
return tmpKey.toString();
|
||||
}
|
||||
else {
|
||||
return tmpKey.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String s = mobilePhone("18653653621");
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.ruoyi.common.core.utils.http;
|
||||
|
||||
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 通用http工具封装
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class HttpHelper
|
||||
{
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HttpHelper.class);
|
||||
|
||||
public static String getBodyString(ServletRequest request)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader reader = null;
|
||||
try (InputStream inputStream = request.getInputStream())
|
||||
{
|
||||
reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
|
||||
String line = "";
|
||||
while ((line = reader.readLine()) != null)
|
||||
{
|
||||
sb.append(line);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LOGGER.warn("getBodyString出现问题!");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (reader != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
reader.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LOGGER.error(ExceptionUtils.getMessage(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
package com.ruoyi.common.core.utils.http;
|
||||
|
||||
|
||||
import com.ruoyi.common.core.constant.Constants;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.net.ssl.*;
|
||||
import java.io.*;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
/**
|
||||
* 通用http发送方法
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class HttpUtils
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
||||
|
||||
/**
|
||||
* 向指定 URL 发送GET方法的请求
|
||||
*
|
||||
* @param url 发送请求的 URL
|
||||
* @return 所代表远程资源的响应结果
|
||||
*/
|
||||
public static String sendGet(String url)
|
||||
{
|
||||
return sendGet(url, StringUtils.EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定 URL 发送GET方法的请求
|
||||
*
|
||||
* @param url 发送请求的 URL
|
||||
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||
* @return 所代表远程资源的响应结果
|
||||
*/
|
||||
public static String sendGet(String url, String param)
|
||||
{
|
||||
return sendGet(url, param, Constants.UTF8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定 URL 发送GET方法的请求
|
||||
*
|
||||
* @param url 发送请求的 URL
|
||||
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||
* @param contentType 编码类型
|
||||
* @return 所代表远程资源的响应结果
|
||||
*/
|
||||
public static String sendGet(String url, String param, String contentType)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
BufferedReader in = null;
|
||||
try
|
||||
{
|
||||
String urlNameString = StringUtils.isNotBlank(param) ? url + "?" + param : url;
|
||||
log.info("sendGet - {}", urlNameString);
|
||||
URL realUrl = new URL(urlNameString);
|
||||
URLConnection connection = realUrl.openConnection();
|
||||
connection.setRequestProperty("accept", "*/*");
|
||||
connection.setRequestProperty("connection", "Keep-Alive");
|
||||
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
|
||||
connection.connect();
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null)
|
||||
{
|
||||
result.append(line);
|
||||
}
|
||||
log.info("recv - {}", result);
|
||||
}
|
||||
catch (ConnectException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (SocketTimeoutException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定 URL 发送POST方法的请求
|
||||
*
|
||||
* @param url 发送请求的 URL
|
||||
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||
* @return 所代表远程资源的响应结果
|
||||
*/
|
||||
public static String sendPost(String url, String param)
|
||||
{
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
StringBuilder result = new StringBuilder();
|
||||
try
|
||||
{
|
||||
log.info("sendPost - {}", url);
|
||||
URL realUrl = new URL(url);
|
||||
URLConnection conn = realUrl.openConnection();
|
||||
conn.setRequestProperty("accept", "*/*");
|
||||
conn.setRequestProperty("connection", "Keep-Alive");
|
||||
conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
|
||||
conn.setRequestProperty("Accept-Charset", "utf-8");
|
||||
conn.setRequestProperty("contentType", "utf-8");
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
out = new PrintWriter(conn.getOutputStream());
|
||||
out.print(param);
|
||||
out.flush();
|
||||
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null)
|
||||
{
|
||||
result.append(line);
|
||||
}
|
||||
log.info("recv - {}", result);
|
||||
}
|
||||
catch (ConnectException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (SocketTimeoutException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (out != null)
|
||||
{
|
||||
out.close();
|
||||
}
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static String sendSSLPost(String url, String param)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
String urlNameString = url + "?" + param;
|
||||
try
|
||||
{
|
||||
log.info("sendSSLPost - {}", urlNameString);
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
|
||||
URL console = new URL(urlNameString);
|
||||
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
|
||||
conn.setRequestProperty("accept", "*/*");
|
||||
conn.setRequestProperty("connection", "Keep-Alive");
|
||||
conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
|
||||
conn.setRequestProperty("Accept-Charset", "utf-8");
|
||||
conn.setRequestProperty("contentType", "utf-8");
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
|
||||
conn.setSSLSocketFactory(sc.getSocketFactory());
|
||||
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
|
||||
conn.connect();
|
||||
InputStream is = conn.getInputStream();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
||||
String ret = "";
|
||||
while ((ret = br.readLine()) != null)
|
||||
{
|
||||
if (ret != null && !"".equals(ret.trim()))
|
||||
{
|
||||
result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
log.info("recv - {}", result);
|
||||
conn.disconnect();
|
||||
br.close();
|
||||
}
|
||||
catch (ConnectException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (SocketTimeoutException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static class TrustAnyTrustManager implements X509TrustManager
|
||||
{
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers()
|
||||
{
|
||||
return new X509Certificate[] {};
|
||||
}
|
||||
}
|
||||
|
||||
private static class TrustAnyHostnameVerifier implements HostnameVerifier
|
||||
{
|
||||
@Override
|
||||
public boolean verify(String hostname, SSLSession session)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user