作者 RuoYi

参数管理支持配置验证码开关

@@ -17,6 +17,7 @@ import com.ruoyi.common.core.domain.AjaxResult; @@ -17,6 +17,7 @@ import com.ruoyi.common.core.domain.AjaxResult;
17 import com.ruoyi.common.core.redis.RedisCache; 17 import com.ruoyi.common.core.redis.RedisCache;
18 import com.ruoyi.common.utils.sign.Base64; 18 import com.ruoyi.common.utils.sign.Base64;
19 import com.ruoyi.common.utils.uuid.IdUtils; 19 import com.ruoyi.common.utils.uuid.IdUtils;
  20 +import com.ruoyi.system.service.ISysConfigService;
20 21
21 /** 22 /**
22 * 验证码操作处理 23 * 验证码操作处理
@@ -38,13 +39,23 @@ public class CaptchaController @@ -38,13 +39,23 @@ public class CaptchaController
38 // 验证码类型 39 // 验证码类型
39 @Value("${ruoyi.captchaType}") 40 @Value("${ruoyi.captchaType}")
40 private String captchaType; 41 private String captchaType;
41 - 42 +
  43 + @Autowired
  44 + private ISysConfigService configService;
42 /** 45 /**
43 * 生成验证码 46 * 生成验证码
44 */ 47 */
45 @GetMapping("/captchaImage") 48 @GetMapping("/captchaImage")
46 public AjaxResult getCode(HttpServletResponse response) throws IOException 49 public AjaxResult getCode(HttpServletResponse response) throws IOException
47 { 50 {
  51 + AjaxResult ajax = AjaxResult.success();
  52 + boolean captchaOnOff = configService.selectCaptchaOnOff();
  53 + ajax.put("captchaOnOff", captchaOnOff);
  54 + if (!captchaOnOff)
  55 + {
  56 + return ajax;
  57 + }
  58 +
48 // 保存验证码信息 59 // 保存验证码信息
49 String uuid = IdUtils.simpleUUID(); 60 String uuid = IdUtils.simpleUUID();
50 String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; 61 String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
@@ -78,7 +89,6 @@ public class CaptchaController @@ -78,7 +89,6 @@ public class CaptchaController
78 return AjaxResult.error(e.getMessage()); 89 return AjaxResult.error(e.getMessage());
79 } 90 }
80 91
81 - AjaxResult ajax = AjaxResult.success();  
82 ajax.put("uuid", uuid); 92 ajax.put("uuid", uuid);
83 ajax.put("img", Base64.encode(os.toByteArray())); 93 ajax.put("img", Base64.encode(os.toByteArray()));
84 return ajax; 94 return ajax;
@@ -21,6 +21,7 @@ import com.ruoyi.common.utils.ServletUtils; @@ -21,6 +21,7 @@ import com.ruoyi.common.utils.ServletUtils;
21 import com.ruoyi.common.utils.ip.IpUtils; 21 import com.ruoyi.common.utils.ip.IpUtils;
22 import com.ruoyi.framework.manager.AsyncManager; 22 import com.ruoyi.framework.manager.AsyncManager;
23 import com.ruoyi.framework.manager.factory.AsyncFactory; 23 import com.ruoyi.framework.manager.factory.AsyncFactory;
  24 +import com.ruoyi.system.service.ISysConfigService;
24 import com.ruoyi.system.service.ISysUserService; 25 import com.ruoyi.system.service.ISysUserService;
25 26
26 /** 27 /**
@@ -43,6 +44,9 @@ public class SysLoginService @@ -43,6 +44,9 @@ public class SysLoginService
43 @Autowired 44 @Autowired
44 private ISysUserService userService; 45 private ISysUserService userService;
45 46
  47 + @Autowired
  48 + private ISysConfigService configService;
  49 +
46 /** 50 /**
47 * 登录验证 51 * 登录验证
48 * 52 *
@@ -54,18 +58,11 @@ public class SysLoginService @@ -54,18 +58,11 @@ public class SysLoginService
54 */ 58 */
55 public String login(String username, String password, String code, String uuid) 59 public String login(String username, String password, String code, String uuid)
56 { 60 {
57 - String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;  
58 - String captcha = redisCache.getCacheObject(verifyKey);  
59 - redisCache.deleteObject(verifyKey);  
60 - if (captcha == null)  
61 - {  
62 - AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));  
63 - throw new CaptchaExpireException();  
64 - }  
65 - if (!code.equalsIgnoreCase(captcha)) 61 + boolean captchaOnOff = configService.selectCaptchaOnOff();
  62 + // 验证码开关
  63 + if (captchaOnOff)
