Răsfoiți Sursa

调整授权

devin.zhu@doverfs.com 1 an în urmă
părinte
comite
f73614ee28

+ 14 - 8
FuelCloud/FuelCloud.sln

@@ -9,11 +9,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fuel.PaymentServer", "src\F
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fuel.Payment.Core", "src\Fuel.Payment.Core\Fuel.Payment.Core.csproj", "{82ADD4DA-C4A6-49EA-8297-D2477CA6EAF3}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fuel.Payment.Service", "src\Fuel.Payment.Service\Fuel.Payment.Service.csproj", "{92947E0E-4C48-4D94-A243-F097081B3694}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fuel.Payment.Service", "src\Fuel.Payment.Service\Fuel.Payment.Service.csproj", "{92947E0E-4C48-4D94-A243-F097081B3694}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fuel.Payment.Repositories", "Fuel.Payment.Repositories\Fuel.Payment.Repositories.csproj", "{8D22ECD9-B1F9-46AD-84B5-CB737B318DDE}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fuel.Payment.ExtServer", "src\Fuel.Payment.ExtServer\Fuel.Payment.ExtServer.csproj", "{ADB9278E-D932-422B-8585-DFA0F0333D3B}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fuel.Payment.ExtServer", "src\Fuel.Payment.ExtServer\Fuel.Payment.ExtServer.csproj", "{ADB9278E-D932-422B-8585-DFA0F0333D3B}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FuelServer.Core", "src\FuelServer.Core\FuelServer.Core.csproj", "{AA90DFD1-3818-4CFF-BD54-B42866AE2FD6}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fuel.Payment.Repositories", "..\..\Smartfuel Lite\FuelCloud\src\Fuel.Payment.Repositories\Fuel.Payment.Repositories.csproj", "{9CAE096E-46E3-4F9A-8CB5-403FF45D751C}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -33,14 +35,18 @@ Global
 		{92947E0E-4C48-4D94-A243-F097081B3694}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{92947E0E-4C48-4D94-A243-F097081B3694}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{92947E0E-4C48-4D94-A243-F097081B3694}.Release|Any CPU.Build.0 = Release|Any CPU
