diff --git a/ZhuQue/pom.xml b/ZhuQue/pom.xml index 79fd38e..b5cfcd4 100644 --- a/ZhuQue/pom.xml +++ b/ZhuQue/pom.xml @@ -59,6 +59,11 @@ jjwt 0.7.0 + + javax.xml.bind + jaxb-api + 2.3.1 + net.sf.json-lib diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/IsLoginInterceptor.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/IsLoginInterceptor.java index b5dd14e..c6caef9 100644 --- a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/IsLoginInterceptor.java +++ b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/IsLoginInterceptor.java @@ -1,8 +1,6 @@ package com.dsideal.ZhuQue.Interceptor; -import com.alibaba.fastjson.JSONObject; import com.dsideal.ZhuQue.Util.CommonUtil; -import com.dsideal.ZhuQue.Util.SessionKit; import com.jfinal.aop.Interceptor; import com.jfinal.aop.Invocation; import com.jfinal.core.Controller; @@ -53,12 +51,6 @@ public class IsLoginInterceptor implements Interceptor { Claims cs = JwtUtil.getClaimsFromToken(token, secret); if (cs == null || cs.isEmpty()) { isTrue = false; - } else { - //{date=Tue Aug 06 13:37:05 CST 2024, phone=13756511990, type=WX, userId=94312} - String phone = cs.get("phone").toString(); - String userId = cs.get("userId").toString(); - SessionKit.set(request, con.getResponse(), "userId", userId); - SessionKit.set(request, con.getResponse(), "phone", phone); } } if (isTrue) { diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/IsSysAdminInterceptor.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/IsSysAdminInterceptor.java deleted file mode 100644 index 364b5b7..0000000 --- a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/IsSysAdminInterceptor.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.dsideal.ZhuQue.Interceptor; - -import com.dsideal.ZhuQue.Util.CommonUtil; -import com.dsideal.ZhuQue.Util.SessionKit; -import com.jfinal.aop.Interceptor; -import com.jfinal.aop.Invocation; -import com.jfinal.core.Controller; - -/** - * 需要是系统管理员身份校验 - * - * @author Administrator - */ - -public class IsSysAdminInterceptor implements Interceptor { - @Override - public void intercept(Invocation inv) { - IsSysAdminInterface annotation = inv.getMethod().getAnnotation(IsSysAdminInterface.class); - if (annotation != null) { - checkSysAdmin(annotation, inv); - } else { - inv.invoke(); - } - } - - public void checkSysAdmin(IsSysAdminInterface annotation, Invocation inv) { - Controller con = inv.getController(); - String header = con.getHeader("Content-Type"); //取出head头 - if (header != null && header.indexOf("multipart/form-data") != -1) { //判断是否是form-data - inv.invoke(); - } - boolean isTrue = false; - String[] value = annotation.value(); - if (SessionKit.get(con.getRequest(), con.getResponse(), "identity_id") != null) { - for (String v : value) { - if (SessionKit.get(con.getRequest(), con.getResponse(), "identity_id").equals(v)) { - isTrue = true; - break; - } - } - } - if (isTrue) { - inv.invoke(); - } else { - con.renderJson(CommonUtil.returnMessageJson(false, "您的身份有误,未被识别为管理员或管理员权限不足,无法进行操作!")); - } - } -} \ No newline at end of file diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/LayUiPageInfoInterceptor.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/LayUiPageInfoInterceptor.java deleted file mode 100644 index 79a1f85..0000000 --- a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/LayUiPageInfoInterceptor.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.dsideal.ZhuQue.Interceptor; - -import com.dsideal.ZhuQue.Util.CommonUtil; -import com.jfinal.aop.Interceptor; -import com.jfinal.aop.Invocation; -import com.jfinal.core.Controller; - -/** - * 检查是不是符合layui的分页查询规则 - * - * @author Administrator - */ -public class LayUiPageInfoInterceptor implements Interceptor { - @Override - public void intercept(Invocation inv) { - LayUiPageInfoInterface annotation = inv.getMethod().getAnnotation(LayUiPageInfoInterface.class); - if (annotation != null) { - checkLayUiPageInfo(annotation, inv); - } else { - inv.invoke(); - } - } - - public void checkLayUiPageInfo(LayUiPageInfoInterface annotation, Invocation inv) { - Controller con = inv.getController(); - String header = con.getHeader("Content-Type"); //取出head头 - if (header != null && header.indexOf("multipart/form-data") != -1) { //判断是否是form-data - inv.invoke(); - } - String[] value = annotation.value(); - - String o_page = null; - String o_limit = null; - - for (String v : value) { - String parameter = con.getPara(v); - if (v.equals("page")) { - o_page = parameter; - } - if (v.equals("limit")) { - o_limit = parameter; - } - } - if (o_page != null && o_limit != null) { - if (!CommonUtil.isNumeric(o_page)) { - con.renderJson(CommonUtil.returnMessageJson(false, "传入的page参数不是数字!")); - } - if (!CommonUtil.isNumeric(o_limit)) { - con.renderJson(CommonUtil.returnMessageJson(false, "传入的limit参数不是数字!")); - } - //检查大小 - int page = Integer.parseInt(o_page); - int limit = Integer.parseInt(o_limit); - - if (limit > 100) { - con.renderJson(CommonUtil.returnMessageJson(false, "传入的limit参数大于100,被禁止!")); - } - if (limit < 1) { - con.renderJson(CommonUtil.returnMessageJson(false, "传入的limit参数小于1,被禁止!")); - } - //放行 - inv.invoke(); - } else if (o_page == null) { - con.renderJson(CommonUtil.returnMessageJson(false, "传入的page参数为空!")); - } else if (o_limit == null) { - con.renderJson(CommonUtil.returnMessageJson(false, "传入的limit参数为空!")); - } - } -} \ No newline at end of file diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/LayUiPageInfoInterface.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/LayUiPageInfoInterface.java deleted file mode 100644 index e66768c..0000000 --- a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/LayUiPageInfoInterface.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.dsideal.ZhuQue.Interceptor; - -import java.lang.annotation.*; - -@Inherited -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) -public @interface LayUiPageInfoInterface { - String[] value(); -} \ No newline at end of file diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/OnlinePersonCountInterceptor.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/OnlinePersonCountInterceptor.java deleted file mode 100644 index fa2f043..0000000 --- a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/OnlinePersonCountInterceptor.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.dsideal.ZhuQue.Interceptor; - -import com.dsideal.ZhuQue.Util.SessionKit; -import com.jfinal.aop.Interceptor; -import com.jfinal.aop.Invocation; -import com.jfinal.kit.PropKit; - -/** - * @author Administrator - */ - -public class OnlinePersonCountInterceptor implements Interceptor { - - @Override - public void intercept(Invocation inv) { - var req = inv.getController().getRequest(); - var res = inv.getController().getResponse(); - if (inv.getController().getRequest().getRequestURL().indexOf("/loginPerson/showOnline") >= 0) { - inv.invoke(); - return; - } - //根据JSessionId判断是不是已登录 - String sessionKey = PropKit.get("sessionKey"); - boolean flag = false; - for (var c : sessionKey.split(",")) { - if (SessionKit.get(req, res, c) != null) { - flag = true; - break; - } - } - String action_set_login = PropKit.get("action_set_login"); - String action_set_nologin = PropKit.get("action_set_nologin"); - if (flag) {//已登录 - OnlinePersonCountUtil.addSet(action_set_login, req, res); - } else {//未登录 - OnlinePersonCountUtil.addSet(action_set_nologin, req, res); - } - inv.invoke(); - } -} \ No newline at end of file diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/OnlinePersonCountUtil.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/OnlinePersonCountUtil.java deleted file mode 100644 index 7d1dde8..0000000 --- a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Interceptor/OnlinePersonCountUtil.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.dsideal.ZhuQue.Interceptor; - -import com.dsideal.ZhuQue.Util.RedisKit; -import com.dsideal.ZhuQue.Util.SessionKit; -import com.jfinal.kit.PropKit; -import redis.clients.jedis.resps.Tuple; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.util.ArrayList; -import java.util.List; - -public class OnlinePersonCountUtil { - public static void clearTimeOut() { - List list = new ArrayList<>(); - list.add(PropKit.get("action_set_login")); - list.add(PropKit.get("action_set_nologin")); - - for (int i = 0; i < list.size(); i++) { - String scoreSet = list.get(i); - long seconds = System.currentTimeMillis() / 1000; //获取当前时间戳(秒) - List members = RedisKit.zrangeByScoreWithScores(scoreSet, "-inf", String.valueOf(seconds)); - for (Tuple member : members) { - long score = (long) member.getScore(); - String memberValue = member.getElement(); - if (score < seconds - PropKit.getInt("action_dead_seconds")) {//600秒以前的算过期,清理掉 - RedisKit.zrem(scoreSet, memberValue); // 删除过期成员 - } - } - } - } - - public static void addSet(String setName, HttpServletRequest req, HttpServletResponse res) { - clearTimeOut(); - String jSessionId = SessionKit.getCookieSessionId(req, res); - long seconds = System.currentTimeMillis() / 1000; - RedisKit.zadd(setName, seconds, jSessionId); - } - - public static void removeSet(String setName, HttpServletRequest req, HttpServletResponse res) { - clearTimeOut(); - String jSessionId = SessionKit.getCookieSessionId(req, res); - RedisKit.zrem(setName, jSessionId); - } -} diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Util/SessionKit.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Util/SessionKit.java deleted file mode 100644 index 6cc3cf7..0000000 --- a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Util/SessionKit.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.dsideal.ZhuQue.Util; - -import com.dsideal.ZhuQue.Interceptor.OnlinePersonCountUtil; -import com.jfinal.kit.PropKit; - -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.util.UUID; - -public class SessionKit { - public static String JSessionId = "sessionId";//Cookie中sessionId的名称 - public static long TimeoutSecond = 60 * 60 * 24 * 7;//一周 - - /** - * 功能:获取Cookie上的jSessionId - * - * @param request - * @return - */ - public static String getCookieSessionId(HttpServletRequest request, HttpServletResponse response) { - String jSessionId = null; - //客户端请求服务器时 从请求对象中获取所有的cookie - Cookie[] cookies = request.getCookies(); - if (cookies != null) { - //遍历cookie集合 根据名字获取对应的value - for (Cookie cookie : cookies) { - //判断是否为指定cookie - if (JSessionId.equals(cookie.getName())) { - //获取对应的值 - jSessionId = cookie.getValue(); - break; - } - } - } - if (cookies == null || jSessionId == null) { - // 创建cookie对象 - jSessionId = UUID.randomUUID().toString().toLowerCase(); - Cookie cookie = new Cookie(JSessionId, jSessionId); - cookie.setPath("/"); - // 设置cookie存活时间 - response.addCookie(cookie); - } - return JSessionId + "_" + jSessionId; - } - - public static void clear(HttpServletRequest request, HttpServletResponse response) { - String jSessionId = getCookieSessionId(request, response); - //写入jSessionId的key域值 - RedisKit.Del(jSessionId); - Cookie[] cookies = request.getCookies(); - if (cookies != null) { - for (Cookie cookie : cookies) { - cookie.setMaxAge(0); // 将Cookie的过期时间设为0,表示立即过期 - response.addCookie(cookie); // 发送修改后的Cookie回客户端 - } - } - //三连击 - OnlinePersonCountUtil.clearTimeOut(); - OnlinePersonCountUtil.removeSet(PropKit.get("action_set_login"), request, response); - OnlinePersonCountUtil.removeSet(PropKit.get("action_set_nologin"), request, response); - } - - /** - * 功能:手工实现的Redis模拟Session写入 - * - * @param request - * @param key - * @param value - * @return - */ - public static void set(HttpServletRequest request, HttpServletResponse response, String key, String value) { - String jSessionId = getCookieSessionId(request, response); - //写入jSessionId的key域值 - RedisKit.HSet(jSessionId, key, value); - //过期时长为TimeoutSecond - RedisKit.Expire(jSessionId, TimeoutSecond); - } - - /** - * 功能:获取Session内容 - * - * @param request - * @param key - * @return - */ - public static String get(HttpServletRequest request, HttpServletResponse response, String key) { - String jSessionId = getCookieSessionId(request, response); - if (jSessionId == null) return null; - String value = RedisKit.HGet(jSessionId, key); - //过期时长为TimeoutSecond - RedisKit.Expire(jSessionId, TimeoutSecond); - return value; - } -} diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Ylt/Controller/YltController.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Ylt/Controller/YltController.java index e475e89..c8532c7 100644 --- a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Ylt/Controller/YltController.java +++ b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Ylt/Controller/YltController.java @@ -2,11 +2,11 @@ package com.dsideal.ZhuQue.Ylt.Controller; import com.dsideal.ZhuQue.Interceptor.IsLoginInterface; import com.dsideal.ZhuQue.Util.CommonUtil; -import com.dsideal.ZhuQue.Util.SessionKit; +import com.dsideal.ZhuQue.Ylt.Model.YltModel; import com.jfinal.aop.Before; import com.jfinal.core.Controller; import com.jfinal.ext.interceptor.GET; -import com.jfinal.kit.Kv; +import com.jfinal.ext.interceptor.POST; import com.jfinal.plugin.activerecord.Record; import java.util.ArrayList; @@ -14,6 +14,7 @@ import java.util.List; public class YltController extends Controller { + YltModel ym = new YltModel(); public List getAll() { List list = new ArrayList<>(); @@ -61,8 +62,10 @@ public class YltController extends Controller { @Before({GET.class}) @IsLoginInterface({}) public void getListWithAuth() { - String userId = SessionKit.get(getRequest(), getResponse(), "userId"); - String phone = SessionKit.get(getRequest(), getResponse(), "phone"); + //获取当前登录人员信息 + Record user = ym.getUser(getRequest()); + String userId = user.get("userId"); + String phone = user.get("phone"); System.out.println(userId); System.out.println(phone); @@ -70,4 +73,28 @@ public class YltController extends Controller { List list = getAll(); renderJson(CommonUtil.getRet(list, true, "获取成功!")); } + + /** + * 功能:测试保存数据 + * + * @param xm + * @param xb + * @param yw_id + */ + @Before({POST.class}) + @IsLoginInterface({}) + public void testPostWithAuth(String xm, String xb, int yw_id) { + //获取当前登录人员信息 + Record user = ym.getUser(getRequest()); + String userId = user.get("userId"); + String phone = user.get("phone"); + + Record record = new Record(); + record.set("xm", xm); + record.set("xb", xb); + record.set("yw_id", yw_id); + record.set("userId", userId); + record.set("phone", phone); + renderJson(CommonUtil.getRet(record, true, "保存成功!")); + } } \ No newline at end of file diff --git a/ZhuQue/src/main/java/com/dsideal/ZhuQue/Ylt/Model/YltModel.java b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Ylt/Model/YltModel.java new file mode 100644 index 0000000..9c777fa --- /dev/null +++ b/ZhuQue/src/main/java/com/dsideal/ZhuQue/Ylt/Model/YltModel.java @@ -0,0 +1,30 @@ +package com.dsideal.ZhuQue.Ylt.Model; + +import com.dsideal.ZhuQue.Util.JwtUtil; +import io.jsonwebtoken.Claims; + +import javax.servlet.http.HttpServletRequest; + +import com.jfinal.plugin.activerecord.Record; + +public class YltModel { + /** + * 功能:根据JWT的token获取当前用户信息 + * + * @param request + * @return + */ + public Record getUser(HttpServletRequest request) { + //从Http请求头中获取Authorization + String Authorization = request.getHeader("Authorization"); + String secret = JwtUtil.SECRET; + String token = Authorization.replaceFirst(JwtUtil.AUTHORIZATION_STARTER, ""); + Claims cs = JwtUtil.getClaimsFromToken(token, secret); + String userId = cs.get("userId", String.class); + String phone = cs.get("phone", String.class); + Record record = new Record(); + record.set("userId", userId); + record.set("phone", phone); + return record; + } +}