新增加验证码开关

This commit is contained in:
不做码农 2021-12-01 16:56:46 +08:00
parent 5e65941e15
commit e5bbf0b298
8 changed files with 42 additions and 7 deletions

View File

@ -17,6 +17,7 @@ using ZR.Common;
using ZR.Service.System; using ZR.Service.System;
using ZR.Model.System; using ZR.Model.System;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using Microsoft.AspNetCore.Authorization;
namespace ZR.Admin.WebApi.Controllers namespace ZR.Admin.WebApi.Controllers
{ {
@ -83,6 +84,7 @@ namespace ZR.Admin.WebApi.Controllers
/// <param name="configKey"></param> /// <param name="configKey"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet("configKey/{configKey}")] [HttpGet("configKey/{configKey}")]
[AllowAnonymous]
public IActionResult GetConfigKey(string configKey) public IActionResult GetConfigKey(string configKey)
{ {
//TODO 增加缓存 //TODO 增加缓存

View File

@ -15,6 +15,7 @@ using ZR.Model.System.Dto;
using ZR.Service.System.IService; using ZR.Service.System.IService;
using Hei.Captcha; using Hei.Captcha;
using ZR.Common; using ZR.Common;
using ZR.Service.System;
namespace ZR.Admin.WebApi.Controllers.System namespace ZR.Admin.WebApi.Controllers.System
{ {
@ -30,6 +31,7 @@ namespace ZR.Admin.WebApi.Controllers.System
private readonly ISysLoginService sysLoginService; private readonly ISysLoginService sysLoginService;
private readonly ISysPermissionService permissionService; private readonly ISysPermissionService permissionService;
private readonly SecurityCodeHelper SecurityCodeHelper; private readonly SecurityCodeHelper SecurityCodeHelper;
private readonly ISysConfigService sysConfigService;
public SysLoginController( public SysLoginController(
IHttpContextAccessor contextAccessor, IHttpContextAccessor contextAccessor,
@ -37,6 +39,7 @@ namespace ZR.Admin.WebApi.Controllers.System
ISysUserService sysUserService, ISysUserService sysUserService,
ISysLoginService sysLoginService, ISysLoginService sysLoginService,
ISysPermissionService permissionService, ISysPermissionService permissionService,
ISysConfigService configService,
SecurityCodeHelper captcha) SecurityCodeHelper captcha)
{ {
httpContextAccessor = contextAccessor; httpContextAccessor = contextAccessor;
@ -45,6 +48,7 @@ namespace ZR.Admin.WebApi.Controllers.System
this.sysUserService = sysUserService; this.sysUserService = sysUserService;
this.sysLoginService = sysLoginService; this.sysLoginService = sysLoginService;
this.permissionService = permissionService; this.permissionService = permissionService;
this.sysConfigService = configService;
} }
@ -60,7 +64,8 @@ namespace ZR.Admin.WebApi.Controllers.System
{ {
if (loginBody == null) { throw new CustomException("请求参数错误"); } if (loginBody == null) { throw new CustomException("请求参数错误"); }
loginBody.LoginIP = HttpContextExtension.GetClientUserIp(HttpContext); loginBody.LoginIP = HttpContextExtension.GetClientUserIp(HttpContext);
if (CacheHelper.Get(loginBody.Uuid) is string str && !str.ToLower().Equals(loginBody.Code.ToLower())) SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(loginBody.Uuid) is string str && !str.ToLower().Equals(loginBody.Code.ToLower()))
{ {
throw new CustomException(ResultCode.CAPTCHA_ERROR, "验证码错误"); throw new CustomException(ResultCode.CAPTCHA_ERROR, "验证码错误");
} }
@ -136,8 +141,26 @@ namespace ZR.Admin.WebApi.Controllers.System
public ApiResult CaptchaImage() public ApiResult CaptchaImage()
{ {
string uuid = Guid.NewGuid().ToString().Replace("-", ""); string uuid = Guid.NewGuid().ToString().Replace("-", "");
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
var code = SecurityCodeHelper.GetRandomEnDigitalText(4); var code = SecurityCodeHelper.GetRandomEnDigitalText(4);
var imgByte = SecurityCodeHelper.GetEnDigitalCodeByte(code); byte[] imgByte;
if (sysConfig.ConfigValue == "1")
{
imgByte = SecurityCodeHelper.GetGifEnDigitalCodeByte(code);//动态gif数字字母
}
else if (sysConfig.ConfigValue == "2")
{
imgByte = SecurityCodeHelper.GetGifBubbleCodeByte(code);//动态gif泡泡
}
else if (sysConfig.ConfigValue == "3")
{
imgByte = SecurityCodeHelper.GetBubbleCodeByte(code);//泡泡
}
else
{
imgByte = SecurityCodeHelper.GetEnDigitalCodeByte(code);//英文字母加数字
}
string base64Str = Convert.ToBase64String(imgByte); string base64Str = Convert.ToBase64String(imgByte);
CacheHelper.SetCache(uuid, code); CacheHelper.SetCache(uuid, code);
var obj = new { uuid, img = base64Str };// File(stream, "image/png") var obj = new { uuid, img = base64Str };// File(stream, "image/png")

View File

@ -12,5 +12,6 @@ namespace ZR.Service.System
/// </summary> /// </summary>
public interface ISysConfigService: IBaseService<SysConfig> public interface ISysConfigService: IBaseService<SysConfig>
{ {
SysConfig GetSysConfigByKey(string key);
} }
} }

View File

@ -18,6 +18,11 @@ namespace ZR.Service.System
#region #region
public SysConfig GetSysConfigByKey(string key)
{
return _SysConfigrepository.Queryable().First(f => f.ConfigKey == key);
}
#endregion #endregion
} }
} }

View File

@ -54,7 +54,7 @@ service.interceptors.response.use(res => {
} }
).then(() => { ).then(() => {
store.dispatch('LogOut').then(() => { store.dispatch('LogOut').then(() => {
location.href = process.env.VUE_APP_ROUTER_PREFIX + '/index'; location.href = '/index';
}) })
}) })

View File

@ -12,7 +12,7 @@
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
</el-input> </el-input>
</el-form-item> </el-form-item>
<el-form-item prop="code"> <el-form-item prop="code" v-if="showCaptcha != 'off'">
<el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin" ref="codeTxt"> <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin" ref="codeTxt">
<svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
</el-input> </el-input>
@ -38,7 +38,7 @@
<script> <script>
import { getCodeImg } from "@/api/system/login"; import { getCodeImg } from "@/api/system/login";
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt"; // import { encrypt, decrypt } from "@/utils/jsencrypt";
import defaultSettings from "@/settings"; import defaultSettings from "@/settings";
export default { export default {
@ -66,6 +66,7 @@ export default {
{ required: true, trigger: "change", message: "验证码不能为空" }, { required: true, trigger: "change", message: "验证码不能为空" },
], ],
}, },
showCaptcha: '',
loading: false, loading: false,
redirect: undefined, redirect: undefined,
}; };
@ -81,6 +82,9 @@ export default {
created() { created() {
this.getCode(); this.getCode();
this.getCookie(); this.getCookie();
this.getConfigKey("sys.account.captchaOnOff").then((response) => {
this.showCaptcha = response.data;
});
}, },
methods: { methods: {
getCode() { getCode() {

View File

@ -525,7 +525,7 @@ CREATE TABLE `sys_user` (
-- ---------------------------- -- ----------------------------
INSERT INTO `sys_user` VALUES (1, 0, 'admin', '管理员', '0', '', '', '0', '', 'e10adc3949ba59abbe56e057f20f883e', '0', '0', '127.0.0.1', '2021-08-23 14:03:17', 'admin', '2020-11-26 11:52:59', 'admin', '2021-08-03 10:11:24', '管理员'); INSERT INTO `sys_user` VALUES (1, 0, 'admin', '管理员', '0', '', '', '0', '', 'e10adc3949ba59abbe56e057f20f883e', '0', '0', '127.0.0.1', '2021-08-23 14:03:17', 'admin', '2020-11-26 11:52:59', 'admin', '2021-08-03 10:11:24', '管理员');
INSERT INTO `sys_user` VALUES (2, 0, 'zr', 'zr', '0', NULL, NULL, '0', '', 'e10adc3949ba59abbe56e057f20f883e', '0', '0', '', '0001-01-01 00:00:00', 'admin', '2021-07-05 17:29:13', 'admin', '2021-08-02 16:53:04', '普通用户2'); INSERT INTO `sys_user` VALUES (2, 0, 'zr', 'zr', '0', NULL, NULL, '0', '', 'e10adc3949ba59abbe56e057f20f883e', '0', '0', '', '0001-01-01 00:00:00', 'admin', '2021-07-05 17:29:13', 'admin', '2021-08-02 16:53:04', '普通用户2');
INSERT INTO `sys_user` VALUES (3, 100, 'editor', '编辑人员', '0', NULL, NULL, '2', 'http://www.izhaorui.cn/static/pay.jpg', 'E10ADC3949BA59ABBE56E057F20F883E', '0', '0', '127.0.0.1', '2021-08-19 09:27:46', 'admin', '2021-08-18 18:24:53', '', NULL, NULL); INSERT INTO `sys_user` VALUES (3, 100, 'editor', '编辑人员', '0', NULL, NULL, '2', '', 'E10ADC3949BA59ABBE56E057F20F883E', '0', '0', '127.0.0.1', '2021-08-19 09:27:46', 'admin', '2021-08-18 18:24:53', '', NULL, NULL);
SET FOREIGN_KEY_CHECKS = 1; SET FOREIGN_KEY_CHECKS = 1;
@ -636,7 +636,7 @@ create table sys_config (
insert into sys_config values(1, '主框架页-默认皮肤样式名称', 'sys.index.skinName', 'skin-blue', 'Y', 'admin', sysdate(), '', null, '蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow' ); insert into sys_config values(1, '主框架页-默认皮肤样式名称', 'sys.index.skinName', 'skin-blue', 'Y', 'admin', sysdate(), '', null, '蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow' );
insert into sys_config values(2, '用户管理-账号初始密码', 'sys.user.initPassword', '123456', 'Y', 'admin', sysdate(), '', null, '初始化密码 123456' ); insert into sys_config values(2, '用户管理-账号初始密码', 'sys.user.initPassword', '123456', 'Y', 'admin', sysdate(), '', null, '初始化密码 123456' );
insert into sys_config values(3, '主框架页-侧边栏主题', 'sys.index.sideTheme', 'theme-dark', 'Y', 'admin', sysdate(), '', null, '深色主题theme-dark浅色主题theme-light' ); insert into sys_config values(3, '主框架页-侧边栏主题', 'sys.index.sideTheme', 'theme-dark', 'Y', 'admin', sysdate(), '', null, '深色主题theme-dark浅色主题theme-light' );
-- insert into sys_config values(4, '账号自助-验证码开关', 'sys.account.captchaOnOff', 'true', 'Y', 'admin', sysdate(), '', null, '是否开启验证码功能true开启false关闭'); insert into sys_config values(4, '账号自助-验证码开关', 'sys.account.captchaOnOff', 'true', 'Y', 'admin', sysdate(), '', null, '是否开启验证码功能off、关闭1、动态验证码 2、动态gif泡泡 3、泡泡 4、静态验证码');
-- ---------------------------- -- ----------------------------
-- 18、代码生成业务表 -- 18、代码生成业务表

Binary file not shown.