66 { 64 {
67 - AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));  
68 - throw new CaptchaException(); 65 + validateCapcha(username, code, uuid);
69 } 66 }
70 // 用户验证 67 // 用户验证
71 Authentication authentication = null; 68 Authentication authentication = null;
@@ -96,6 +93,31 @@ public class SysLoginService @@ -96,6 +93,31 @@ public class SysLoginService
96 } 93 }
97 94
98 /** 95 /**
  96 + * 校验验证码
  97 + *
  98 + * @param username 用户名
  99 + * @param code 验证码
  100 + * @param uuid 唯一标识
  101 + * @return 结果
  102 + */
  103 + public void validateCapcha(String username, String code, String uuid)
  104 + {
  105 + String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  106 + String captcha = redisCache.getCacheObject(verifyKey);
  107 + redisCache.deleteObject(verifyKey);
  108 + if (captcha == null)
  109 + {
  110 + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
  111 + throw new CaptchaExpireException();
  112 + }
  113 + if (!code.equalsIgnoreCase(captcha))
  114 + {
  115 + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
  116 + throw new CaptchaException();
  117 + }
  118 + }
  119 +
  120 + /**
99 * 记录登录信息 121 * 记录登录信息
100 */ 122 */
101 public void recordLoginInfo(SysUser user) 123 public void recordLoginInfo(SysUser user)
@@ -27,6 +27,13 @@ public interface ISysConfigService @@ -27,6 +27,13 @@ public interface ISysConfigService
27 public String selectConfigByKey(String configKey); 27 public String selectConfigByKey(String configKey);
28 28
29 /** 29 /**
  30 + * 获取验证码开关
  31 + *
  32 + * @return true开启,false关闭
  33 + */
  34 + public boolean selectCaptchaOnOff();
  35 +
  36 + /**
30 * 查询参数配置列表 37 * 查询参数配置列表
31 * 38 *
32 * @param config 参数配置信息 39 * @param config 参数配置信息
@@ -81,6 +81,21 @@ public class SysConfigServiceImpl implements ISysConfigService @@ -81,6 +81,21 @@ public class SysConfigServiceImpl implements ISysConfigService
81 } 81 }
82 82
83 /** 83 /**
  84 + * 获取验证码开关
  85 + *
  86 + * @return true开启,false关闭
  87 + */
  88 + public boolean selectCaptchaOnOff()
  89 + {
  90 + String captchaOnOff = selectConfigByKey("sys.account.captchaOnOff");
  91 + if (StringUtils.isEmpty(captchaOnOff))
  92 + {
  93 + return true;
  94 + }
  95 + return Convert.toBool(captchaOnOff);
  96 + }
  97 +
  98 + /**
84 * 查询参数配置列表 99 * 查询参数配置列表
85 * 100 *
86 * @param config 参数配置信息 101 * @param config 参数配置信息
@@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
18 <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> 18 <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
19 </el-input> 19 </el-input>
20 </el-form-item> 20 </el-form-item>
21 - <el-form-item prop="code"> 21 + <el-form-item prop="code" v-if="captchaOnOff">
22 <el-input 22 <el-input
23 v-model="loginForm.code" 23 v-model="loginForm.code"
24 auto-complete="off" 24 auto-complete="off"
@@ -81,6 +81,7 @@ export default { @@ -81,6 +81,7 @@ export default {
81 code: [{ required: true, trigger: "change", message: "验证码不能为空" }] 81 code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
82 }, 82 },
83 loading: false, 83 loading: false,
  84 + captchaOnOff: true,
84 redirect: undefined 85 redirect: undefined
85 }; 86 };
86 }, 87 },
@@ -99,8 +100,11 @@ export default { @@ -99,8 +100,11 @@ export default {
99 methods: { 100 methods: {
100 getCode() { 101 getCode() {
101 getCodeImg().then(res => { 102 getCodeImg().then(res => {
102 - this.codeUrl = "data:image/gif;base64," + res.img;  
103 - this.loginForm.uuid = res.uuid; 103 + this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
  104 + if (this.captchaOnOff) {
  105 + this.codeUrl = "data:image/gif;base64," + res.img;
  106 + this.loginForm.uuid = res.uuid;
  107 + }
104 }); 108 });
105 }, 109 },
106 getCookie() { 110 getCookie() {
@@ -130,7 +134,9 @@ export default { @@ -130,7 +134,9 @@ export default {
130 this.$router.push({ path: this.redirect || "/" }).catch(()=>{}); 134 this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
131 }).catch(() => { 135 }).catch(() => {
132 this.loading = false; 136 this.loading = false;
133 - this.getCode(); 137 + if (this.captchaOnOff) {
  138 + this.getCode();
  139 + }
134 }); 140 });
135 } 141 }
136 }); 142 });