-		{8D22ECD9-B1F9-46AD-84B5-CB737B318DDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{8D22ECD9-B1F9-46AD-84B5-CB737B318DDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{8D22ECD9-B1F9-46AD-84B5-CB737B318DDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{8D22ECD9-B1F9-46AD-84B5-CB737B318DDE}.Release|Any CPU.Build.0 = Release|Any CPU
 		{ADB9278E-D932-422B-8585-DFA0F0333D3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{ADB9278E-D932-422B-8585-DFA0F0333D3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{ADB9278E-D932-422B-8585-DFA0F0333D3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{ADB9278E-D932-422B-8585-DFA0F0333D3B}.Release|Any CPU.Build.0 = Release|Any CPU
+		{AA90DFD1-3818-4CFF-BD54-B42866AE2FD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{AA90DFD1-3818-4CFF-BD54-B42866AE2FD6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{AA90DFD1-3818-4CFF-BD54-B42866AE2FD6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{AA90DFD1-3818-4CFF-BD54-B42866AE2FD6}.Release|Any CPU.Build.0 = Release|Any CPU
+		{9CAE096E-46E3-4F9A-8CB5-403FF45D751C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{9CAE096E-46E3-4F9A-8CB5-403FF45D751C}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{9CAE096E-46E3-4F9A-8CB5-403FF45D751C}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{9CAE096E-46E3-4F9A-8CB5-403FF45D751C}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -49,8 +55,8 @@ Global
 		{C6136845-5F8D-4021-9C6B-028CB386647A} = {A70E5B56-8E5C-4EB2-92F0-66FA7E67A2D1}
 		{82ADD4DA-C4A6-49EA-8297-D2477CA6EAF3} = {A70E5B56-8E5C-4EB2-92F0-66FA7E67A2D1}
 		{92947E0E-4C48-4D94-A243-F097081B3694} = {A70E5B56-8E5C-4EB2-92F0-66FA7E67A2D1}
-		{8D22ECD9-B1F9-46AD-84B5-CB737B318DDE} = {A70E5B56-8E5C-4EB2-92F0-66FA7E67A2D1}
 		{ADB9278E-D932-422B-8585-DFA0F0333D3B} = {A70E5B56-8E5C-4EB2-92F0-66FA7E67A2D1}
+		{9CAE096E-46E3-4F9A-8CB5-403FF45D751C} = {A70E5B56-8E5C-4EB2-92F0-66FA7E67A2D1}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {61A0BE4B-D15C-4489-B07B-A028AEC93C1D}

+ 4 - 0
FuelCloud/src/Fuel.Payment.Core/Fuel.Payment.Core.csproj

@@ -16,4 +16,8 @@
     <PackageReference Include="DFS.Core" Version="8.0.1" />
   </ItemGroup>
 
+  <ItemGroup>
+    <ProjectReference Include="..\FuelServer.Core\FuelServer.Core.csproj" />
+  </ItemGroup>
+
 </Project>

+ 0 - 0
FuelCloud/Fuel.Payment.Repositories/Class1.cs → FuelCloud/src/Fuel.Payment.Repositories/Class1.cs


+ 17 - 0
FuelCloud/src/Fuel.Payment.Repositories/Fuel.Payment.Repositories.csproj

@@ -0,0 +1,17 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net8.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <Folder Include="Impl\" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <PackageReference Include="ShardingCore" Version="7.8.1.21" />
+  </ItemGroup>
+
+</Project>

+ 19 - 1
FuelCloud/src/Fuel.Payment.Server/Controllers/WeatherForecastController.cs

@@ -1,9 +1,10 @@
+using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
 
 namespace Fuel.PaymentServer.Controllers
 {
     [ApiController]
-    [Route("[controller]")]
+    [Route("api/[controller]/[action]")]
     public class WeatherForecastController : ControllerBase
     {
         private static readonly string[] Summaries = new[]
@@ -18,6 +19,11 @@ namespace Fuel.PaymentServer.Controllers
             _logger = logger;
         }
 
+
+        /// <summary>
+        /// GetWeatherForecast
+        /// </summary>
+        /// <returns></returns>
         [HttpGet(Name = "GetWeatherForecast")]
         public IEnumerable<WeatherForecast> Get()
         {
@@ -29,5 +35,17 @@ namespace Fuel.PaymentServer.Controllers
             })
             .ToArray();
         }
+
+
+        /// <summary>
+        /// get
+        /// </summary>
+        /// <returns></returns>
+        [HttpGet(Name = "Apis")]
+        [Authorize]
+        public int GetApi()
+        {
+            return 0;
+        }
     }
 }

+ 1 - 0
FuelCloud/src/Fuel.Payment.Server/Fuel.PaymentServer.csproj

@@ -9,6 +9,7 @@
   <ItemGroup>
     <PackageReference Include="DFS.Infrastructure.Redis" Version="8.0.0" />
     <PackageReference Include="JWT" Version="10.1.1" />
+    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.5" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
   </ItemGroup>

+ 1 - 1
FuelCloud/src/Fuel.Payment.Server/MicServer/Authorize/DfsAuthorizationHandler.cs

@@ -10,7 +10,7 @@ namespace DFS.Core.Mvc.Authorize
     /// </summary>
     public class DFSAuthorizationHandler : AuthorizationHandler<DfsAuthorizationRequirement>
     {
-        public IRedisClient _redisClient;
+        //public IRedisClient _redisClient;
         public DFSAuthorizationHandler()
         {
 

+ 60 - 0
FuelCloud/src/Fuel.Payment.Server/MicServer/WebAppliactionExtend.cs

@@ -1,15 +1,26 @@
 using DFS.Core.Mvc;
+using DFS.Core.Mvc.Authorize;
 using DFS.Core.Mvc.Filters;
 using DFS.Core.Mvc.Jwt;
 using DFS.Core.Utils;
 using DFS.Core.Utily;
+using FuelServer.Core;
+using Microsoft.AspNetCore.Authentication.JwtBearer;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.IdentityModel.Tokens;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Serialization;
+using System.Text;
+using System.Xml.Linq;
 
 namespace Fuel.PaymentServer.MicServer
 {
+
+   
+
     public static class WebAppliactionExtend
     {
+       
 
         /// <summary>
         /// 添加服务注入
@@ -22,6 +33,55 @@ namespace Fuel.PaymentServer.MicServer
             services.AddSingleton(new Appsettings(environmentName.ContentRootPath, environmentName.EnvironmentName));
             //services.AddJwtAuth();
             services.AddHealthChecks();
+            SecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConstKey.JwtKey));    // 注意:这里的key不能低于16位
+            services.AddAuthentication("Bearer")        // 注入认证服务,认证类型:Bearer
+                .AddJwtBearer(o =>        // 注入 Jwt Bearer认证 服务,对其进行配置
+                {
+                    // 主要是这里:
+                    o.Events = new JwtBearerEvents()
+                    {
+                        OnMessageReceived = context => {
+                            context.Token = context.Request.Query["access_token"];
+                            return Task.CompletedTask;
+                        }
+                    };
+
+                    // 对 jwt 进行配置
+                    o.TokenValidationParameters = new TokenValidationParameters()        // 对Token的认证是哪些参数,这里设置
+                    {
+                        ValidateIssuerSigningKey = true, 
+                        IssuerSigningKey = securityKey,     
+                        ValidateIssuer = true, 
+                        ValidIssuer = ConstKey.JwtIssuer, 
+                        ValidateAudience = true,     
+                        ValidAudience = ConstKey.JwtAudience,       
+                        RequireExpirationTime = true,       
+                        ValidateLifetime = true,        
+                    };
+                });
+
+
+            // 服务注入
+            services.AddAuthorization(o => {
+                //内部账户认证
+                o.AddPolicy("IntClient",o => {
+                    o.RequireRole("IntClient").Build();
+                });
+
+                //内部服务认证
+                o.AddPolicy("IntServer", o => {
+                    o.RequireRole("IntServer").Build();
+                });
+
+                //自定义授权(后台权限验证)
+                o.AddPolicy("Admin", o => {
+                    var myAdminRequirement = new DfsAuthorizationRequirement("DfsAuthorizationRequirement");    // 可以传递参数
+                    o.Requirements.Add(myAdminRequirement);        
+                });
+            });
+
+            services.AddSingleton<IAuthorizationHandler, DFSAuthorizationHandler>();
+
             services.AddControllers(options =>
             {
                 options.Filters.Add(typeof(ValidationActionFilter));

+ 2 - 0
FuelCloud/src/Fuel.Payment.Server/Program.cs

@@ -29,6 +29,8 @@ app.UseDFSServer();
 
 app.UseHttpsRedirection();
 
+app.UseAuthentication();
+
 app.UseAuthorization();
 
 app.MapControllers();

+ 24 - 0
FuelCloud/src/FuelServer.Core/ConstKey.cs

@@ -0,0 +1,24 @@
+namespace FuelServer.Core
+{
+
+    /// <summary>
+    /// 系统的常量定义
+    /// </summary>
+    public static class ConstKey
+    {
+        /// <summary>
+        /// key
+        /// </summary>
+        public const string JwtKey = "e5d37731-9010-4718-956e-541745d01687";
+
+        /// <summary>
+        /// Issuer
+        /// </summary>
+        public const string JwtIssuer = "fuelServer.com";
+
+        /// <summary>
+        /// Audience
+        /// </summary>
+        public const string JwtAudience = "fuelServer.com";
+    }
+}

+ 0 - 4
FuelCloud/Fuel.Payment.Repositories/Fuel.Payment.Repositories.csproj → FuelCloud/src/FuelServer.Core/FuelServer.Core.csproj

@@ -6,8 +6,4 @@
     <Nullable>enable</Nullable>
   </PropertyGroup>
 
-  <ItemGroup>
-    <Folder Include="Impl\" />
-  </ItemGroup>
-
 </Project>