修正项目
This commit is contained in:
42
xlcs-parent/common/common-util/pom.xml
Normal file
42
xlcs-parent/common/common-util/pom.xml
Normal file
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.yovinchen</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>common-util</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.yovinchen</groupId>
|
||||
<artifactId>model</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
@@ -0,0 +1,126 @@
|
||||
package com.yovinchen.xlcs.common.utils;
|
||||
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* ClassName: DateUtil
|
||||
* Package: com.yovinchen.xlcs.common.utils
|
||||
* 日期操作工具类
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/12 18:04
|
||||
*/
|
||||
public class DateUtil {
|
||||
|
||||
private static final String dateFormat = "yyyy-MM-dd";
|
||||
private static final String timeFormat = "HH:mm:ss";
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String formatDate(Date date) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
return sdf.format(date);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String formatTime(Date date) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
|
||||
return sdf.format(date);
|
||||
|
||||
}
|
||||
|
||||
public static Date parseTime(String date) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
|
||||
try {
|
||||
return sdf.parse(date);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取比较断两个日期对象的field处的值 。
|
||||
* 如果第一个日期小于、等于、大于第二个,则对应返回负整数、0、正整数
|
||||
*
|
||||
* @param date1 第一个日期对象,非null
|
||||
* @param date2 第二个日期对象,非null
|
||||
* @param field Calendar中的阈值
|
||||
* <p>
|
||||
* date1 > date2 返回:1
|
||||
* date1 = date2 返回:0
|
||||
* date1 < date2 返回:-1
|
||||
*/
|
||||
public static int truncatedCompareTo(final Date date1, final Date date2, final int field) {
|
||||
return DateUtils.truncatedCompareTo(date1, date2, field);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比对日期与时间大小
|
||||
*
|
||||
* @param beginDate
|
||||
* @param endDate
|
||||
* @return
|
||||
*/
|
||||
public static boolean dateCompare(Date beginDate, Date endDate) {
|
||||
// endDate > beginDate
|
||||
return DateUtil.truncatedCompareTo(beginDate, endDate, Calendar.SECOND) != 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 比对日期与时间大小
|
||||
*
|
||||
* @param beginDate
|
||||
* @param endDate
|
||||
* @return
|
||||
*/
|
||||
public static boolean timeCompare(Date beginDate, Date endDate) {
|
||||
Calendar instance1 = Calendar.getInstance();
|
||||
instance1.setTime(beginDate); //设置时间为当前时间
|
||||
instance1.set(Calendar.YEAR, 0);
|
||||
instance1.set(Calendar.MONTH, 0);
|
||||
instance1.set(Calendar.DAY_OF_MONTH, 0);
|
||||
|
||||
Calendar instance2 = Calendar.getInstance();
|
||||
instance2.setTime(endDate); //设置时间为当前时间
|
||||
instance2.set(Calendar.YEAR, 0);
|
||||
instance2.set(Calendar.MONTH, 0);
|
||||
instance2.set(Calendar.DAY_OF_MONTH, 0);
|
||||
// endDate > beginDate
|
||||
return DateUtil.truncatedCompareTo(instance1.getTime(), instance2.getTime(), Calendar.SECOND) != 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间到晚上23点59分59秒的时间间隔,单位:秒
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Long getCurrentExpireTimes() {
|
||||
//过期截止时间
|
||||
Calendar instance = Calendar.getInstance();
|
||||
instance.setTime(new Date()); //设置时间为当前时间
|
||||
instance.set(Calendar.HOUR_OF_DAY, 23);
|
||||
instance.set(Calendar.MINUTE, 59);
|
||||
instance.set(Calendar.SECOND, 59);
|
||||
Date endTime = instance.getTime();
|
||||
//当前时间与截止时间间隔,单位:秒
|
||||
long interval = (endTime.getTime() - new Date().getTime()) / 1000;
|
||||
return 100 * 60 * 60 * 24 * 365L;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,64 @@
|
||||
package com.yovinchen.xlcs.common.utils;
|
||||
|
||||
import io.jsonwebtoken.*;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* ClassName: JwtHelper
|
||||
* Package: com.yovinchen.xlcs.common.utils
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/22 15:50
|
||||
*/
|
||||
public class JwtHelper {
|
||||
|
||||
private static final long tokenExpiration = 365L * 24 * 60 * 60 * 1000;
|
||||
private static final String tokenSignKey = "xlcs";
|
||||
|
||||
public static String createToken(Long userId, String userName) {
|
||||
String token = Jwts.builder()
|
||||
.setSubject("xlcs-USER")
|
||||
.setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
|
||||
.claim("userId", userId)
|
||||
.claim("userName", userName)
|
||||
.signWith(SignatureAlgorithm.HS512, tokenSignKey)
|
||||
.compressWith(CompressionCodecs.GZIP)
|
||||
.compact();
|
||||
return token;
|
||||
}
|
||||
|
||||
public static Long getUserId(String token) {
|
||||
if (StringUtils.isEmpty(token)) return null;
|
||||
|
||||
Jws<Claims> claimsJws = Jwts.parser()
|
||||
.setSigningKey(tokenSignKey)
|
||||
.parseClaimsJws(token);
|
||||
Claims claims = claimsJws.getBody();
|
||||
Integer userId = (Integer) claims.get("userId");
|
||||
return userId.longValue();
|
||||
// return 1L;
|
||||
}
|
||||
|
||||
public static String getUserName(String token) {
|
||||
if (StringUtils.isEmpty(token)) return "";
|
||||
|
||||
Jws<Claims> claimsJws = Jwts.parser()
|
||||
.setSigningKey(tokenSignKey)
|
||||
.parseClaimsJws(token);
|
||||
Claims claims = claimsJws.getBody();
|
||||
return (String) claims.get("userName");
|
||||
}
|
||||
|
||||
public static void removeToken(String token) {
|
||||
//jwttoken无需删除,客户端扔掉即可。
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String token = JwtHelper.createToken(7L, "admin");
|
||||
System.out.println(token);
|
||||
System.out.println(JwtHelper.getUserId(token));
|
||||
System.out.println(JwtHelper.getUserName(token));
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package com.yovinchen.xlcs.common.utils;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
|
||||
public final class MD5 {
|
||||
|
||||
public static String encrypt(String strSrc) {
|
||||
try {
|
||||
char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
|
||||
'9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
byte[] bytes = strSrc.getBytes();
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(bytes);
|
||||
bytes = md.digest();
|
||||
int j = bytes.length;
|
||||
char[] chars = new char[j * 2];
|
||||
int k = 0;
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
byte b = bytes[i];
|
||||
chars[k++] = hexChars[b >>> 4 & 0xf];
|
||||
chars[k++] = hexChars[b & 0xf];
|
||||
}
|
||||
return new String(chars);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("MD5加密出错!!+" + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user