Browse Source

认证授权服务新增是否开启验证码接口
认证授权服务登录接口支持新版滑块验证功能

zhontai 2 years ago
parent
commit
4cd0538a31

+ 1 - 24
src/hosts/ZhonTai.Host/Program.cs

@@ -1,25 +1,13 @@
-using Autofac.Core;
-using FreeRedis;
-using FreeScheduler;
-using Lazy.SlideCaptcha.Core;
-using Microsoft.Extensions.Caching.Distributed;
+using FreeScheduler;
 using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.DependencyModel;
 using Microsoft.Extensions.Hosting;
-using Newtonsoft.Json;
-using Savorboard.CAP.InMemoryMessageQueue;
-using System.Linq;
-using System.Reflection;
 using ZhonTai;
 using ZhonTai.Admin.Core;
-using ZhonTai.Admin.Core.Captcha;
 using ZhonTai.Admin.Core.Configs;
 using ZhonTai.Admin.Core.Consts;
 using ZhonTai.Admin.Core.Startup;
-using ZhonTai.Admin.Tools.Cache;
 using ZhonTai.Admin.Tools.TaskScheduler;
 using ZhonTai.ApiUI;
-using ZhonTai.Common.Helpers;
 
 new HostApp(new HostAppOptions
 {
@@ -60,17 +48,6 @@ new HostApp(new HostAppOptions
             //模块任务处理器
             options.TaskHandler = new TaskHandler(options.FreeSql);
         });
-
-        //oss文件上传
-        //context.Services.AddOSS();
-
-        //滑块验证码
-        context.Services.AddSlideCaptcha(context.Configuration, options =>
-        {
-            options.StoreageKeyPrefix = CacheKeys.Captcha;
-        });
-        context.Services.AddScoped<ISlideCaptcha, SlideCaptcha>();
-
     },
 
     //配置Autofac容器

+ 11 - 0
src/platform/ZhonTai.Admin/Core/HostApp.cs

@@ -58,6 +58,7 @@ using FreeRedis;
 using HealthChecks.UI.Client;
 using Microsoft.AspNetCore.Diagnostics.HealthChecks;
 using Microsoft.Extensions.Caching.Distributed;
+using ZhonTai.Admin.Core.Captcha;
 
 namespace ZhonTai.Admin.Core;
 
@@ -620,6 +621,16 @@ public class HostApp
             _hostAppOptions?.ConfigureDynamicApi?.Invoke(options);
         });
 
+        //oss文件上传
+        services.AddOSS();
+
+        //滑块验证码
+        services.AddSlideCaptcha(configuration, options =>
+        {
+            options.StoreageKeyPrefix = CacheKeys.Captcha;
+        });
+        services.AddScoped<ISlideCaptcha, SlideCaptcha>();
+
         _hostAppOptions?.ConfigurePostServices?.Invoke(hostAppContext);
     }
 

+ 16 - 35
src/platform/ZhonTai.Admin/Services/Auth/AuthService.cs

@@ -5,7 +5,6 @@ using System.Text;
 using System.Security.Claims;
 using System.Threading.Tasks;
 using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Cors;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc.ModelBinding;
 using Microsoft.IdentityModel.Tokens;
@@ -21,7 +20,6 @@ using ZhonTai.Admin.Domain.Tenant;
 using ZhonTai.Admin.Services.Auth.Dto;
 using ZhonTai.Admin.Domain.RolePermission;
 using ZhonTai.Admin.Domain.UserRole;
-using ZhonTai.Admin.Tools.Captcha;
 using ZhonTai.Admin.Services.LoginLog.Dto;
 using ZhonTai.Admin.Services.LoginLog;
 using ZhonTai.Admin.Services.User;
@@ -33,6 +31,10 @@ using FreeSql;
 using ZhonTai.Admin.Domain.TenantPermission;
 using Microsoft.AspNetCore.Identity;
 using System.Collections.Generic;
+using ZhonTai.Admin.Core.Captcha;
+using Newtonsoft.Json;
+using Lazy.SlideCaptcha.Core.Validator;
+using static Lazy.SlideCaptcha.Core.ValidateResult;
 
 namespace ZhonTai.Admin.Services.Auth;
 
