初始化
This commit is contained in:
@@ -0,0 +1,299 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yexb(创建)-----denghuajun(移植使用)
|
||||
* @version 1.0
|
||||
* @date 2018/8/23 16:02
|
||||
*/
|
||||
//因素集U={频率偏差、电网谐波、电压波动与闪变、电压偏差、电压暂降、三相不平衡}
|
||||
//评判级分为5个等级,即:Q = {很差,较差,合格,良好,优质}
|
||||
/* 电压偏差 电网谐波 三相不平衡 频率偏差 电压波动 电压暂降
|
||||
(偏差绝对值/%) (电压总谐波畸变率%) (不平衡度/%) (偏差绝对值/Hz) (短时闪变值) (暂降幅度%)
|
||||
第1 级 10~ 6..0~ 4.0~ 0.3~ 0.8~ 90~
|
||||
第2 级 7~10 4.0~6.0 2..0~4.0 0.2~0.3 0.6~0.8 40~90
|
||||
第3 级 4~7 2.0~4.0 1.0~2.0 0.1~0.2 0.4~0.6 20~40
|
||||
第4 级 2~4 1.0~2.0 0.5~1.0 0.05~0.1 0.2~0.4 10~20
|
||||
第5 级 0~2 0~1.0 0~0.5 0~0.05 0~0.2 0~10
|
||||
*/
|
||||
@Component
|
||||
public class ComAssesUtil {
|
||||
// 日志记录
|
||||
private static final Logger logger = LoggerFactory.getLogger(ComAssesUtil.class);
|
||||
private static final int ST_QT_NUM = 6;//系统评价指标数目
|
||||
private static final int GRADE_NUM = 5;//指标分级数目
|
||||
private static final int METHOD_NUM = 5;//评估方法数
|
||||
private static final int METHOD_IDX1 = 0;//层次分析法
|
||||
private static final int METHOD_IDX2 = 1;//优序图法
|
||||
private static final int METHOD_IDX3 = 2;//专家打分法
|
||||
private static final int METHOD_IDX4 = 3;//熵权法
|
||||
private static final int METHOD_IDX5 = 4;//变异系数法
|
||||
|
||||
private static final int IDX_FREQ = 0;//频率
|
||||
private static final int IDX_UTHD = 1;//电压畸变率
|
||||
private static final int IDX_FLICK = 2;//电压闪变
|
||||
private static final int IDX_UDEV = 3;//电压偏差
|
||||
private static final int IDX_EVT = 4;//电压暂降
|
||||
private static final int IDX_UBPH = 5;//电压不平衡
|
||||
|
||||
private static final int MAX_DATA_TYPE = 5;//5种统计数据
|
||||
private static final int MAX_DATA_NUM = 1440 * 31;//最大统计数据个数
|
||||
private static final int MAX_EVT_NUM = 1000;//最大暂态事件个数
|
||||
|
||||
//3种主观赋权法直接摘录文档中计算好的最终评估权重
|
||||
//层次分析法
|
||||
private float W1[] = {0.38f, 0.22f, 0.13f, 0.12f, 0.08f, 0.07f};
|
||||
//优序图法
|
||||
private float W2[] = {0.28f, 0.24f, 0.19f, 0.14f, 0.10f, 0.05f};
|
||||
//专家打分法
|
||||
private float W3[] = {0.39f, 0.26f, 0.12f, 0.09f, 0.07f, 0.07f};
|
||||
|
||||
//数据评估矩阵
|
||||
private float Assess[][];
|
||||
//权重矩阵
|
||||
private float Weight[][];
|
||||
|
||||
float A[];
|
||||
|
||||
// 综合评估程序,返回值为评估分
|
||||
public float GetComAsses(float in_data[][]) {
|
||||
float fResult = 0.0f;//返回最终评分
|
||||
try{
|
||||
//实例化所有参数
|
||||
Assess = new float[ST_QT_NUM][GRADE_NUM];
|
||||
Weight = new float[ST_QT_NUM][METHOD_NUM];
|
||||
A = new float[ST_QT_NUM];
|
||||
float B[] = new float[GRADE_NUM];
|
||||
|
||||
int i, j;
|
||||
float sum1, sum2;
|
||||
Assess = in_data;//给评估矩阵赋值,此值直接从相应的数据库中获取
|
||||
|
||||
//W1-W3为主观赋权,直接从文档上摘录赋权
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
Weight[i][METHOD_IDX1] = W1[i];
|
||||
Weight[i][METHOD_IDX2] = W2[i];
|
||||
Weight[i][METHOD_IDX3] = W3[i];
|
||||
}
|
||||
//熵权法求W4
|
||||
if (getSqf()) {
|
||||
//变异系数法求W5
|
||||
if (getBysxf()) {
|
||||
//G和F得出综合权重A
|
||||
if (getZhqzf()) {
|
||||
//A[0] = 0.28;A[1] = 0.23;A[2] = 0.13;A[3] = 0.16;A[4] = 0.08;A[5] = 0.12;
|
||||
for (i = 0; i < GRADE_NUM; i++) {
|
||||
B[i] = 0;
|
||||
for (j = 0; j < ST_QT_NUM; j++) {
|
||||
B[i] += A[j] * Assess[j][i];
|
||||
}
|
||||
}
|
||||
sum1 = 0;
|
||||
sum2 = 0;
|
||||
for (i = 0; i < GRADE_NUM; i++) {
|
||||
sum1 += (i + 1) * B[i];
|
||||
sum2 += B[i];
|
||||
}
|
||||
fResult = sum1 / sum2;
|
||||
}
|
||||
}
|
||||
}
|
||||
fResult = FloatUtils.get2Float(fResult);
|
||||
}catch (Exception e){
|
||||
//Todo
|
||||
}
|
||||
return fResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大批量的监测点的综合得分获取平均值
|
||||
* @param pqsComasses 批量数据
|
||||
*/
|
||||
public float getAllComAss(List<PqsComasses> pqsComasses) {
|
||||
float allData=0f;
|
||||
for(int i=0;i<pqsComasses.size();i++){
|
||||
PqsComasses tempPqs=pqsComasses.get(i);
|
||||
//组合二维数组
|
||||
float f1[][]={{tempPqs.getFreqDev1(),tempPqs.getFreqDev2(),tempPqs.getFreqDev3(),tempPqs.getFreqDev4(),tempPqs.getFreqDev5()}
|
||||
,{tempPqs.getVThd1(),tempPqs.getVThd2(),tempPqs.getVThd3(),tempPqs.getVThd4(),tempPqs.getVThd5(),}
|
||||
,{tempPqs.getDataPst1(),tempPqs.getDataPst2(),tempPqs.getDataPst3(),tempPqs.getDataPst4(),tempPqs.getDataPst5()}
|
||||
,{tempPqs.getVuDev1(),tempPqs.getVuDev2(),tempPqs.getVuDev3(),tempPqs.getVuDev4(),tempPqs.getVuDev5(),}
|
||||
,{tempPqs.getVUnbalance1(),tempPqs.getVUnbalance2(),tempPqs.getVUnbalance3(),tempPqs.getVUnbalance4(),tempPqs.getVUnbalance5(),}
|
||||
,{tempPqs.getEvent1(),tempPqs.getEvent2(),tempPqs.getEvent3(),tempPqs.getEvent4(),tempPqs.getEvent5(),}};
|
||||
//获取该值返回的数据
|
||||
float temp=GetComAsses(f1);
|
||||
allData+=temp;
|
||||
}
|
||||
float aveData=allData/pqsComasses.size();
|
||||
return FloatUtils.get2Float(aveData);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//熵权法求权重
|
||||
private boolean getSqf() {
|
||||
boolean blSqfFlag = true;
|
||||
try {
|
||||
int i, j;
|
||||
float k, m;
|
||||
float sum[] = new float[ST_QT_NUM];
|
||||
float e[] = new float[ST_QT_NUM];
|
||||
float d[] = new float[ST_QT_NUM];
|
||||
|
||||
//计算第j个指标的熵值e(j)
|
||||
m = GRADE_NUM;
|
||||
//k = (1/1.6094379124341);
|
||||
k = (float) (1 / ((Math.log(m)) / Math.log(2.7183)));
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
sum[i] = 0;
|
||||
for (j = 0; j < GRADE_NUM; j++) {
|
||||
if (Assess[i][j] != 0)
|
||||
sum[i] += Assess[i][j] * (Math.log(Assess[i][j]) / Math.log(2.7183));
|
||||
}
|
||||
e[i] = -k * sum[i];
|
||||
}
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
d[i] = 1 - e[i];
|
||||
sum[0] = 0;
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
sum[0] += d[i];
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
Weight[i][METHOD_IDX4] = d[i] / sum[0];
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
blSqfFlag = false;
|
||||
}
|
||||
return blSqfFlag;
|
||||
}
|
||||
|
||||
//变异系数法求权重
|
||||
private boolean getBysxf() {
|
||||
boolean blBysxfFlag = true;
|
||||
try {
|
||||
float avg_f[] = new float[ST_QT_NUM];//平均值
|
||||
float std_f[] = new float[ST_QT_NUM];//标准差
|
||||
float byxs[] = new float[ST_QT_NUM];//变异系数
|
||||
float sum;
|
||||
int i, j;
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
avg_f[i] = 0;
|
||||
std_f[i] = 0;
|
||||
byxs[i] = 0;
|
||||
}
|
||||
//求平均值
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
sum = 0;
|
||||
for (j = 0; j < GRADE_NUM; j++)
|
||||
sum += Assess[i][j];
|
||||
avg_f[i] = sum / GRADE_NUM;
|
||||
}
|
||||
//求标准差
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
sum = 0;
|
||||
for (j = 0; j < GRADE_NUM; j++)
|
||||
sum += Math.pow((Assess[i][j] - avg_f[i]), 2);
|
||||
std_f[i] = (float) (Math.sqrt(sum / GRADE_NUM));
|
||||
}
|
||||
//求变异系数
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
if (avg_f[i] < 0)
|
||||
avg_f[i] = 0 - avg_f[i];
|
||||
byxs[i] = std_f[i] / avg_f[i];
|
||||
}
|
||||
sum = 0;
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
sum += byxs[i];
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
Weight[i][METHOD_IDX5] = byxs[i] / sum;
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
blBysxfFlag = false;
|
||||
}
|
||||
return blBysxfFlag;
|
||||
}
|
||||
|
||||
//求综合权重,主观权重和客观权重占比相等各自50%
|
||||
private boolean getZhqzf() {
|
||||
float D[] = new float[ST_QT_NUM];
|
||||
float e[] = new float[ST_QT_NUM];
|
||||
float C[][] = new float[ST_QT_NUM][ST_QT_NUM];
|
||||
float C1[][] = new float[ST_QT_NUM][ST_QT_NUM];
|
||||
float tmp1[] = new float[ST_QT_NUM];
|
||||
float tmp2[] = new float[ST_QT_NUM];
|
||||
boolean blZhqzfFlag = true;
|
||||
try {
|
||||
int i, j, k;
|
||||
float t1, t2;
|
||||
|
||||
//求C
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
tmp1[i] = 0;
|
||||
for (j = 0; j < GRADE_NUM; j++)
|
||||
tmp1[i] += 2 * METHOD_NUM * Math.pow(Assess[i][j], 2);
|
||||
}
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
for (j = 0; j < ST_QT_NUM; j++) {
|
||||
if (i == j)
|
||||
C[i][j] = tmp1[i];
|
||||
else
|
||||
C[i][j] = 0;
|
||||
}
|
||||
}
|
||||
//求C的逆矩阵C1,由于C是对角矩阵,简化矩阵求逆
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
for (j = 0; j < ST_QT_NUM; j++) {
|
||||
if (i == j)
|
||||
C1[i][j] = (float) 1.0 / C[i][j];
|
||||
else
|
||||
C1[i][j] = 0;
|
||||
}
|
||||
}
|
||||
//求D
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
tmp1[i] = 0;
|
||||
for (k = 0; k < METHOD_NUM; k++)
|
||||
tmp1[i] += Weight[i][k];
|
||||
tmp2[i] = 0;
|
||||
for (j = 0; j < GRADE_NUM; j++) {
|
||||
tmp2[i] += tmp1[i] * Math.pow(Assess[i][j], 2);
|
||||
}
|
||||
D[i] = 2 * tmp2[i];
|
||||
}
|
||||
//e赋值
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
e[i] = 1;
|
||||
//计算eT*C1
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
tmp1[i] = 0;
|
||||
for (j = 0; j < ST_QT_NUM; j++)
|
||||
tmp1[i] += e[i] * C1[j][i];
|
||||
}
|
||||
t1 = 0;
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
t1 += tmp1[i] * e[i];
|
||||
t2 = 0;
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
t2 += tmp1[i] * D[i];
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
e[i] = e[i] * ((1 - t2) / t1);
|
||||
for (i = 0; i < ST_QT_NUM; i++)
|
||||
D[i] = D[i] + e[i];
|
||||
//求A
|
||||
for (i = 0; i < ST_QT_NUM; i++) {
|
||||
A[i] = 0;
|
||||
for (j = 0; j < ST_QT_NUM; j++)
|
||||
A[i] += C1[i][j] * D[j];
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.error(ex.getMessage());
|
||||
blZhqzfFlag = false;
|
||||
}
|
||||
return blZhqzfFlag;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
import com.njcn.common.pojo.constant.LogInfo;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2021年06月22日 10:25
|
||||
*/
|
||||
@Slf4j
|
||||
public class ControllerUtil {
|
||||
|
||||
/**
|
||||
* 针对methodArgumentNotValidException 异常的处理
|
||||
* @author cdf
|
||||
*/
|
||||
public static String getMethodArgumentNotValidException(MethodArgumentNotValidException methodArgumentNotValidException) {
|
||||
String operate = LogInfo.UNKNOWN_OPERATE;
|
||||
Method method = null;
|
||||
try {
|
||||
method = methodArgumentNotValidException.getParameter().getMethod();
|
||||
if (!Objects.isNull(method) && method.isAnnotationPresent(ApiOperation.class)) {
|
||||
ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
|
||||
operate = apiOperation.value();
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("根据方法参数非法异常获取@ApiOperation注解值失败,参数非法异常信息:{},方法名:{},异常信息:{}",methodArgumentNotValidException.getMessage(),method,e.getMessage());
|
||||
}
|
||||
return operate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author hongawen(创建) -----denghuajun(移植使用)
|
||||
* @Date: 2018/8/27 11:29
|
||||
*/
|
||||
public class FloatUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 保留传入进来的float的两位小数,四舍五入的方式
|
||||
*
|
||||
* @param data Float参数
|
||||
*/
|
||||
public static float get2Float(Float data) {
|
||||
if (data == null) {
|
||||
return 0f;
|
||||
}
|
||||
int scale = 2;//设置位数
|
||||
int roundingMode = 4;//表示四舍五入,可以选择其他舍值方式,例如去尾,等等.
|
||||
BigDecimal bd = new BigDecimal(data);
|
||||
bd = bd.setScale(scale, roundingMode);
|
||||
data = bd.floatValue();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* pqs
|
||||
*
|
||||
* @author cdf
|
||||
* @date 2022/2/28
|
||||
*/
|
||||
public class GeneralUtil {
|
||||
|
||||
/**
|
||||
* 按指定大小,分隔集合,将集合按规定个数分为n个部分
|
||||
* @author cdf
|
||||
* @date 2021/10/26
|
||||
*/
|
||||
public static List<List<String>> splitList(List<String> list, int len){
|
||||
if(CollectionUtil.isEmpty(list) || len<1){
|
||||
return null;
|
||||
}
|
||||
List<List<String>> result = new ArrayList<>();
|
||||
int size = list.size();
|
||||
int count = (size+len-1)/1000;
|
||||
for(int i=0;i<count;i++){
|
||||
List<String> subList= list.subList(i*len,((i+1)*len>size?size:len*(i+1)));
|
||||
result.add(subList);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
import com.alibaba.nacos.shaded.com.google.protobuf.ServiceException;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* HttpServlet工具类,获取当前request和response
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2022年04月01日 11:04
|
||||
*/
|
||||
public class HttpServletUtil {
|
||||
|
||||
|
||||
public static HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if (requestAttributes == null) {
|
||||
throw new BusinessException(CommonResponseEnum.REQUEST_EMPTY);
|
||||
} else {
|
||||
return requestAttributes.getRequest();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static HttpServletResponse getResponse() {
|
||||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if (requestAttributes == null) {
|
||||
throw new BusinessException(CommonResponseEnum.REQUEST_EMPTY);
|
||||
} else {
|
||||
return requestAttributes.getResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @createTime 2021年05月25日 11:25
|
||||
*/
|
||||
@Slf4j
|
||||
public class IpUtils {
|
||||
|
||||
|
||||
private static final String UNKNOWN = "unknown";
|
||||
private static final String LOCALHOST_IPV4 = "127.0.0.1";
|
||||
private static final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
|
||||
private static final String SEPARATOR = ",";
|
||||
|
||||
private static final String HEADER_X_FORWARDED_FOR = "x-forwarded-for";
|
||||
private static final String HEADER_PROXY_CLIENT_IP = "Proxy-Client-IP";
|
||||
private static final String HEADER_WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";
|
||||
|
||||
/**
|
||||
* 获取真实客户端IP
|
||||
*/
|
||||
public static String getRealIpAddress(ServerHttpRequest serverHttpRequest) {
|
||||
String ipAddress;
|
||||
try {
|
||||
// 1.根据常见的代理服务器转发的请求ip存放协议,从请求头获取原始请求ip。值类似于203.98.182.163, 203.98.182.163
|
||||
ipAddress = serverHttpRequest.getHeaders().getFirst(HEADER_X_FORWARDED_FOR);
|
||||
if (StrUtil.isBlankIfStr(ipAddress) || UNKNOWN.equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = serverHttpRequest.getHeaders().getFirst(HEADER_PROXY_CLIENT_IP);
|
||||
}
|
||||
if (StrUtil.isBlankIfStr(ipAddress) || UNKNOWN.equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = serverHttpRequest.getHeaders().getFirst(HEADER_WL_PROXY_CLIENT_IP);
|
||||
}
|
||||
|
||||
|
||||
// 2.如果没有转发的ip,则取当前通信的请求端的ip
|
||||
if (StrUtil.isBlankIfStr(ipAddress) || UNKNOWN.equalsIgnoreCase(ipAddress)) {
|
||||
InetSocketAddress inetSocketAddress = serverHttpRequest.getRemoteAddress();
|
||||
if (!Objects.isNull(inetSocketAddress)) {
|
||||
ipAddress = inetSocketAddress.getAddress().getHostAddress();
|
||||
}
|
||||
// 如果是127.0.0.1,则取本地真实ip
|
||||
if (LOCALHOST_IPV4.equals(ipAddress) || LOCALHOST_IPV6.equals(ipAddress)) {
|
||||
InetAddress localAddress = NetUtil.getLocalhost();;
|
||||
if (localAddress.getHostAddress() != null) {
|
||||
ipAddress = localAddress.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
||||
// "***.***.***.***"
|
||||
if (ipAddress != null) {
|
||||
ipAddress = ipAddress.split(SEPARATOR)[0].trim();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("解析请求IP失败,{}", Stream.of(e.getStackTrace()).map(StackTraceElement::toString).collect(Collectors.joining(StrUtil.CRLF)));
|
||||
ipAddress = "";
|
||||
}
|
||||
return ipAddress == null ? "" : ipAddress;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class PqsComasses implements Serializable {
|
||||
private LocalDateTime timeid;
|
||||
private String lineid;
|
||||
private float freqDev1;
|
||||
private float freqDev2;
|
||||
private float freqDev3;
|
||||
private float freqDev4;
|
||||
private float freqDev5;
|
||||
private float vuDev1;
|
||||
private float vuDev2;
|
||||
private float vuDev3;
|
||||
private float vuDev4;
|
||||
private float vuDev5;
|
||||
private float dataPst1;
|
||||
private float dataPst2;
|
||||
private float dataPst3;
|
||||
private float dataPst4;
|
||||
private float dataPst5;
|
||||
private float vUnbalance1;
|
||||
private float vUnbalance2;
|
||||
private float vUnbalance3;
|
||||
private float vUnbalance4;
|
||||
private float vUnbalance5;
|
||||
private float vThd1;
|
||||
private float vThd2;
|
||||
private float vThd3;
|
||||
private float vThd4;
|
||||
private float vThd5;
|
||||
private float event1;
|
||||
private float event2;
|
||||
private float event3;
|
||||
private float event4;
|
||||
private float event5;
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.nimbusds.jose.JWSObject;
|
||||
import com.njcn.common.pojo.constant.SecurityConstants;
|
||||
import com.njcn.common.pojo.constant.LogInfo;
|
||||
import com.njcn.common.pojo.dto.LogInfoDTO;
|
||||
import com.njcn.common.pojo.enums.auth.AuthenticationMethodEnum;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.util.Strings;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 请求工具类
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2021年05月11日 14:03
|
||||
*/
|
||||
@Slf4j
|
||||
public class RequestUtil {
|
||||
|
||||
|
||||
/**
|
||||
* 获取登录认证的客户端ID
|
||||
* 兼容两种方式获取OAuth2客户端信息(client_id、client_secret)
|
||||
* 放在请求头(Request Headers)中的Authorization字段,且经过加密,例如 Basic Y2xpZW50OnNlY3JldA== 明文等于 client:secret
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static String getOAuth2ClientId() {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
// 从请求路径中获取
|
||||
String clientId = "";
|
||||
// 从请求头获取
|
||||
String basic = request.getHeader(SecurityConstants.AUTHORIZATION_KEY);
|
||||
if (StrUtil.isNotBlank(basic) && basic.startsWith(SecurityConstants.BASIC_PREFIX)) {
|
||||
basic = basic.replace(SecurityConstants.BASIC_PREFIX, Strings.EMPTY);
|
||||
String basicPlainText = new String(Base64.getDecoder().decode(basic.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
|
||||
clientId = basicPlainText.split(":")[0]; //client:secret
|
||||
}
|
||||
return clientId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析JWT获取获取认证方式
|
||||
*
|
||||
* @return .
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static String getAuthenticationMethod() {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
String refreshToken = request.getParameter(SecurityConstants.REFRESH_TOKEN_KEY);
|
||||
String payload = StrUtil.toString(JWSObject.parse(refreshToken).getPayload());
|
||||
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(payload);
|
||||
|
||||
String authenticationMethod = jsonObject.getStr(SecurityConstants.AUTHENTICATION_METHOD);
|
||||
if (StrUtil.isBlank(authenticationMethod)) {
|
||||
authenticationMethod = AuthenticationMethodEnum.USERNAME.getValue();
|
||||
}
|
||||
return authenticationMethod;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* 获取请求体
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||
if(Objects.nonNull(requestAttributes)){
|
||||
return ((ServletRequestAttributes)requestAttributes).getRequest();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求体
|
||||
*/
|
||||
public static HttpServletRequest getRequest(ServerHttpRequest request) {
|
||||
ServletServerHttpRequest servletServerHttpRequest = (ServletServerHttpRequest) request;
|
||||
return servletServerHttpRequest.getServletRequest();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取请求头中的IP地址
|
||||
*/
|
||||
public static String getRealIp() {
|
||||
String ip = getRequest().getHeader(SecurityConstants.REQUEST_HEADER_KEY_CLIENT_REAL_IP);
|
||||
return StrUtil.isBlank(ip) ? LogInfo.UNKNOWN_IP : ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取头中存储的IP地址
|
||||
*/
|
||||
public static String getRealIp(HttpServletRequest request) {
|
||||
String ip = request.getHeader(SecurityConstants.REQUEST_HEADER_KEY_CLIENT_REAL_IP);
|
||||
return StrUtil.isBlank(ip) ? LogInfo.UNKNOWN_IP : ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* ServerHttpRequest获取头中存储的IP地址
|
||||
*/
|
||||
public static String getRealIp(ServerHttpRequest request) {
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
String ip = CollectionUtils.isEmpty(headers.getOrEmpty(SecurityConstants.REQUEST_HEADER_KEY_CLIENT_REAL_IP)) ? "" :
|
||||
headers.getOrEmpty(SecurityConstants.REQUEST_HEADER_KEY_CLIENT_REAL_IP).get(0);
|
||||
return StrUtil.isBlank(ip) ? LogInfo.UNKNOWN_IP : ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的用户token元信息
|
||||
*/
|
||||
public static JSONObject getJwtPayload() {
|
||||
JSONObject jsonObject = null;
|
||||
String jwtPayload = getRequest().getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
|
||||
try {
|
||||
if (StrUtil.isNotBlank(jwtPayload)) {
|
||||
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
|
||||
jsonObject = JSONObject.fromObject(jwtPayload);
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("解码网关中心传递的请求头中内容异常,异常为:{}", e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的用户token元信息
|
||||
*/
|
||||
public static JSONObject getJwtPayload(HttpServletRequest request) {
|
||||
JSONObject jsonObject = null;
|
||||
String jwtPayload = request.getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
|
||||
try {
|
||||
if (StrUtil.isNotBlank(jwtPayload)) {
|
||||
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
|
||||
jsonObject = JSONObject.fromObject(jwtPayload);
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("解码网关中心传递的请求头中内容异常,异常为:{}", e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* ServerHttpRequest获取在网关中存储的用户token元信息
|
||||
*/
|
||||
public static JSONObject getJwtPayload(ServerHttpRequest request) {
|
||||
JSONObject jsonObject = null;
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
try {
|
||||
String jwtPayload = headers.get(SecurityConstants.JWT_PAYLOAD_KEY).get(0);
|
||||
if (StrUtil.isNotBlank(jwtPayload)) {
|
||||
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
|
||||
jsonObject = JSONObject.fromObject(jwtPayload);
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("解码网关中心传递的请求头中内容异常,异常为:{}", e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的用户索引
|
||||
*/
|
||||
public static String getUserIndex() {
|
||||
String userIndex = LogInfo.UNKNOWN_USER;
|
||||
JSONObject jwtPayload = getJwtPayload();
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
userIndex = jwtPayload.getString(SecurityConstants.USER_INDEX_KEY);
|
||||
}
|
||||
return userIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的部门索引
|
||||
*/
|
||||
public static String getDeptIndex() {
|
||||
String deptIndex = LogInfo.UNKNOWN_DEPT;
|
||||
JSONObject jwtPayload = getJwtPayload();
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
deptIndex = jwtPayload.getString(SecurityConstants.DEPT_INDEX_KEY);
|
||||
}
|
||||
return deptIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的用户登录名
|
||||
*/
|
||||
public static String getUsername() {
|
||||
String userSign = LogInfo.UNKNOWN_USER;
|
||||
JSONObject jwtPayload = getJwtPayload();
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
String userSignTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY);
|
||||
userSign = StrUtil.isBlank(userSignTemp) ? LogInfo.UNKNOWN_USER : userSignTemp;
|
||||
}
|
||||
return userSign;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的用户登录名
|
||||
*/
|
||||
public static String getUsername(HttpServletRequest request) {
|
||||
String userSign = LogInfo.UNKNOWN_USER;
|
||||
JSONObject jwtPayload = getJwtPayload(request);
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
String userSignTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY);
|
||||
userSign = StrUtil.isBlank(userSignTemp) ? LogInfo.UNKNOWN_USER : userSignTemp;
|
||||
}
|
||||
return userSign;
|
||||
}
|
||||
|
||||
/**
|
||||
* ServerHttpRequest获取在网关中存储的用户登录名
|
||||
*/
|
||||
public static String getUsername(ServerHttpRequest request) {
|
||||
String userSign = LogInfo.UNKNOWN_USER;
|
||||
JSONObject jwtPayload = getJwtPayload(request);
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
String userSignTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY);
|
||||
userSign = StrUtil.isBlank(userSignTemp) ? LogInfo.UNKNOWN_USER : userSignTemp;
|
||||
}
|
||||
return userSign;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的用户昵称
|
||||
*/
|
||||
public static String getUserNickname() {
|
||||
String nickname = LogInfo.UNKNOWN_USER;
|
||||
JSONObject jwtPayload = getJwtPayload();
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
String nicknameTemp = jwtPayload.getString(SecurityConstants.USER_NICKNAME_KEY);
|
||||
nickname = StrUtil.isBlank(nicknameTemp) ? LogInfo.UNKNOWN_USER : nicknameTemp;
|
||||
}
|
||||
return nickname;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的用户昵称
|
||||
*/
|
||||
public static String getUserNickname(HttpServletRequest request) {
|
||||
String nickname = LogInfo.UNKNOWN_USER;
|
||||
JSONObject jwtPayload = getJwtPayload(request);
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
String nicknameTemp = jwtPayload.getString(SecurityConstants.USER_NICKNAME_KEY);
|
||||
nickname = StrUtil.isBlank(nicknameTemp) ? LogInfo.UNKNOWN_USER : nicknameTemp;
|
||||
}
|
||||
return nickname;
|
||||
}
|
||||
|
||||
/**
|
||||
* ServerHttpRequest获取在网关中存储的用户昵称
|
||||
*/
|
||||
public static String getUserNickname(ServerHttpRequest request) {
|
||||
String nickname = LogInfo.UNKNOWN_USER;
|
||||
JSONObject jwtPayload = getJwtPayload(request);
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
String nicknameTemp = jwtPayload.getString(SecurityConstants.USER_NICKNAME_KEY);
|
||||
nickname = StrUtil.isBlank(nicknameTemp) ? LogInfo.UNKNOWN_USER : nicknameTemp;
|
||||
}
|
||||
return nickname;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取在网关中存储的载体中的clientId
|
||||
*/
|
||||
public static String getClientId() {
|
||||
String client = LogInfo.UNKNOWN_CLIENT;
|
||||
JSONObject jwtPayload = getJwtPayload();
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
String clientTemp = jwtPayload.getString(SecurityConstants.CLIENT_ID_KEY);
|
||||
client = StrUtil.isBlank(clientTemp) ? LogInfo.UNKNOWN_CLIENT : clientTemp;
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* HttpServletRequest获取用户登录名
|
||||
*/
|
||||
public static String getLoginName(HttpServletRequest request) {
|
||||
String loginName = (String) request.getAttribute(SecurityConstants.AUTHENTICATE_USERNAME);
|
||||
return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName;
|
||||
}
|
||||
|
||||
/**
|
||||
* ServerHttpRequest获取用户登录名
|
||||
*/
|
||||
public static String getLoginName(ServerHttpRequest serverHttpRequest) {
|
||||
HttpServletRequest request = getRequest(serverHttpRequest);
|
||||
String loginName = (String) request.getAttribute(SecurityConstants.AUTHENTICATE_USERNAME);
|
||||
return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户登录名
|
||||
*/
|
||||
public static String getLoginName() {
|
||||
String loginName = (String) getRequest().getAttribute(SecurityConstants.AUTHENTICATE_USERNAME);
|
||||
return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 认证之前存储用户登录名
|
||||
*/
|
||||
public static void saveLoginName(String loginName) {
|
||||
getRequest().setAttribute(SecurityConstants.AUTHENTICATE_USERNAME, loginName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上游服务名称
|
||||
*/
|
||||
public static String getServerName() {
|
||||
String serverName = getRequest().getHeader(SecurityConstants.SERVER_NAME);
|
||||
if (StringUtils.isBlank(serverName)) {
|
||||
return LogInfo.UNKNOWN_SERVER;
|
||||
} else {
|
||||
return serverName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局异常捕获处,初始化用户名、登录名、ip信息
|
||||
*/
|
||||
public static LogInfoDTO initLogInfo(HttpServletRequest request) {
|
||||
String ip = request.getHeader(SecurityConstants.REQUEST_HEADER_KEY_CLIENT_REAL_IP);
|
||||
LogInfoDTO temp = new LogInfoDTO();
|
||||
temp.setIp(StrUtil.isBlank(ip) ? LogInfo.UNKNOWN_IP : ip);
|
||||
String username = LogInfo.UNKNOWN_USER;
|
||||
JSONObject jwtPayload = getJwtPayload(request);
|
||||
if (Objects.nonNull(jwtPayload)) {
|
||||
String userSignTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY);
|
||||
username = StrUtil.isBlank(userSignTemp) ? LogInfo.UNKNOWN_USER : userSignTemp;
|
||||
}
|
||||
temp.setUserName(username);
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.Validation;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2021年06月10日 14:10
|
||||
*/
|
||||
public class ValidatorUtils {
|
||||
|
||||
/**
|
||||
* 校验所有字段并返回不合法字段
|
||||
*/
|
||||
public static void validate(@Valid Object domain) {
|
||||
Set<ConstraintViolation<@Valid Object>> validateSet = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator()
|
||||
.validate(domain, new Class[0]);
|
||||
if (!CollectionUtil.isEmpty(validateSet)) {
|
||||
String messages = validateSet.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.reduce((m1, m2) -> m1 + ";" + m2)
|
||||
.orElse("参数输入有误!");
|
||||
throw new IllegalArgumentException(messages);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.njcn.web.utils;
|
||||
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.njcn.common.pojo.enums.auth.ClientEnum;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.common.utils.HttpResultUtil;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2021年05月24日 16:56
|
||||
*/
|
||||
public class WebUtil {
|
||||
|
||||
private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* @param code 响应码
|
||||
* @param message 响应信息
|
||||
*/
|
||||
public static void responseInfo( HttpServletResponse response, String code, String message) throws IOException {
|
||||
response.setStatus(HttpStatus.HTTP_OK);
|
||||
response.setHeader("Access-Control-Allow-Origin", "*");
|
||||
response.setHeader("Cache-Control", "no-cache");
|
||||
//可以使用封装类简写Content-Type,使用该方法则无需使用setCharacterEncoding
|
||||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
HttpResult<String> result = HttpResultUtil.assembleResult(code, null, message);
|
||||
response.getWriter().print(OBJECT_MAPPER.writeValueAsString(result));
|
||||
response.getWriter().flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 对应的部门类型
|
||||
*/
|
||||
public static List<Integer> filterDeptType() {
|
||||
List<Integer> deptType = new ArrayList<>();
|
||||
String clientId = RequestUtil.getClientId();
|
||||
String clientType = ClientEnum.getClientType(clientId);
|
||||
switch (clientType) {
|
||||
case "app":
|
||||
deptType.add(2);
|
||||
break;
|
||||
case "screen":
|
||||
deptType.add(0);
|
||||
break;
|
||||
default:
|
||||
deptType.add(0);
|
||||
deptType.add(1);
|
||||
deptType.add(3);
|
||||
break;
|
||||
}
|
||||
return deptType;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user