Jelajahi Sumber

FIX 任务,算法,温度 完成

tjf 1 hari lalu
induk
melakukan
8c75b4b358

+ 76 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/manage/ManageCommonController.java

@@ -0,0 +1,76 @@
+package com.ruoyi.web.controller.manage;
+
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.system.service.ISysConfigService;
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.concurrent.TimeUnit;
+
+import static com.ruoyi.common.constant.Constants.*;
+
+/**
+ * @Author: tjf
+ * @Date: 2025/8/14 星期四 14:05
+ * @Describe:
+ */
+@RestController
+@RequestMapping("/manage/common")
+public class ManageCommonController {
+    @Autowired
+    private RedisCache redisCache;
+
+    @Autowired
+    private ISysConfigService sysConfigService;
+
+    /**
+     * 生产6位数动态码
+     */
+    @GetMapping("/getDynamicPassword")
+    @PreAuthorize("@ss.hasPermi('manage:common:getDynamicPassword')")
+    public AjaxResult warnManageIndex() {
+        String code = getCode(6);
+        redisCache.setCacheObject(DYNAMIC_PASSWORD + code, code, CAPTCHA_EXPIRATION_FIVE, TimeUnit.MINUTES);
+        return AjaxResult.success("操作成功", code);
+    }
+
+    /**
+     * 验证6位数动态码
+     */
+    @GetMapping("/checkDynamicPassword/{code}")
+    public AjaxResult warnManageIndex(@PathVariable("code") String code) {
+        Object codeRedis = redisCache.getCacheObject(DYNAMIC_PASSWORD + code);
+        if (ObjectUtils.isNotEmpty(codeRedis)) {
+            if (code.equals(codeRedis)) {
+                return AjaxResult.success();
+            }
+        } else {
+            //匹配固定密码
+            String passWord = sysConfigService.selectConfigByKey(SYS_DYNAMIC_PASSWORD);
+            if (StringUtils.isNotEmpty(passWord)) {
+                if (StringUtils.equals(passWord, code)) {
+                    return AjaxResult.success();
+                }
+            }
+        }
+        return AjaxResult.error();
+    }
+
+    //生成X位验证码
+    public static String getCode(Integer num) {
+        String[] codes = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9"};
+        StringBuilder code = new StringBuilder();
+        for (int i = 0; i < num; i++) {
+            int j = (int) (Math.random() * 10);
+            code.append(codes[j]);
+        }
+        return code.toString();
+    }
+}

+ 5 - 0
ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java

@@ -94,6 +94,7 @@ public class Constants {
      * 验证码有效期(分钟)
      */
     public static final Integer CAPTCHA_EXPIRATION = 2;
+    public static final Integer CAPTCHA_EXPIRATION_FIVE = 5;
 
     /**
      * 令牌
@@ -202,4 +203,8 @@ public class Constants {
     public static final String TO_SERVER_PIC_POS= "toServer_picPos";
     //首页统计折线图往期年份数据
     public static final String WARN_MANAGE_LAST_YEAR = "warn_manage_last_year:";
+    //固定系统动态密码
+    public static final String SYS_DYNAMIC_PASSWORD = "sys.dynamic.password";
+    //动态验证码
+    public static final String DYNAMIC_PASSWORD = "dynamic_password:";
 }

+ 1 - 1
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java

@@ -114,7 +114,7 @@ public class SecurityConfig
                 requests.antMatchers("/login", "/register", "/captchaImage", "/system/holidays/task").permitAll()
                     // 静态资源,可匿名访问
                     .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
-                    .antMatchers("/webjars/**",  "/druid/**").permitAll()
+                    .antMatchers("/webjars/**",  "/druid/**","/manage/common/checkDynamicPassword/**").permitAll()
                     // 除上面外的所有请求全部需要鉴权认证
                     .anyRequest().authenticated();
             })