@@ -47,16 +49,15 @@ public class AuthService : BaseService, IAuthService, IDynamicApi
     private readonly IPermissionRepository _permissionRepository;
     private readonly IUserRepository _userRepository;
     private readonly ITenantRepository _tenantRepository;
-    private readonly ICaptchaTool _captchaTool;
     private IPasswordHasher<UserEntity> _passwordHasher => LazyGetRequiredService<IPasswordHasher<UserEntity>>();
+    private ISlideCaptcha _captcha => LazyGetRequiredService<ISlideCaptcha>();
 
     public AuthService(
         AppConfig appConfig,
         JwtConfig jwtConfig,
         IUserRepository userRepository,
         IPermissionRepository permissionRepository,
-        ITenantRepository tenantRepository,
-        ICaptchaTool captchaTool
+        ITenantRepository tenantRepository
     )
     {
         _appConfig = appConfig;
@@ -64,7 +65,6 @@ public class AuthService : BaseService, IAuthService, IDynamicApi
         _userRepository = userRepository;
         _permissionRepository = permissionRepository;
         _tenantRepository = tenantRepository;
-        _captchaTool = captchaTool;
     }
 
     /// <summary>
@@ -230,7 +230,6 @@ public class AuthService : BaseService, IAuthService, IDynamicApi
         }
     }
 
-
     /// <summary>
     /// 查询用户信息
     /// </summary>
@@ -324,12 +323,14 @@ public class AuthService : BaseService, IAuthService, IDynamicApi
 
             if (_appConfig.VarifyCode.Enable)
             {
-                input.Captcha.DeleteCache = true;
-                input.Captcha.CaptchaKey = CacheKeys.Captcha;
-                var isOk = await _captchaTool.CheckAsync(input.Captcha);
-                if (!isOk)
+                if(input.CaptchaId.IsNull() || input.CaptchaData.IsNull())
+                {
+                    throw ResultOutput.Exception("请完成安全验证");
+                }
+                var validateResult = _captcha.Validate(input.CaptchaId, JsonConvert.DeserializeObject<SlideTrack>(input.CaptchaData));
+                if (validateResult.Result != ValidateResultType.Success)
                 {
-                    throw ResultOutput.Exception("安全验证不通过,请重新登录");
+                    throw ResultOutput.Exception($"安全{validateResult.Message},请重新登录");
                 }
             }
 
@@ -484,34 +485,14 @@ public class AuthService : BaseService, IAuthService, IDynamicApi
     }
 
     /// <summary>
-    /// 获取验证数据
-    /// </summary>
-    /// <returns></returns>
-    [HttpGet]
-    [AllowAnonymous]
-    [NoOprationLog]
-    [EnableCors(AdminConsts.AllowAnyPolicyName)]
-    public async Task<CaptchaOutput> GetCaptcha()
-    {
-        var data = await _captchaTool.GetAsync(CacheKeys.Captcha);
-        return data;
-    }
-
-    /// <summary>
-    /// 检查验证数据
+    /// 是否开启验证码
     /// </summary>
     /// <returns></returns>
     [HttpGet]
     [AllowAnonymous]
     [NoOprationLog]
-    [EnableCors(AdminConsts.AllowAnyPolicyName)]
-    public async Task CheckCaptcha([FromQuery] CaptchaInput input)
+    public bool IsCaptcha()
     {
-        input.CaptchaKey = CacheKeys.Captcha;
-        var check = await _captchaTool.CheckAsync(input);
-        if (!check)
-        {
-            throw ResultOutput.Exception("安全验证不通过");
-        }
+        return _appConfig.VarifyCode.Enable;
     }
 }

+ 8 - 4
src/platform/ZhonTai.Admin/Services/Auth/Dto/AuthLoginInput.cs

@@ -1,5 +1,4 @@
-using ZhonTai.Admin.Tools.Captcha;
-using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations;
 
 namespace ZhonTai.Admin.Services.Auth.Dto;
 
