浏览代码

4/10 登录修改

lwhhszx 1 年之前
父节点
当前提交
e72bf767d2

+ 81 - 0
src/main/java/com/example/xiaoshiweixinback/checkLogin/CheckLoginAop.java

@@ -0,0 +1,81 @@
+package com.example.xiaoshiweixinback.checkLogin;
+
+
+import cn.dev33.satoken.stp.StpUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.example.xiaoshiweixinback.business.common.Response;
+import com.example.xiaoshiweixinback.business.utils.CacheUtil;
+import com.example.xiaoshiweixinback.business.utils.LoginUtils;
+import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
+import io.swagger.v3.oas.annotations.Operation;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.*;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import java.lang.reflect.Method;
+import java.util.List;
+
+@Order(2)
+@Aspect
+@Component
+public class CheckLoginAop {
+    @Autowired
+    private CacheUtil cacheUtil;
+
+    /**
+     * 定义切点
+     */
+    @Pointcut("@annotation(com.example.xiaoshiweixinback.checkLogin.checkLogin)")
+    public void annotationPointcut() {
+
+    }
+
+    @Before("annotationPointcut()")
+    public void beforePointcut(JoinPoint joinPoint) {
+        //此处进入到方法前  可以实现一些业务逻辑
+        //获取目标对象方法参数
+
+    }
+
+    /**
+     * @param joinPoint 当前执行的方法
+     */
+    @Around("annotationPointcut()")
+    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
+        // 是否通过切面过滤标记
+        Boolean isPass = true;
+        try { cacheUtil.getLoginUser(LoginUtils.getToken());
+        }
+    catch (Exception e){
+            return Response.unLogin("未登录");
+    }
+        //判断不通过
+        if (!isPass) {
+            return Response.unLogin("未登录");
+        }
+
+        //判断通过
+        return joinPoint.proceed();
+    }
+
+    /**
+     * 在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)
+     *
+     * @param joinPoint 切点
+     */
+    @AfterReturning("annotationPointcut()")
+    public void doAfterReturning(JoinPoint joinPoint) {
+    }
+
+    private void checkToken(String token) {
+    }
+}
+

+ 17 - 0
src/main/java/com/example/xiaoshiweixinback/checkLogin/checkLogin.java

@@ -0,0 +1,17 @@
+package com.example.xiaoshiweixinback.checkLogin;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * @author LRJ
+ * @date 2022-8-23
+ * @description 数据权限(增删改)条件判断注解
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface checkLogin {
+
+}
+

+ 6 - 3
src/main/java/com/example/xiaoshiweixinback/controller/ProductCategoryController.java

@@ -4,6 +4,7 @@ package com.example.xiaoshiweixinback.controller;
 import com.example.xiaoshiweixinback.business.common.Constants;
 import com.example.xiaoshiweixinback.business.common.Response;
 import com.example.xiaoshiweixinback.business.common.base.Records;
+import com.example.xiaoshiweixinback.checkLogin.checkLogin;
 import com.example.xiaoshiweixinback.domain.AssoCategoryFile;
 import com.example.xiaoshiweixinback.entity.dto.ConcernCategoryDTO;
 import com.example.xiaoshiweixinback.entity.dto.ProductCategoryDTO;
@@ -31,22 +32,24 @@ public class ProductCategoryController {
 
     @Operation(summary = "查询产品类别")
     @PostMapping("/queryCategory")
-    private Response queryProductCategory(@RequestBody ProductCategoryDTO productCategoryDTO) {
+    public Response queryProductCategory(@RequestBody ProductCategoryDTO productCategoryDTO) {
         Records records = productCategoryService.queryProductCategory(productCategoryDTO);
         return Response.success(records);
     }
 
     @Operation(summary = "查询关注的产品类别")
     @PostMapping("/queryConcernedCategory")
-    private Response queryConcernedCategory(@RequestBody ProductCategoryDTO productCategoryDTO) {
+    public Response queryConcernedCategory(@RequestBody ProductCategoryDTO productCategoryDTO) {
         productCategoryDTO.setIfConcern(true);
         Records records = productCategoryService.queryProductCategory(productCategoryDTO);
         return Response.success(records);
     }
 
+
+    @checkLogin
     @Operation(summary = "关注产品类别")
     @PostMapping("/concernCategory")
-    private Response concernCategory(@RequestBody ConcernCategoryDTO concernCategoryDTO) {
+    public Response concernCategory(@RequestBody ConcernCategoryDTO concernCategoryDTO) {
         assoPersonCategoryService.concernCategory(concernCategoryDTO);
         Records records = new Records();
         records.setData("关注成功");