删除无用代码

This commit is contained in:
liuchengqian 2023-02-14 14:08:50 +08:00
parent e44f1a301f
commit 16c5cf4cdb
15 changed files with 7 additions and 704 deletions

View File

@ -4,7 +4,6 @@ import com.xkrs.common.encapsulation.OutputEncapsulation;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.straw.model.entity.SysUserEntity;
import com.xkrs.straw.service.SysUserService;
import com.xkrs.utils.IpUtil;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;

View File

@ -11,7 +11,6 @@ import java.util.List;
/**
* token工具
* @author tajochen
*/
public class TokenUtil {
/**
@ -19,9 +18,7 @@ public class TokenUtil {
*/
static public final String TOKEN_PREFIX = "Bearer";
static public final String SECRETKEY = "0Y9H364Q9Y908262F25LMXGIKIN5N858XM3674GWL2DD8X1DS4W6I722IRY8PS4XPNB6U30345" +
"HBVCUL94STG8C3Z53T7A09JJ100I56YE9894CI11PX9J71HIZ8L5Y2O504C4E2K8276425UA8734833F45K36878FXAG799QV9LXUJ" +
"OI3XA2046UPG8TB2OT84R5T6ZB127N9ZPJ7AJMC41JVHB2WK2B6H8NL45LZNAZ666KHZH3QUT65UX6F8";
static public final String SECRETKEY = "0Y9H364Q9Y908262F25LMXGIKIN5N858XM3674GWL2DD8X1DS4W6I722IRY8PS4XPNB6U30345" + "HBVCUL94STG8C3Z53T7A09JJ100I56YE9894CI11PX9J71HIZ8L5Y2O504C4E2K8276425UA8734833F45K36878FXAG799QV9LXUJ" + "OI3XA2046UPG8TB2OT84R5T6ZB127N9ZPJ7AJMC41JVHB2WK2B6H8NL45LZNAZ666KHZH3QUT65UX6F8";
static SecretKey key = Keys.hmacShaKeyFor(SECRETKEY.getBytes());
@ -30,11 +27,9 @@ public class TokenUtil {
if (token != null) {
try {
// 解析 Token
Claims claims = Jwts.parserBuilder()
.setSigningKey(key).build()
Claims claims = Jwts.parserBuilder().setSigningKey(key).build()
// 去掉 Bearer
.parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
.getBody();
.parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody();
// 获取用户名
userName = claims.getSubject();
// 获取权限

View File

@ -1,8 +1,5 @@
package com.xkrs.utils;
/**
* @author XinYi Song
*/
public interface CommonConstant {
/**

View File

@ -1,7 +1,6 @@
package com.xkrs.utils;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
@ -10,8 +9,6 @@ import java.util.regex.Pattern;
/**
* 日期时间工具
*
* @author tajochen
*/
public class DateTimeUtil {
@ -21,41 +18,8 @@ public class DateTimeUtil {
public final static DateTimeFormatter COMMON_FORMATTER_DATE = DateTimeFormatter.ofPattern(COMMON_PATTERN_DATE);
public final static ZoneOffset DEFAULT_ZONE_OFFSET = ZoneOffset.of("+8");
/**
* 字符串转LocalDate
*
* @param date
* @return
*/
public static LocalDate stringToDate(String date) {
LocalDate parse = null;
try {
parse = LocalDate.parse(date, COMMON_FORMATTER_DATE);
} catch (Exception e) {
e.printStackTrace();
}
if (parse == null) {
return LocalDate.parse(date, COMMON_FORMATTER_DATETIME);
}
return parse;
}
/**
* LocalDate转字符串
*
* @param date
* @return
*/
public static String dateToString(LocalDate date) {
assert date != null;
return COMMON_FORMATTER_DATE.format(date);
}
/**
* LocalDateTime转字符串
*
* @param dateTime
* @return
*/
public static String dateTimeToString(LocalDateTime dateTime) {
assert dateTime != null;
@ -64,9 +28,6 @@ public class DateTimeUtil {
/**
* 字符串转LocalDateTime
*
* @param date
* @return
*/
public static LocalDateTime stringToDateTime(String date) {
LocalDateTime parse = null;
@ -81,159 +42,40 @@ public class DateTimeUtil {
return parse;
}
/**
* 字符串转Instant时间戳
*
* @param str
* @return
*/
public static Instant stringToInstant(String str) {
assert str != null;
return stringToDateTime(str).toInstant(DEFAULT_ZONE_OFFSET);
}
/**
* LocalDateTime转字符串格式化
*
* @param dateTime
* @param formatter
* @return
*/
public static String dateToStringFormatter(LocalDateTime dateTime, DateTimeFormatter formatter) {
assert dateTime != null;
return formatter.format(dateTime);
}
/**
* 字符串转LocalDateTime格式化
*
* @param dateStr
* @param formatter
* @return
*/
public static LocalDateTime stringToDateTimeFormatter(String dateStr, DateTimeFormatter formatter) {
assert dateStr != null;
return LocalDateTime.parse(dateStr, formatter);
}
/**
* 字符串转 local date
*
* @param dateStr
* @return
*/
public static LocalDate stringToDateFormatter(String dateStr) {
LocalDate date = LocalDate.parse(dateStr, COMMON_FORMATTER_DATE);
return date;
}
/**
* 日期转时间戳
*
* @param dateTime
* @return
*/
public static long dateToTimeMillis(LocalDateTime dateTime) {
assert dateTime != null;
return dateTime.toInstant(DEFAULT_ZONE_OFFSET).toEpochMilli();
}
/**
* 时间戳转日期
*
* @param timeMillis
* @return
*/
public static LocalDateTime timeMillisToDate(long timeMillis) {
Instant instant = Instant.ofEpochMilli(timeMillis);
return LocalDateTime.ofInstant(instant, DEFAULT_ZONE_OFFSET);
}
/**
* 时间戳转时间
*
* @param timeMillis
* @return
*/
public static LocalDateTime timeMillisToTime(long timeMillis) {
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeMillis, 0, ZoneOffset.ofHours(8));
return localDateTime;
}
/**
* 时间戳转时间再转字符串
*
* @param timeMillis
* @return
*/
public static String timeMillisToString(long timeMillis) {
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeMillis, 0, ZoneOffset.ofHours(8));
return COMMON_FORMATTER_DATETIME.format(localDateTime);
}
/**
* 获取当前时间 hh:mm:ss:nnn
*
* @return
*/
public static LocalDateTime getNowTime() {
LocalDateTime now = LocalDateTime.now();
return now;
}
/**
* 获取当前日期 yyyy-MM-dd
*
* @return
*/
public static LocalDate getToday() {
LocalDate now = LocalDate.now();
return now;
}
/**
* 获取当前 Instant 时间戳
*
* @return
*/
public static Instant getInstant() {
Instant timeStamp = Instant.now();
return timeStamp;
}
/**
* 获取当前时间 yyyy-MM-ss hh:mm:ss
*
* @return
*/
public static String getNowTimeStr() {
return COMMON_FORMATTER_DATETIME.format(getNowTime());
}
/**
* 判断日期格式是否合法 "yyyy-MM-dd"
*
* @param strDate
* @return
*/
public static boolean isValidDate(String strDate) {
final int minLen = 10;
if (strDate == null || strDate.length() < minLen) {
return false;
}
//正则表达式校验日期格式 yyyy-MM-dd
String eL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-" + "(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})" + "(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)(([0-9]{3}[1-9]|[0-9]{2}[1-9]" + "[0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-" + "(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|" + "((0[48]|[2468][048]|[3579][26])00))-02-29)";
Pattern pat = Pattern.compile(eL);
Matcher matcher = pat.matcher(strDate);
return matcher.matches();
return COMMON_FORMATTER_DATETIME.format(LocalDateTime.now());
}
/**
* 判断时间格式 格式必须为 "YYYY-MM-DD HH:mm:ss"
*
* @param sDateTime
* @return
*/
public static boolean isValidDateTime(String sDateTime) {
final int minLen = 19;
@ -246,12 +88,4 @@ public class DateTimeUtil {
return matcher.matches();
}
public static void main(String[] args) {
LocalDate now = LocalDate.now();
LocalDate localDate = now.plusDays(1);
String s = dateToString(localDate);
LocalDate localDate1 = stringToDate(s);
System.out.println(localDate1);
}
}

View File

@ -80,40 +80,6 @@ public class FileUtil {
return list;
}
/**
* 移动文件到目标目录
* @param path 文件路径
* @return
*/
/*public static FileEntity mvFile(String path){
String savePath = "D:" + File.separator + "dms" + File.separator + "data" ;
File file = new File(path);
if (!file.isFile()) {
log.info("{}不是文件。",path);
return null;
}
// 从文件名获取到入库信息 暂时不知道
String[] infos = path.split("/");
String fileNameUnHandler = infos[infos.length - 1];
String[] fileNameUnHandlerSplitWithPoint = fileNameUnHandler.split(".");
String fileName = fileNameUnHandlerSplitWithPoint[0];
String scene = fileNameUnHandlerSplitWithPoint[1];
String[] fileNames = fileNameUnHandler.split("_");
long length = file.length();
String filePath = File.separator + scene + File.separator + fileName;
// savePath = savePath + File.separator + dir + File.separator + fileName;
if (isFileExit(savePath + File.separator + scene)) {
if (file.renameTo(new File(filePath))) {
log.info("移动文件{}到{}目录",file.getName(), savePath + filePath);
} else {
log.info("文件{}移动失败",file.getName());
return null;
}
}
return null;
}*/
/**
* 读取txt文件内容
* @param file

View File

@ -55,7 +55,7 @@ public class FirePointCodeUtils {
continue;
}
LocalDateTime sysUserOverDateTime = DateTimeUtil.stringToDateTimeFormatter(sysUser.getOverTime(), DateTimeUtil.COMMON_FORMATTER_DATETIME);
if (sysUserOverDateTime.isBefore(DateTimeUtil.getNowTime())) {
if (sysUserOverDateTime.isBefore(LocalDateTime.now())) {
if (!normalSmsEnable) {//根据配置信息决定普通用户是否发送短信
continue;
}

View File

@ -55,13 +55,10 @@ public class GaoDeApiUtil {
}
public static void main(String[] args) {
List<String> location = new ArrayList<>();
location.add("118.06454,37.8562");
GaoDeIgGeocodeVo geocode = geocode(location);
System.out.println("sdfghjm");
}
}

View File

@ -55,8 +55,6 @@ public class HttpClientUtils {
* @throws Exception
*/
public static String sendHttpsGet(String url) throws Exception {
// CloseableHttpClient httpClient = createIgnoreVerifyHttpClient();
//CloseableHttpClient httpClient = HttpClients.createDefault();
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(scsf).build();
return doGet(url, httpClient);
@ -101,8 +99,6 @@ public class HttpClientUtils {
* @throws Exception
*/
public static String sendHttpsPost(String url, String params) throws Exception {
// CloseableHttpClient httpClient = createIgnoreVerifyHttpClient();
// CloseableHttpClient httpClient = null;
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(scsf).build();
// CloseableHttpClient httpClient = HttpClients.createDefault();

View File

@ -1,43 +0,0 @@
package com.xkrs.utils;
import javax.servlet.http.HttpServletRequest;
/**
* IP地址处理工具
*
* @author tajochen
*/
public class IpUtil {
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
final String unkonwMsg = "unknown";
final char splitChar = ',';
if (ip != null && ip.length() != 0 && !unkonwMsg.equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个ip值第一个ip才是真实ip
if (ip.indexOf(splitChar) != -1) {
ip = ip.split(",")[0];
}
}
if (ip == null || ip.length() == 0 || unkonwMsg.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || unkonwMsg.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || unkonwMsg.equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || unkonwMsg.equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || unkonwMsg.equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || unkonwMsg.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
System.out.println("获取客户端ip: " + ip);
return ip;
}
}

View File

@ -4,8 +4,6 @@ import java.util.Random;
/**
* 随机字符串产生工具
*
* @author tajochen
*/
public class RandomUtil {

View File

@ -1,260 +0,0 @@
package com.xkrs.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xkrs.model.vo.FileServerResultVo;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* 上传工具
* @author tajochen
**/
public class RequestUtil {
/**
* 发送管理命令到文件服务器
* @param url
* @param map
* @return
*/
public static FileServerResultVo postFileManage(String url, Map<String,String> map){
Logger log = LoggerFactory.getLogger(RequestUtil.class);
String body = "";
// 创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 装填参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if(map!=null){
for (Map.Entry<String, String> entry : map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
try {
// 设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
// 设置header报文头信息
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0");
httpPost.setHeader("Accept", "application");
httpPost.setHeader("Accept-Encoding", "gzip, deflate");
// 执行请求操作并拿到结果同步阻塞
CloseableHttpResponse response = client.execute(httpPost);
// 获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
// 释放链接
response.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileServerResultVo fileServerResultVo = new FileServerResultVo();
ObjectMapper mapper = new ObjectMapper();
try {
fileServerResultVo = mapper.readValue(body, FileServerResultVo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
log.warn(e.toString());
}
return fileServerResultVo;
}
/**
* 模拟 get请求
* @param url 链接
* @param map 参数列表
*/
public static void getStandard(String url, Map<String,String> map) {
// 1.拿到一个httpclient的对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2.设置请求方式和请求信息
HttpPost httpPost = new HttpPost(url);
//2.1 提交header头信息
httpPost.addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
//2.1 提交请求体
ArrayList<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
// 装填参数
if(map!=null){
for (Map.Entry<String, String> entry : map.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 3.执行请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
// 4.获取返回值
String html = null;
try {
assert response != null;
html = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
// 5.打印
System.out.println(html);
}
/**
* 模拟Post请求 application/x-www-form-urlencoded
* @param url 资源地址
* @param map 参数列表
* @return
*/
public static String postStandard(String url, Map<String,String> map) {
String body = "";
// 创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 装填参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if(map!=null){
for (Map.Entry<String, String> entry : map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
try {
// 设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
// 设置header报文头信息
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0");
httpPost.setHeader("Accept", "application");
httpPost.setHeader("Accept-Encoding", "gzip, deflate");
// 执行请求操作并拿到结果同步阻塞
CloseableHttpResponse response = client.execute(httpPost);
// 获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
// 释放链接
response.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}
/**
* 模拟Post请求 multipart/form-data
* @param postFile
* @param postUrl
* @param postParam
* @return
*/
public static Map<String,Object> postFile(File postFile,String postUrl,Map<String,String> postParam) {
Logger log = LoggerFactory.getLogger(RequestUtil.class);
Map<String,Object> resultMap = new HashMap<String,Object>(16);
CloseableHttpClient httpClient = HttpClients.createDefault();
try{
// 把一个普通参数和文件上传给下面这个地址
HttpPost httpPost = new HttpPost(postUrl);
// 设置传输参数
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
// 解决乱码问题
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.setCharset(StandardCharsets.UTF_8);
// 设置文件参数 获取文件名postFile.getName()
// 把文件转换成流对象FileBody
multipartEntity.addBinaryBody("file",postFile, ContentType.create("multipart/form-data", Consts.UTF_8),postFile.getName());
// 设计文件以外的参数
Set<String> keySet = postParam.keySet();
for (String key : keySet) {
// 相当于<input type="text" name="name" value=name>
multipartEntity.addPart(key, new StringBody(postParam.get(key), ContentType.create("multipart/form-data", Consts.UTF_8)));
}
HttpEntity reqEntity = multipartEntity.build();
httpPost.setEntity(reqEntity);
// 设置header报文头信息
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0");
httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPost.setHeader("Accept-Encoding","gzip, deflate");
// 发起请求返回请求的响应
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
resultMap.put("statusCode", response.getStatusLine().getStatusCode());
// 获取响应对象
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
// 打印响应内容
resultMap.put("data", EntityUtils.toString(resEntity, StandardCharsets.UTF_8));
resultMap.put("message", EntityUtils.toString(resEntity, StandardCharsets.UTF_8));
resultMap.put("status", EntityUtils.toString(resEntity, StandardCharsets.UTF_8));
}
// 销毁
EntityUtils.consume(resEntity);
} catch (Exception e) {
e.printStackTrace();
} finally {
response.close();
}
} catch (IOException e1) {
e1.printStackTrace();
} finally{
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
log.info("上传结果:" + resultMap);
return resultMap;
}
}

View File

@ -1,94 +0,0 @@
package com.xkrs.utils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @author xkrs
*/
@Service
public class RestTemplateUtil {
@Resource
private RestTemplate restTemplate;
/**
* 以get方式请求第三方http接口 getForEntity
* @param url
* @return
*/
public JSONObject doGetForEntity(String url) {
ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(url, JSONObject.class);
return responseEntity.getBody();
}
/**
* 以get方式请求第三方http接口 getForObject
* 返回值返回的是响应体
* @param url
* @return
*/
public JSONObject doGetForObject(String url) {
JSONObject result = restTemplate.getForObject(url, JSONObject.class);
return result;
}
/**
* 以post方式请求第三方http接口 postForEntity
* @param url
* @return
*/
public JSONObject doPostForEntity(String url) {
//可设置请求参数
JSONObject param = new JSONObject();
param.put("userName","admin");
param.put("ipAddress","");
param.put("clientType","WINPC");
ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, param, JSONObject.class);
return responseEntity.getBody();
}
/**
* 以post方式请求第三方http接口 postForObject
* @param url
* @return
*/
public JSONObject doPostForObject(String url,JSONObject param) {
JSONObject result = restTemplate.postForObject(url, param, JSONObject.class);
return result;
}
/**
* exchange方法请求第三方http接口
*
*/
public JSONObject doExchange(String url, HttpHeaders httpHeaders, JSONObject param) {
//创建请求对象
HttpEntity<JSONObject> request = new HttpEntity<>(param,httpHeaders);
//执行请求(请求路径请求方式请求体响应体)
ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, JSONObject.class);
return responseEntity.getBody();
}
/**
* 发送第三方小米推送请求向app端推送消息
* @param url
* @param httpHeaders
* @param param
*/
public void doPostMessageToApp(String url,HttpHeaders httpHeaders,JSONObject param) {
//创建请求对象
HttpEntity<JSONObject> request = new HttpEntity<>(param,httpHeaders);
//执行请求(请求路径请求方式请求体响应体)
restTemplate.exchange(url, HttpMethod.POST, request, JSONObject.class);
}
}

View File

@ -4,30 +4,13 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* http工具类
*/
@ -47,21 +30,6 @@ public class WDHttpClientUtils {
return doGet(url, httpClient);
}
/**
* 发送https+get请求绕过证书
*
* @param url 请求地址
* @return 返回结果
* @throws Exception
*/
public static String sendHttpsGet(String url) throws Exception {
// CloseableHttpClient httpClient = createIgnoreVerifyHttpClient();
//CloseableHttpClient httpClient = HttpClients.createDefault();
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(scsf).build();
return doGet(url, httpClient);
}
/**
* 发送http+post请求
*
@ -87,24 +55,6 @@ public class WDHttpClientUtils {
return doPost(httpClient, url);
}
/**
* 发送https+post请求
*
* @param url 请求地址
* @param params 请求参数 json字符串
* @return 返回结果
* @throws Exception
*/
public static String sendHttpsPost(String url, String params) throws Exception {
// CloseableHttpClient httpClient = createIgnoreVerifyHttpClient();
// CloseableHttpClient httpClient = null;
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(scsf).build();
// CloseableHttpClient httpClient = HttpClients.createDefault();
return doPost(httpClient, url, params);
}
/**
* 封装get请求方式的处理
*/
@ -166,37 +116,6 @@ public class WDHttpClientUtils {
return result;
}
/**
* 绕过验证
*
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static CloseableHttpClient createIgnoreVerifyHttpClient() throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLS");
// 实现一个X509TrustManager接口
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[]{trustManager}, null);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext)).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).build();
return httpClient;
}
}

View File

@ -134,7 +134,6 @@ public class XlsxUtil {
}
}
/**
* 下载文件 单个
*
@ -149,4 +148,5 @@ public class XlsxUtil {
e.printStackTrace();
}
}
}

View File

@ -26,7 +26,6 @@ public class WeatherDataServiceImpl implements WeatherDataService {
return this.getWeatherResponse(uri);
}
@Override
public WeatherResponse getDataByCityName(String cityName) {
String uri = WEATHER_URI + "city=" + cityName;