- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *
- * http://www.apache.org/licenses/LICENSE-2.0 - *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.xncoding.jwt.common.util;
-
-import java.sql.Timestamp;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-
-public class DateUtil {
- private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
-
- private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
-
- private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
-
- private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
- private final static SimpleDateFormat sdfmsTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
-
- private final static SimpleDateFormat allTime = new SimpleDateFormat("yyyyMMddHHmmss");
-
- private final static SimpleDateFormat sdfDay_ = new SimpleDateFormat("yyyy/MM/dd");
-
-
- /**
- * 获取YYYY格式
- *
- * @return
- */
- public static String getYear() {
- return sdfYear.format(new Date());
- }
-
- /**
- * 获取YYYY格式
- *
- * @return
- */
- public static String getYear(Date date) {
- return sdfYear.format(date);
- }
-
- /**
- * 获取YYYY-MM-DD格式
- *
- * @return
- */
- public static String getDay() {
- return sdfDay.format(new Date());
- }
-
- /**
- * 获取YYYY-MM-DD格式
- *
- * @return
- */
- public static String getDay(Date date) {
- return sdfDay.format(date);
- }
-
- /**
- * 获取YYYYMMDD格式
- *
- * @return
- */
- public static String getDays() {
- return sdfDays.format(new Date());
- }
-
- /**
- * 获取YYYYMMDD格式
- *
- * @return
- */
- public static String getDays(Date date) {
- return sdfDays.format(date);
- }
-
- /**
- * 获取YYYY/MM/DD格式
- *
- * @return
- */
- public static String getDays_(Date date) {
- return sdfDay_.format(date);
- }
-
- /**
- * 获取YYYY-MM-DD HH:mm:ss格式
- *
- * @return
- */
- public static String getTime() {
- return sdfTime.format(new Date());
- }
-
- /**
- * 获取YYYY-MM-DD HH:mm:ss.SSS格式
- *
- * @return
- */
- public static String getMsTime() {
- return sdfmsTime.format(new Date());
- }
-
- /**
- * 获取YYYYMMDDHHmmss格式
- *
- * @return
- */
- public static String getAllTime() {
- return allTime.format(new Date());
- }
-
- /**
- * 获取YYYY-MM-DD HH:mm:ss格式
- *
- * @return
- */
- public static String getTime(Date date) {
- return sdfTime.format(date);
- }
-
- /**
- * @param s
- * @param e
- * @return boolean
- * @throws
- * @Title: compareDate
- * @Description:(日期比较,如果s>=e 返回true 否则返回false)
- * @author luguosui
- */
- public static boolean compareDate(String s, String e) {
- if (parseDate(s) == null || parseDate(e) == null) {
- return false;
- }
- return parseDate(s).getTime() >= parseDate(e).getTime();
- }
-
- /**
- * 格式化日期
- *
- * @return
- */
- public static Date parseDate(String date) {
- try {
- return sdfDay.parse(date);
- } catch (ParseException e) {
- e.printStackTrace();
- return null;
- }
- }
-
- /**
- * 格式化日期
- *
- * @return
- */
- public static Date parseTime(String date) {
- try {
- return sdfTime.parse(date);
- } catch (ParseException e) {
- e.printStackTrace();
- return null;
- }
- }
-
- /**
- * 格式化日期
- *
- * @return
- */
- public static Date parse(String date, String pattern) {
- DateFormat fmt = new SimpleDateFormat(pattern);
- try {
- return fmt.parse(date);
- } catch (ParseException e) {
- e.printStackTrace();
- return null;
- }
- }
-
- /**
- * 格式化日期
- *
- * @return
- */
- public static String format(Date date, String pattern) {
- DateFormat fmt = new SimpleDateFormat(pattern);
- return fmt.format(date);
- }
-
- /**
- * 把日期转换为Timestamp
- *
- * @param date
- * @return
- */
- public static Timestamp format(Date date) {
- return new Timestamp(date.getTime());
- }
-
- /**
- * 校验日期是否合法
- *
- * @return
- */
- public static boolean isValidDate(String s) {
- try {
- sdfTime.parse(s);
- return true;
- } catch (Exception e) {
- // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
- return false;
- }
- }
-
- /**
- * 校验日期是否合法
- *
- * @return
- */
- public static boolean isValidDate(String s, String pattern) {
- DateFormat fmt = new SimpleDateFormat(pattern);
- try {
- fmt.parse(s);
- return true;
- } catch (Exception e) {
- // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
- return false;
- }
- }
-
- /**
- * 获取指定日期偏移指定时间后的时间
- *
- * @param date 基准日期
- * @param calendarField 偏移的粒度大小(小时、天、月等)使用Calendar中的常数
- * @param offsite 偏移量,正数为向后偏移,负数为向前偏移
- * @return 偏移后的日期
- */
- public static Date offsiteDate(Date date, int calendarField, int offsite) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(calendarField, offsite);
- return cal.getTime();
- }
-
- /**
- * 返回日期零时
- *
- * @param date 目标日期
- * @return 目标日期零时
- */
- public static Date getDateStartTime(Date date) {
- String str = format(date, "yyyyMMdd") + "000000";
- try {
- return allTime.parse(str);
- } catch (ParseException e) {
- return null;
- }
- }
-
-
- /**
- * 返回日期最后时间End
- *
- * @param date 目标日期
- * @return 目标日日期最后时间
- */
- public static Date getDateEndTime(Date date) {
- String str = format(date, "yyyyMMdd") + "235959";
- try {
- return allTime.parse(str);
- } catch (ParseException e) {
- return null;
- }
- }
-
-}
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/common/util/JWTUtil.java b/springboot-jwt/src/main/java/com/xncoding/jwt/common/util/JWTUtil.java
index d2a5ac5..ad2b9b2 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/common/util/JWTUtil.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/common/util/JWTUtil.java
@@ -3,12 +3,10 @@ package com.xncoding.jwt.common.util;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
-import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.UnsupportedEncodingException;
import java.util.Date;
public class JWTUtil {
@@ -18,6 +16,23 @@ public class JWTUtil {
// 过期时间5分钟
private static final long EXPIRE_TIME = 5 * 60 * 1000;
+ /**
+ * 生成签名,5min后过期
+ *
+ * @param username 用户名
+ * @param secret 用户的密码
+ * @return 加密的token
+ */
+ public static String sign(String username, String secret) {
+ Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
+ Algorithm algorithm = Algorithm.HMAC256(secret);
+ // 附带username信息
+ return JWT.create()
+ .withClaim("username", username)
+ .withExpiresAt(date)
+ .sign(algorithm);
+ }
+
/**
* 校验token是否正确
*
@@ -26,17 +41,12 @@ public class JWTUtil {
* @return 是否正确
*/
public static boolean verify(String token, String username, String secret) {
- try {
- Algorithm algorithm = Algorithm.HMAC256(secret);
- JWTVerifier verifier = JWT.require(algorithm)
- .withClaim("username", username)
- .build();
- DecodedJWT jwt = verifier.verify(token);
- return true;
- } catch (Exception exception) {
- log.error("校验token失败", exception);
- return false;
- }
+ Algorithm algorithm = Algorithm.HMAC256(secret);
+ JWTVerifier verifier = JWT.require(algorithm)
+ .withClaim("username", username)
+ .build();
+ DecodedJWT jwt = verifier.verify(token);
+ return true;
}
/**
@@ -45,129 +55,8 @@ public class JWTUtil {
* @return token中包含的用户名
*/
public static String getUsername(String token) {
- try {
- DecodedJWT jwt = JWT.decode(token);
- return jwt.getClaim("username").asString();
- } catch (JWTDecodeException e) {
- return null;
- }
- }
-
- /**
- * 生成签名,5min后过期
- *
- * @param username 用户名
- * @param secret 用户的密码
- * @return 加密的token
- */
- public static String sign(String username, String secret) {
- try {
- Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
- Algorithm algorithm = Algorithm.HMAC256(secret);
- // 附带username信息
- return JWT.create()
- .withClaim("username", username)
- .withExpiresAt(date)
- .sign(algorithm);
- } catch (UnsupportedEncodingException e) {
- return null;
- }
- }
-
- /*----------------------------以下是socket校验--------------------------*/
- /**
- * 生成Socket Token签名, 5min后过期
- *
- * @param username 用户名
- * @param secret 用户的密码
- * @param appid applicationId
- * @param imei IMEI码
- * @return 加密的token
- */
- public static String signSocket(String username, String secret, String appid, String imei) {
- try {
- Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
- Algorithm algorithm = Algorithm.HMAC256(secret);
- // 附带username信息
- return JWT.create()
- .withClaim("username", username)
- .withClaim("appid", appid)
- .withClaim("imei", imei)
- .withExpiresAt(date)
- .sign(algorithm);
- } catch (UnsupportedEncodingException e) {
- return null;
- }
- }
-
- /**
- * 校验token是否正确
- *
- * @param token 密钥
- * @param secret 用户的密码
- * @return 是否正确
- */
- public static boolean verifySocket(String token, String secret) {
- try {
- DecodedJWT jwt1 = JWT.decode(token);
- String username = jwt1.getClaim("username").asString();
- String appid = jwt1.getClaim("appid").asString();
- String imei = jwt1.getClaim("imei").asString();
-
- Algorithm algorithm = Algorithm.HMAC256(secret);
- JWTVerifier verifier = JWT.require(algorithm)
- .withClaim("username", username)
- .withClaim("appid", appid)
- .withClaim("imei", imei)
- .build();
- DecodedJWT jwt = verifier.verify(token);
- return true;
- } catch (Exception exception) {
- log.error("校验token失败", exception);
- return false;
- }
- }
-
- /**
- * 获得token中的信息无需secret解密也能获得
- *
- * @return token中包含的用户名
- */
- public static String getSocketUsername(String token) {
- try {
- DecodedJWT jwt1 = JWT.decode(token);
- return jwt1.getClaim("username").asString();
- } catch (JWTDecodeException e) {
- return null;
- }
- }
-
- /**
- * 获得token中的信息无需secret解密也能获得
- *
- * @return token中包含的Appid
- */
- public static String getSocketAppid(String token) {
- try {
- DecodedJWT jwt1 = JWT.decode(token);
- return jwt1.getClaim("appid").asString();
- } catch (JWTDecodeException e) {
- return null;
- }
- }
-
- /**
- * 获得token中的信息无需secret解密也能获得
- *
- * @return token中包含的IMEI码
- */
- public static String getSocketImei(String token) {
- try {
- DecodedJWT jwt1 = JWT.decode(token);
- return jwt1.getClaim("imei").asString();
- } catch (JWTDecodeException e) {
- return null;
- }
+ DecodedJWT jwt = JWT.decode(token);
+ return jwt.getClaim("username").asString();
}
}
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Manager.java b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Manager.java
similarity index 98%
rename from springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Manager.java
rename to springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Manager.java
index 980ccf0..f1cc535 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Manager.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Manager.java
@@ -1,4 +1,4 @@
-package com.xncoding.jwt.common.dao.entity;
+package com.xncoding.jwt.dao.domain;
import java.io.Serializable;
import java.util.Date;
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/ManagerRole.java b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/ManagerRole.java
similarity index 98%
rename from springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/ManagerRole.java
rename to springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/ManagerRole.java
index 8ea4d4c..a8e1e9f 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/ManagerRole.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/ManagerRole.java
@@ -1,4 +1,4 @@
-package com.xncoding.jwt.common.dao.entity;
+package com.xncoding.jwt.dao.domain;
import java.io.Serializable;
import java.util.Date;
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Permission.java b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Permission.java
similarity index 98%
rename from springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Permission.java
rename to springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Permission.java
index 41c9af7..9349efa 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Permission.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Permission.java
@@ -1,4 +1,4 @@
-package com.xncoding.jwt.common.dao.entity;
+package com.xncoding.jwt.dao.domain;
import java.io.Serializable;
import java.util.Date;
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Role.java b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Role.java
similarity index 98%
rename from springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Role.java
rename to springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Role.java
index c9d9475..29fb5b5 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/Role.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/Role.java
@@ -1,4 +1,4 @@
-package com.xncoding.jwt.common.dao.entity;
+package com.xncoding.jwt.dao.domain;
import java.io.Serializable;
import java.util.Date;
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/RolePermission.java b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/RolePermission.java
similarity index 98%
rename from springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/RolePermission.java
rename to springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/RolePermission.java
index f246604..031dc29 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/common/dao/entity/RolePermission.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/dao/domain/RolePermission.java
@@ -1,4 +1,4 @@
-package com.xncoding.jwt.common.dao.entity;
+package com.xncoding.jwt.dao.domain;
import java.io.Serializable;
import java.util.Date;
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/dao/entity/ManagerInfo.java b/springboot-jwt/src/main/java/com/xncoding/jwt/model/ManagerInfo.java
similarity index 86%
rename from springboot-jwt/src/main/java/com/xncoding/jwt/dao/entity/ManagerInfo.java
rename to springboot-jwt/src/main/java/com/xncoding/jwt/model/ManagerInfo.java
index 74951d9..d7b79b8 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/dao/entity/ManagerInfo.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/model/ManagerInfo.java
@@ -1,11 +1,9 @@
-package com.xncoding.jwt.dao.entity;
+package com.xncoding.jwt.model;
-import com.xncoding.jwt.common.constant.DictMap;
-import com.xncoding.jwt.common.dao.entity.Manager;
+import com.xncoding.jwt.dao.domain.Manager;
import java.io.Serializable;
import java.util.List;
-import java.util.Map;
/**
* Description: 后台运维管理员信息
@@ -91,7 +89,4 @@ public class ManagerInfo extends Manager implements Serializable {
this.pnames = pnames;
}
- public void buildTable() {
- stateStr = DictMap.value(DictMap.KEY_USER_STATUS, getState());
- }
}
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/dao/entity/SysRole.java b/springboot-jwt/src/main/java/com/xncoding/jwt/model/SysRole.java
similarity index 78%
rename from springboot-jwt/src/main/java/com/xncoding/jwt/dao/entity/SysRole.java
rename to springboot-jwt/src/main/java/com/xncoding/jwt/model/SysRole.java
index a3205a6..4f6231f 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/dao/entity/SysRole.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/model/SysRole.java
@@ -1,7 +1,7 @@
-package com.xncoding.jwt.dao.entity;
+package com.xncoding.jwt.model;
-import com.xncoding.jwt.common.dao.entity.Permission;
-import com.xncoding.jwt.common.dao.entity.Role;
+import com.xncoding.jwt.dao.domain.Permission;
+import com.xncoding.jwt.dao.domain.Role;
import java.io.Serializable;
import java.util.List;
diff --git a/springboot-jwt/src/main/java/com/xncoding/jwt/service/ManagerInfoService.java b/springboot-jwt/src/main/java/com/xncoding/jwt/service/ManagerInfoService.java
index daa87d6..00e442f 100644
--- a/springboot-jwt/src/main/java/com/xncoding/jwt/service/ManagerInfoService.java
+++ b/springboot-jwt/src/main/java/com/xncoding/jwt/service/ManagerInfoService.java
@@ -1,7 +1,8 @@
package com.xncoding.jwt.service;
-import com.xncoding.jwt.dao.entity.ManagerInfo;
-import com.xncoding.jwt.dao.entity.SysRole;
+import com.xncoding.jwt.model.ManagerInfo;
+import com.xncoding.jwt.model.SysRole;
+import com.xncoding.jwt.shiro.ShiroKit;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@@ -18,20 +19,23 @@ public class ManagerInfoService {
/**
* 通过名称查找用户
+ * 这里我直接写常量,实际生产环境会通过DAO访问数据库
*
* @param username
* @return
*/
public ManagerInfo findByUsername(String username) {
ManagerInfo managerInfo = new ManagerInfo();
+ managerInfo.setUsername(username);
managerInfo.setPids("1,2,3");
managerInfo.setPidsList(Arrays.asList(1, 2, 3));
managerInfo.setPnames("第1个,第2个");
managerInfo.setState(1);
managerInfo.setCreatedTime(new Date());
managerInfo.setName("系统管理员");
- managerInfo.setPassword("4a496ba2a4172c71540fa643ddc8bb7c");
- managerInfo.setSalt("b4752b4b73034de06afb2db30fe19061");
+ // 随机数
+ managerInfo.setSalt("ef748186673033723bbf4e056f4ec92e");
+ managerInfo.setPassword("da9c3061ae5c0973a3d48e4e721cfbad");
List