10 changed files with 50 additions and 426 deletions
@ -1,93 +0,0 @@ |
|||||
package cn.chjyj.szwh.utils; |
|
||||
|
|
||||
import cn.pelerin.wh.transaction.model.exception.ServiceException; |
|
||||
import org.apache.commons.collections4.CollectionUtils; |
|
||||
import org.apache.commons.lang3.ArrayUtils; |
|
||||
import org.apache.commons.lang3.StringUtils; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
import org.springframework.util.ReflectionUtils; |
|
||||
|
|
||||
import java.lang.reflect.Field; |
|
||||
import java.util.Collection; |
|
||||
import java.util.List; |
|
||||
|
|
||||
/** |
|
||||
* 断言 - 中断往下操作, 直接返回到前端 |
|
||||
* |
|
||||
* @author xie.xinquan |
|
||||
* @create 2021/5/13 11:21 |
|
||||
* @company 深圳亿起融网络科技有限公司 |
|
||||
*/ |
|
||||
@Component |
|
||||
public class AssertUtil { |
|
||||
|
|
||||
private static final String defaultNullMsg = "参数为空"; |
|
||||
|
|
||||
public static void haveLength(String str, String msg) { |
|
||||
if (StringUtils.isEmpty(str)) { |
|
||||
throw new ServiceException(msg); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 校验前端传过来的数据 |
|
||||
* |
|
||||
* @param t |
|
||||
* @param list |
|
||||
* @param msg |
|
||||
* @param <T> |
|
||||
*/ |
|
||||
public static <T> void dataNotNull(T t, List<T> list, String msg) { |
|
||||
if (t instanceof String && StringUtils.isEmpty((String) t) && CollectionUtils.isEmpty(list)) { |
|
||||
throw new ServiceException(msg); |
|
||||
} |
|
||||
if (t == null && CollectionUtils.isEmpty(list)) { |
|
||||
throw new ServiceException(msg); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public static void collectionNotNull(Collection collection, String msg) { |
|
||||
if (CollectionUtils.isEmpty(collection)) { |
|
||||
throw new ServiceException(msg); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public static void notNull(Object obj) { |
|
||||
notNull(obj, defaultNullMsg); |
|
||||
} |
|
||||
|
|
||||
public static void notNull(Object obj, String msg) { |
|
||||
if (obj == null) { |
|
||||
throw new ServiceException(msg); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public static void notNull(Object obj, String... fieldNames) { |
|
||||
notNull(defaultNullMsg, obj, fieldNames); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 校验字段是否为空 |
|
||||
* |
|
||||
* @param msg |
|
||||
* @param obj |
|
||||
* @param fieldNames |
|
||||
*/ |
|
||||
public static void notNull(String msg, Object obj, String... fieldNames) { |
|
||||
notNull(obj, msg); |
|
||||
if (ArrayUtils.isEmpty(fieldNames)) { |
|
||||
throw new ServiceException(msg); |
|
||||
} |
|
||||
for (String fieldName : fieldNames) { |
|
||||
Field field = ReflectionUtils.findField(obj.getClass(), fieldName); |
|
||||
notNull(field, msg); |
|
||||
field.setAccessible(true); |
|
||||
Object value = ReflectionUtils.getField(field, obj); |
|
||||
if (field.getType().isAssignableFrom(String.class)) { |
|
||||
haveLength((String) value, msg); |
|
||||
} else { |
|
||||
notNull(value, msg); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,46 +0,0 @@ |
|||||
package cn.chjyj.szwh.utils; |
|
||||
|
|
||||
import com.google.common.collect.Lists; |
|
||||
import org.springframework.util.ObjectUtils; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
/** |
|
||||
* @author xie.xinquan |
|
||||
* @create 2021/1/11 14:17 |
|
||||
* @company 深圳亿起融网络科技有限公司 |
|
||||
*/ |
|
||||
public class DataUtil { |
|
||||
|
|
||||
|
|
||||
/** |
|
||||
* 将大集合分批成多个子集 -- 设置默认值 |
|
||||
*/ |
|
||||
public static <E> List<List<E>> bigSetSplit(List<E> list){ |
|
||||
return bigSetSplit(list, null); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 将大集合分批成多个子集 |
|
||||
*/ |
|
||||
public static <E> List<List<E>> bigSetSplit(List<E> list, Integer batchCount){ |
|
||||
if (ObjectUtils.isEmpty(list)){ |
|
||||
return Lists.newArrayList(); |
|
||||
} |
|
||||
List<List<E>> result = Lists.newArrayList(); |
|
||||
|
|
||||
// 每批commit的个数
|
|
||||
batchCount = batchCount == null || batchCount <= 0 ? 100 : batchCount; |
|
||||
//循环次数
|
|
||||
int cycleTimes = list.size() % batchCount == 0 ? list.size() / batchCount : list.size() / batchCount + 1; |
|
||||
|
|
||||
for (int i = 0; i < cycleTimes; i++){ |
|
||||
int startIndex = i * batchCount; |
|
||||
int endIndex = (i+1) * batchCount; |
|
||||
endIndex = endIndex > list.size() ? list.size() : endIndex; |
|
||||
|
|
||||
result.add(list.subList(startIndex, endIndex)); |
|
||||
} |
|
||||
return result; |
|
||||
} |
|
||||
} |
|
||||
@ -1,146 +0,0 @@ |
|||||
package cn.chjyj.szwh.utils; |
|
||||
|
|
||||
import com.alibaba.druid.DbType; |
|
||||
import com.alibaba.druid.sql.SQLUtils; |
|
||||
import com.google.common.collect.Lists; |
|
||||
import org.apache.commons.collections4.MapUtils; |
|
||||
import org.apache.commons.io.FileUtils; |
|
||||
import org.apache.commons.lang3.StringUtils; |
|
||||
|
|
||||
import java.io.File; |
|
||||
import java.io.IOException; |
|
||||
import java.io.InputStream; |
|
||||
import java.sql.*; |
|
||||
import java.util.*; |
|
||||
import java.util.concurrent.TimeUnit; |
|
||||
import java.util.stream.Stream; |
|
||||
|
|
||||
/** |
|
||||
* @author xie.xinquan |
|
||||
* @create 2022/6/15 10:15 |
|
||||
* @company 深圳亿起融网络科技有限公司 |
|
||||
*/ |
|
||||
public class FormatLogSqlUtil { |
|
||||
|
|
||||
private static final String phpLogFilePath = "D:\\project\\wh_php\\runtime\\admin\\log\\202206\\"; |
|
||||
|
|
||||
|
|
||||
public static void main(String[] args) throws IOException, InterruptedException { |
|
||||
|
|
||||
File file = new File(phpLogFilePath); |
|
||||
File[] files = file.listFiles(); |
|
||||
File lastFile = Stream.of(files).max(Comparator.comparing(o -> o.lastModified())).get(); |
|
||||
long l = lastFile.lastModified(); |
|
||||
List<String> list = FileUtils.readLines(lastFile, "utf-8"); |
|
||||
int size = list.size(); |
|
||||
while (true) { |
|
||||
TimeUnit.SECONDS.sleep(1); |
|
||||
if (lastFile.lastModified() > l) { |
|
||||
l = lastFile.lastModified(); |
|
||||
list = FileUtils.readLines(lastFile, "utf-8"); |
|
||||
List<String> data = list.subList(size, list.size()); |
|
||||
size = list.size(); |
|
||||
for (String str : data) { |
|
||||
str = StringUtils.substringAfter(str, " "); |
|
||||
str = StringUtils.substringBeforeLast(str, "["); |
|
||||
try { |
|
||||
String format = SQLUtils.format(str, DbType.mysql); |
|
||||
if (StringUtils.isNotEmpty(format)) { |
|
||||
System.out.println("...........start..............."); |
|
||||
System.out.println(format); |
|
||||
// 执行SQL
|
|
||||
execSql(str); |
|
||||
System.out.println("............end................"); |
|
||||
} |
|
||||
}catch (Exception e) { |
|
||||
System.out.println("格式化出错:" + e.getMessage()); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
private static Connection connection ; |
|
||||
private static void setConnection() throws IOException, ClassNotFoundException, SQLException { |
|
||||
Properties properties = new Properties(); |
|
||||
{ |
|
||||
InputStream resourceAsStream = FormatLogSqlUtil.class.getClassLoader().getResourceAsStream("application.properties"); |
|
||||
properties.load(resourceAsStream); |
|
||||
resourceAsStream.close(); |
|
||||
} |
|
||||
String dev = MapUtils.getString(properties, "spring.profiles.active", "dev"); |
|
||||
{ |
|
||||
Properties prop = new Properties(); |
|
||||
InputStream resourceAsStream = FormatLogSqlUtil.class.getClassLoader().getResourceAsStream(String.format("application-%s.properties", dev)); |
|
||||
prop.load(resourceAsStream); |
|
||||
resourceAsStream.close(); |
|
||||
for (Map.Entry<Object, Object> entry : prop.entrySet()) { |
|
||||
properties.put(entry.getKey(), entry.getValue()); |
|
||||
} |
|
||||
} |
|
||||
Class.forName(properties.getProperty("spring.datasource.driverClassName")); |
|
||||
|
|
||||
connection = DriverManager.getConnection(properties.getProperty("spring.datasource.url"), properties.getProperty("spring.datasource.username"), properties.getProperty("spring.datasource.password")); |
|
||||
|
|
||||
} |
|
||||
private static void execSql(String sql) throws SQLException, IOException, ClassNotFoundException { |
|
||||
|
|
||||
if (connection == null) { |
|
||||
setConnection(); |
|
||||
} |
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql); |
|
||||
ResultSet resultSet = preparedStatement.executeQuery(); |
|
||||
|
|
||||
System.out.println("...执行sql start..."); |
|
||||
ResultSetMetaData metaData = resultSet.getMetaData(); |
|
||||
List<List<String>> execdata = new ArrayList<>(); |
|
||||
int columnCount = metaData.getColumnCount(); |
|
||||
{ |
|
||||
int i = 0; |
|
||||
List<String> data = new ArrayList<>(); |
|
||||
while (++i <= columnCount) { |
|
||||
data.add(metaData.getColumnName(i)); |
|
||||
} |
|
||||
execdata.add(data); |
|
||||
} |
|
||||
while (resultSet.next()) { |
|
||||
int i = 0; |
|
||||
List<String> data = new ArrayList<>(); |
|
||||
while (++i <= columnCount) { |
|
||||
data.add(resultSet.getString(i)); |
|
||||
} |
|
||||
execdata.add(data); |
|
||||
} |
|
||||
int[] max = new int[execdata.get(0).size()]; |
|
||||
for (List<String> list : execdata) { |
|
||||
for (int i = 0; i < list.size(); i++) { |
|
||||
String s = list.get(i) == null ? "null" : list.get(i); |
|
||||
max[i] = Math.max(max[i], charLen(s)); |
|
||||
} |
|
||||
} |
|
||||
for (List<String> list : execdata) { |
|
||||
for (int i = 0; i < list.size(); i++) { |
|
||||
String s = list.get(i) == null ? "null" : list.get(i); |
|
||||
while (s.length() < max[i]) s += ' '; |
|
||||
list.set(i, s); |
|
||||
} |
|
||||
System.out.println(StringUtils.join(list, " ")); |
|
||||
} |
|
||||
|
|
||||
System.out.println("...执行sql end..."); |
|
||||
|
|
||||
} |
|
||||
private static int charLen(String str) { |
|
||||
double len = 0; |
|
||||
for (char c : str.toCharArray()) { |
|
||||
if (isChineseChar(c)) { |
|
||||
len += 1.66; |
|
||||
}else { |
|
||||
len += 1; |
|
||||
} |
|
||||
} |
|
||||
return (int) len; |
|
||||
} |
|
||||
public static boolean isChineseChar(char c) { |
|
||||
return String.valueOf(c).matches("[\u4e00-\u9fa5]"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,123 +0,0 @@ |
|||||
package cn.chjyj.szwh.utils; |
|
||||
|
|
||||
import cn.pelerin.wh.transaction.auth.entity.Cert; |
|
||||
import cn.pelerin.wh.transaction.auth.mapper.CertMapper; |
|
||||
import cn.pelerin.wh.transaction.constant.CertTypeConst; |
|
||||
import cn.pelerin.wh.transaction.model.base.SysLogin; |
|
||||
import com.auth0.jwt.JWT; |
|
||||
import com.auth0.jwt.interfaces.DecodedJWT; |
|
||||
import com.google.common.collect.Maps; |
|
||||
import io.jsonwebtoken.Claims; |
|
||||
import io.jsonwebtoken.Jwts; |
|
||||
import io.jsonwebtoken.SignatureAlgorithm; |
|
||||
import io.jsonwebtoken.impl.TextCodec; |
|
||||
import lombok.SneakyThrows; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.apache.commons.io.FileUtils; |
|
||||
import org.apache.tomcat.util.codec.binary.Base64; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
import org.springframework.util.ObjectUtils; |
|
||||
|
|
||||
import javax.annotation.PostConstruct; |
|
||||
import javax.annotation.Resource; |
|
||||
import javax.crypto.SecretKey; |
|
||||
import javax.crypto.spec.SecretKeySpec; |
|
||||
import java.io.File; |
|
||||
import java.util.Map; |
|
||||
import java.util.Objects; |
|
||||
|
|
||||
/** |
|
||||
* @author :luo.laiyong |
|
||||
* @date :Created in 2019/6/5 11:20 |
|
||||
* @company: 深圳亿起融网络科技有限公司 |
|
||||
* @modified By: |
|
||||
*/ |
|
||||
@Slf4j |
|
||||
@Component |
|
||||
public class JwtRsaUtil { |
|
||||
@Resource |
|
||||
private CertMapper certMapper; |
|
||||
private static CertMapper initCertMapper; |
|
||||
|
|
||||
@PostConstruct |
|
||||
public void beforeInit() { |
|
||||
initCertMapper = certMapper; |
|
||||
} |
|
||||
|
|
||||
private static Claims parseJWT(String token, String signKey) { |
|
||||
SecretKey key = generalKey(signKey); //签名秘钥,和生成的签名的秘钥一模一样
|
|
||||
Claims claims = Jwts.parser() //得到DefaultJwtParser
|
|
||||
.setSigningKey(key) //设置签名的秘钥
|
|
||||
.parseClaimsJws(token).getBody();//设置需要解析的jwt
|
|
||||
return claims; |
|
||||
} |
|
||||
|
|
||||
public static SysLogin getUserFromToken(String token) { |
|
||||
try { |
|
||||
DecodedJWT decodedJWT = JWT.decode(token); |
|
||||
if (!ObjectUtils.isEmpty(decodedJWT.getClaims())) { |
|
||||
SysLogin sysLoginInfo = new SysLogin(); |
|
||||
sysLoginInfo.setIat(decodedJWT.getClaim("iat").asInt()); |
|
||||
sysLoginInfo.setMenuPermission(decodedJWT.getClaim("menuPermission").asList(String.class)); |
|
||||
sysLoginInfo.setAccountId(decodedJWT.getClaim("accountId").asString()); |
|
||||
sysLoginInfo.setAccountName(decodedJWT.getClaim("accountName").asString()); |
|
||||
sysLoginInfo.setAud(decodedJWT.getClaim("aud").asString()); |
|
||||
sysLoginInfo.setJti(decodedJWT.getClaim("jti").asString()); |
|
||||
return sysLoginInfo; |
|
||||
} |
|
||||
} catch (Exception e) { |
|
||||
log.error("DecodedJWT error", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public static SecretKey generalKey(String signKey) { |
|
||||
byte[] encodedKey = Base64.decodeBase64(signKey);//本地的密码解码
|
|
||||
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, SignatureAlgorithm.HS256.getValue());// 根据给定的字节数组使用AES加密算法构造一个密钥,使用 encodedKey中的始于且包含 0 到前 leng 个字节这是当然是所有。(后面的文章中马上回推出讲解Java加密和解密的一些算法)
|
|
||||
return key; |
|
||||
} |
|
||||
|
|
||||
@SneakyThrows |
|
||||
public static void main(String[] args) { |
|
||||
//String signKey = "KHXT0V7NVLOFPS9BZ88R5VLIH5COPULV";
|
|
||||
String token = "eyJhbGciOiJIUzUxMiJ9.eyJhdWQiOiJCRDg0REQ0MkE3MjM0QjA1QjBDNUQxMTYxNjEzMkFDNCIsImp0aSI6IjcyMDgyMWZiMjNiNTQ4YjZhMmMxNGNlYjYxMjEzYWI0IiwiYWNjb3VudElkIjoiYWRtaW4iLCJhY2NvdW50TmFtZSI6Iui2hee6p-euoeeQhuWRmCIsIm1lbnVQZXJtaXNzaW9uIjpbIm1fMDAyIiwibV8wMDMiLCJtXzAwNCIsIm1fMDA1IiwibV8wMDYiXSwiaWF0IjoxNjU1MDk5NjU2fQ.pmXSrvjSJjTFB22QyTp3q7ZlzFwcqlHVYI-qCPlc1zfeQa6Spn_AJdAT-VkLyXPbaKUaovroUvZ_ay5vug5Aow"; |
|
||||
getUserFromToken(token); |
|
||||
////Claims claims = parseJWT(token, signKey);
|
|
||||
//String str = Base64Util.encode(signKey);
|
|
||||
//Claims claims = Jwts.parser() //得到DefaultJwtParser
|
|
||||
// .setSigningKey(str) //设置签名的秘钥
|
|
||||
// .parseClaimsJws(token).getBody();
|
|
||||
//System.out.println(claims);
|
|
||||
DecodedJWT decodedJWT = JWT.decode(token); |
|
||||
String json = TextCodec.BASE64URL.decodeToString("eyJhdWQiOiJCRDg0REQ0MkE3MjM0QjA1QjBDNUQxMTYxNjEzMkFDNCIsImp0aSI6IjcyMDgyMWZiMjNiNTQ4YjZhMmMxNGNlYjYxMjEzYWI0IiwiYWNjb3VudElkIjoiYWRtaW4iLCJhY2NvdW50TmFtZSI6Iui2hee6p-euoeeQhuWRmCIsIm1lbnVQZXJtaXNzaW9uIjpbIm1fMDAyIiwibV8wMDMiLCJtXzAwNCIsIm1fMDA1IiwibV8wMDYiXSwiaWF0IjoxNjU1MDk5NjU2fQ"); |
|
||||
System.out.println(json); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 调用其他系统时,需要生成token写入header |
|
||||
* |
|
||||
* @param type |
|
||||
* @return |
|
||||
*/ |
|
||||
public static String createSign(String type) { |
|
||||
Cert cert = initCertMapper.selectOneByStatusAndType(0, type); |
|
||||
//读取私钥
|
|
||||
String privateFilePath = System.getProperty("user.dir") + cert.getPrivateKey(); |
|
||||
String token = ""; |
|
||||
try { |
|
||||
String privateKey = FileUtils.readFileToString(new File(privateFilePath)); |
|
||||
privateKey = privateKey.replaceAll("\\-*BEGIN.*KEY\\-*", "").replaceAll("\\-*END.*KEY\\-*", ""); |
|
||||
if (Objects.equals(CertTypeConst.USER_REAL, type)) { |
|
||||
Map<String, Object> claims = Maps.newHashMap(); |
|
||||
claims.put("aud", "BD84DD42A7234B05B0C5D11616132AC4"); |
|
||||
token = ApiTokenUtils.getToken(cert.getToken(), privateKey, 5 * 60 * 1000L, claims); |
|
||||
} else { |
|
||||
token = ApiTokenUtils.getToken(cert.getToken(), privateKey, 5 * 60 * 1000L); |
|
||||
} |
|
||||
} catch (Exception e) { |
|
||||
log.error("createSign error", e); |
|
||||
} |
|
||||
return token; |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
Loading…
Reference in new issue