From 7639ba548e9f762c71cdc6dfbad2d93c7e1cd3e2 Mon Sep 17 00:00:00 2001 From: anjoy8 <3143422472@qq.com> Date: Tue, 26 Jan 2021 10:48:56 +0800 Subject: [PATCH] adjust gateway MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调整网关 --- Blog.Core.Common/GlobalVar/GlobalVars.cs | 8 +++ Blog.Core.Gateway/Blog.Core.Gateway.csproj | 7 ++- Blog.Core.Gateway/Blog.Core.Gateway.xml | 17 ++++++ .../Controllers/UserController.cs | 19 ++++-- .../Extensions/CustomOcelotSetup.cs | 30 ++++++++++ .../Extensions/CustomSwaggerSetup.cs | 58 +++++++++++++++++++ Blog.Core.Gateway/Extensions/OcelotMildd.cs | 16 ----- Blog.Core.Gateway/Program.cs | 3 +- Blog.Core.Gateway/Startup.cs | 11 +++- Blog.Core.Gateway/ocelot.Development.json | 52 +++++++++++++++++ Blog.Core.Gateway/ocelot.Production.json | 3 + Blog.Core.Gateway/ocelot.Staging.json | 3 + .../{OcelotGatewaySet.json => ocelot.json} | 0 13 files changed, 202 insertions(+), 25 deletions(-) create mode 100644 Blog.Core.Gateway/Blog.Core.Gateway.xml create mode 100644 Blog.Core.Gateway/Extensions/CustomOcelotSetup.cs create mode 100644 Blog.Core.Gateway/Extensions/CustomSwaggerSetup.cs delete mode 100644 Blog.Core.Gateway/Extensions/OcelotMildd.cs create mode 100644 Blog.Core.Gateway/ocelot.Development.json create mode 100644 Blog.Core.Gateway/ocelot.Production.json create mode 100644 Blog.Core.Gateway/ocelot.Staging.json rename Blog.Core.Gateway/{OcelotGatewaySet.json => ocelot.json} (100%) diff --git a/Blog.Core.Common/GlobalVar/GlobalVars.cs b/Blog.Core.Common/GlobalVar/GlobalVars.cs index 7fa1b8d..6813487 100644 --- a/Blog.Core.Common/GlobalVar/GlobalVars.cs +++ b/Blog.Core.Common/GlobalVar/GlobalVars.cs @@ -7,6 +7,14 @@ { public const string Name = "Permission"; + /// + /// 测试网关授权 + /// 可以使用Blog.Core项目中的test用户 + /// 账号:test + /// 密码:test + /// + public const string GWName = "GW"; + /// /// 当前项目是否启用IDS4权限方案 /// true:表示启动IDS4 diff --git a/Blog.Core.Gateway/Blog.Core.Gateway.csproj b/Blog.Core.Gateway/Blog.Core.Gateway.csproj index 09f6dee..8471335 100644 --- a/Blog.Core.Gateway/Blog.Core.Gateway.csproj +++ b/Blog.Core.Gateway/Blog.Core.Gateway.csproj @@ -1,9 +1,14 @@ - + net5.0 + + ..\Blog.Core.Gateway\Blog.Core.Gateway.xml + 1701;1702;1591 + + diff --git a/Blog.Core.Gateway/Blog.Core.Gateway.xml b/Blog.Core.Gateway/Blog.Core.Gateway.xml new file mode 100644 index 0000000..33e74e7 --- /dev/null +++ b/Blog.Core.Gateway/Blog.Core.Gateway.xml @@ -0,0 +1,17 @@ + + + + Blog.Core.Gateway + + + + ┌──────────────────────────────────────────────────────────────┐ + │ 描 述:模拟一个网关项目 + │ 测 试:http://localhost:9000/gateway/user/MyClaims + │ 测 试:http://localhost:9000/gateway/api/blog + │ 测 试:http://localhost:9000/gateway/is4api/GetAchieveUsers + │ 作 者:anson zhang + └──────────────────────────────────────────────────────────────┘ + + + diff --git a/Blog.Core.Gateway/Controllers/UserController.cs b/Blog.Core.Gateway/Controllers/UserController.cs index b03889a..df715f3 100644 --- a/Blog.Core.Gateway/Controllers/UserController.cs +++ b/Blog.Core.Gateway/Controllers/UserController.cs @@ -8,7 +8,7 @@ using System.Security.Claims; namespace Blog.Core.Gateway.Controllers { - [Authorize] + [Authorize(Permissions.GWName)] [Route("/gateway/[controller]/[action]")] public class UserController : ControllerBase { @@ -20,13 +20,24 @@ namespace Blog.Core.Gateway.Controllers } [HttpGet] - public MessageModel> MyClaims() + public MessageModel> MyClaims() { - return new MessageModel>() + return new MessageModel>() { success = true, - response = _user.GetClaimsIdentity().ToList() + response = (_user.GetClaimsIdentity().ToList()).Select(d => + new ClaimDto + { + Type = d.Type, + Value = d.Value + } + ).ToList() }; } } + public class ClaimDto + { + public string Type { get; set; } + public string Value { get; set; } + } } diff --git a/Blog.Core.Gateway/Extensions/CustomOcelotSetup.cs b/Blog.Core.Gateway/Extensions/CustomOcelotSetup.cs new file mode 100644 index 0000000..831d3e6 --- /dev/null +++ b/Blog.Core.Gateway/Extensions/CustomOcelotSetup.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; +using Ocelot.Provider.Consul; +using Ocelot.Provider.Polly; +using System; +using System.Threading.Tasks; + +namespace Blog.Core.Gateway.Extensions +{ + public static class CustomOcelotSetup + { + public static void AddCustomOcelotSetup(this IServiceCollection services) + { + if (services == null) throw new ArgumentNullException(nameof(services)); + + var basePath = AppContext.BaseDirectory; + + services.AddOcelot().AddConsul().AddPolly(); + } + + public static async Task UseCustomOcelotMildd(this IApplicationBuilder app) + { + await app.UseOcelot(); + return app; + } + + } +} diff --git a/Blog.Core.Gateway/Extensions/CustomSwaggerSetup.cs b/Blog.Core.Gateway/Extensions/CustomSwaggerSetup.cs new file mode 100644 index 0000000..f392ef6 --- /dev/null +++ b/Blog.Core.Gateway/Extensions/CustomSwaggerSetup.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.Filters; +using System; +using System.IO; + +namespace Blog.Core.Gateway.Extensions +{ + public static class CustomSwaggerSetup + { + public static void AddCustomSwaggerSetup(this IServiceCollection services) + { + if (services == null) throw new ArgumentNullException(nameof(services)); + + var basePath = AppContext.BaseDirectory; + + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo + { + Version = "v1", + Title = "自定义网关 接口文档", + }); + + var xmlPath = Path.Combine(basePath, "Blog.Core.Gateway.xml"); + c.IncludeXmlComments(xmlPath, true); + + c.OperationFilter(); + c.OperationFilter(); + + c.OperationFilter(); + + c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme + { + Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"", + Name = "Authorization", + In = ParameterLocation.Header, + Type = SecuritySchemeType.ApiKey + }); + }); + } + + public static void UseCustomSwaggerMildd(this IApplicationBuilder app) + { + if (app == null) throw new ArgumentNullException(nameof(app)); + + app.UseSwagger(); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint($"/swagger/v1/swagger.json", $"Blog.Core.Gateway-v1"); + c.RoutePrefix = ""; + }); + } + + + } +} diff --git a/Blog.Core.Gateway/Extensions/OcelotMildd.cs b/Blog.Core.Gateway/Extensions/OcelotMildd.cs deleted file mode 100644 index d8e3393..0000000 --- a/Blog.Core.Gateway/Extensions/OcelotMildd.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Ocelot.Middleware; -using System.Threading.Tasks; - -namespace Blog.Core.Gateway.Extensions -{ - public static class OcelotMildd - { - public static async Task UseOcelotMildd(this IApplicationBuilder app) - { - await app.UseOcelot(); - return app; - } - - } -} diff --git a/Blog.Core.Gateway/Program.cs b/Blog.Core.Gateway/Program.cs index 6a5ba3a..1ba4681 100644 --- a/Blog.Core.Gateway/Program.cs +++ b/Blog.Core.Gateway/Program.cs @@ -15,7 +15,8 @@ namespace Blog.Core.AdminMvc Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) => { - config.AddJsonFile("OcelotGatewaySet.json", optional: false, reloadOnChange: true); + config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true) + .AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true); }) .ConfigureWebHostDefaults(webBuilder => { diff --git a/Blog.Core.Gateway/Startup.cs b/Blog.Core.Gateway/Startup.cs index 5f98e1c..64e7b48 100644 --- a/Blog.Core.Gateway/Startup.cs +++ b/Blog.Core.Gateway/Startup.cs @@ -39,16 +39,18 @@ namespace Blog.Core.AdminMvc services.AddAuthorization(options => { - options.AddPolicy("GW", policy => policy.RequireRole("GW").Build()); + options.AddPolicy("GW", policy => policy.RequireRole("AdminTest").Build()); }); + services.AddCustomSwaggerSetup(); + services.AddControllers(); services.AddHttpContextSetup(); services.AddCorsSetup(); - services.AddOcelot().AddConsul(); + services.AddCustomOcelotSetup(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -61,8 +63,11 @@ namespace Blog.Core.AdminMvc app.UseRouting(); + app.UseAuthentication(); app.UseAuthorization(); + app.UseCustomSwaggerMildd(); + app.UseCors(Appsettings.app(new string[] { "Startup", "Cors", "PolicyName" })); app.UseEndpoints(endpoints => @@ -70,7 +75,7 @@ namespace Blog.Core.AdminMvc endpoints.MapControllers(); }); - app.UseOcelotMildd().Wait(); + app.UseCustomOcelotMildd().Wait(); } } } diff --git a/Blog.Core.Gateway/ocelot.Development.json b/Blog.Core.Gateway/ocelot.Development.json new file mode 100644 index 0000000..3889dfb --- /dev/null +++ b/Blog.Core.Gateway/ocelot.Development.json @@ -0,0 +1,52 @@ +{ + "Routes": [ + { + "UpstreamPathTemplate": "/gateway/api/{url}", + "UpstreamHttpMethod": [ + "Get", + "Post", + "Put", + "Delete" + ], + "LoadBalancerOptions": { + "Type": "RoundRobin" + }, + "DownstreamPathTemplate": "/api/{url}", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 8081 + } + ] + }, + { + "UpstreamPathTemplate": "/gateway/is4api/{url}", + "UpstreamHttpMethod": [ + "Get", + "Post", + "Put", + "Delete" + ], + "LoadBalancerOptions": { + "Type": "RoundRobin" + }, + "DownstreamPathTemplate": "/is4api/{url}", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 5004 + } + ] + } + ], + "GlobalConfiguration": { + "BaseUrl": "http://localhost:9000", + "ServiceDiscoveryProvider": { + "Host": "localhost", + "Port": 8500, + "Type": "Consul" + } + } +} \ No newline at end of file diff --git a/Blog.Core.Gateway/ocelot.Production.json b/Blog.Core.Gateway/ocelot.Production.json new file mode 100644 index 0000000..0db3279 --- /dev/null +++ b/Blog.Core.Gateway/ocelot.Production.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/Blog.Core.Gateway/ocelot.Staging.json b/Blog.Core.Gateway/ocelot.Staging.json new file mode 100644 index 0000000..0db3279 --- /dev/null +++ b/Blog.Core.Gateway/ocelot.Staging.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/Blog.Core.Gateway/OcelotGatewaySet.json b/Blog.Core.Gateway/ocelot.json similarity index 100% rename from Blog.Core.Gateway/OcelotGatewaySet.json rename to Blog.Core.Gateway/ocelot.json