@@ -26,7 +25,12 @@ public class AuthLoginInput
     public string PasswordKey { get; set; }
 
     /// <summary>
-    /// 验证数据
+    /// 验证码Id
     /// </summary>
-    public CaptchaInput Captcha { get; set; }
+    public string CaptchaId { get; set; }
+
+    /// <summary>
+    /// 验证码数据
+    /// </summary>
+    public string CaptchaData { get; set; }
 }

+ 0 - 4
src/platform/ZhonTai.Admin/Services/Auth/IAuthService.cs

@@ -16,10 +16,6 @@ public interface IAuthService
 
     Task<AuthGetPasswordEncryptKeyOutput> GetPasswordEncryptKeyAsync();
 
-    Task<CaptchaOutput> GetCaptcha();
-
-    Task CheckCaptcha(CaptchaInput input);
-
     Task<dynamic> Refresh([BindRequired] string token);
 
 }

+ 5 - 5
src/platform/ZhonTai.Admin/Services/Captcha/CaptchaService.cs

@@ -26,7 +26,7 @@ public class CaptchaService : BaseService, IDynamicApi
     /// <summary>
     /// 生成
     /// </summary>
-    /// <param name="captchaId"></param>
+    /// <param name="captchaId">验证码id</param>
     /// <returns></returns>
     [AllowAnonymous]
     [NoOprationLog]
@@ -38,13 +38,13 @@ public class CaptchaService : BaseService, IDynamicApi
     /// <summary>
     /// 验证
     /// </summary>
-    /// <param name="id"></param>
-    /// <param name="track"></param>
+    /// <param name="captchaId">验证码id</param>
+    /// <param name="track">滑动轨迹</param>
     /// <returns></returns>
     [AllowAnonymous]
     [NoOprationLog]
-    public ValidateResult CheckAsync([FromQuery] string id, SlideTrack track)
+    public ValidateResult CheckAsync([FromQuery] string captchaId, SlideTrack track)
     {
-        return _slideCaptcha.Validate(id, track, false);
+        return _slideCaptcha.Validate(captchaId, track, false);
     }
 }

+ 12 - 13
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -3718,15 +3718,9 @@
             <param name="token"></param>
             <returns></returns>
         </member>
-        <member name="M:ZhonTai.Admin.Services.Auth.AuthService.GetCaptcha">
+        <member name="M:ZhonTai.Admin.Services.Auth.AuthService.IsCaptcha">
             <summary>
-            获取验证数据
-            </summary>
-            <returns></returns>
-        </member>
-        <member name="M:ZhonTai.Admin.Services.Auth.AuthService.CheckCaptcha(ZhonTai.Admin.Tools.Captcha.CaptchaInput)">
-            <summary>
-            检查验证数据
+            是否开启验证码
             </summary>
             <returns></returns>
         </member>
@@ -3795,9 +3789,14 @@
             密码键
             </summary>
         </member>
-        <member name="P:ZhonTai.Admin.Services.Auth.Dto.AuthLoginInput.Captcha">
+        <member name="P:ZhonTai.Admin.Services.Auth.Dto.AuthLoginInput.CaptchaId">
             <summary>
-            验证数据
+            验证码Id
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Services.Auth.Dto.AuthLoginInput.CaptchaData">
+            <summary>
+            验证码数据
             </summary>
         </member>
         <member name="P:ZhonTai.Admin.Services.Auth.Dto.AuthLoginOutput.Id">
@@ -4059,15 +4058,15 @@
             <summary>
             生成
             </summary>
-            <param name="captchaId"></param>
+            <param name="captchaId">验证码id</param>
             <returns></returns>
         </member>
         <member name="M:ZhonTai.Admin.Services.Cache.CaptchaService.CheckAsync(System.String,Lazy.SlideCaptcha.Core.Validator.SlideTrack)">
             <summary>
             验证
             </summary>
-            <param name="id"></param>
-            <param name="track"></param>
+            <param name="captchaId">验证码id</param>
+            <param name="track">滑动轨迹</param>
             <returns></returns>
         </member>
         <member name="T:ZhonTai.Admin.Services.DictionaryType.DictionaryTypeService">