mirror of
https://github.com/anjoy8/Blog.Core.git
synced 2024-09-20 23:48:27 +08:00
adjust gateway
调整网关
This commit is contained in:
parent
8ecb6e8ac4
commit
7639ba548e
|
@ -7,6 +7,14 @@
|
|||
{
|
||||
public const string Name = "Permission";
|
||||
|
||||
/// <summary>
|
||||
/// 测试网关授权
|
||||
/// 可以使用Blog.Core项目中的test用户
|
||||
/// 账号:test
|
||||
/// 密码:test
|
||||
/// </summary>
|
||||
public const string GWName = "GW";
|
||||
|
||||
/// <summary>
|
||||
/// 当前项目是否启用IDS4权限方案
|
||||
/// true:表示启动IDS4
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DocumentationFile>..\Blog.Core.Gateway\Blog.Core.Gateway.xml</DocumentationFile>
|
||||
<NoWarn>1701;1702;1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Extensions\ApiResponseHandler.cs" />
|
||||
</ItemGroup>
|
||||
|
|
17
Blog.Core.Gateway/Blog.Core.Gateway.xml
Normal file
17
Blog.Core.Gateway/Blog.Core.Gateway.xml
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>Blog.Core.Gateway</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="M:Blog.Core.AdminMvc.Startup.#ctor(Microsoft.Extensions.Configuration.IConfiguration,Microsoft.AspNetCore.Hosting.IWebHostEnvironment)">
|
||||
┌──────────────────────────────────────────────────────────────┐
|
||||
│ 描 述:模拟一个网关项目
|
||||
│ 测 试:http://localhost:9000/gateway/user/MyClaims
|
||||
│ 测 试:http://localhost:9000/gateway/api/blog
|
||||
│ 测 试:http://localhost:9000/gateway/is4api/GetAchieveUsers
|
||||
│ 作 者:anson zhang
|
||||
└──────────────────────────────────────────────────────────────┘
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
|
@ -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<List<Claim>> MyClaims()
|
||||
public MessageModel<List<ClaimDto>> MyClaims()
|
||||
{
|
||||
return new MessageModel<List<Claim>>()
|
||||
return new MessageModel<List<ClaimDto>>()
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
||||
|
|
30
Blog.Core.Gateway/Extensions/CustomOcelotSetup.cs
Normal file
30
Blog.Core.Gateway/Extensions/CustomOcelotSetup.cs
Normal file
|
@ -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<IApplicationBuilder> UseCustomOcelotMildd(this IApplicationBuilder app)
|
||||
{
|
||||
await app.UseOcelot();
|
||||
return app;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
58
Blog.Core.Gateway/Extensions/CustomSwaggerSetup.cs
Normal file
58
Blog.Core.Gateway/Extensions/CustomSwaggerSetup.cs
Normal file
|
@ -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<AddResponseHeadersFilter>();
|
||||
c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
|
||||
|
||||
c.OperationFilter<SecurityRequirementsOperationFilter>();
|
||||
|
||||
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 = "";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -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<IApplicationBuilder> UseOcelotMildd(this IApplicationBuilder app)
|
||||
{
|
||||
await app.UseOcelot();
|
||||
return app;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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 =>
|
||||
{
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
52
Blog.Core.Gateway/ocelot.Development.json
Normal file
52
Blog.Core.Gateway/ocelot.Development.json
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
3
Blog.Core.Gateway/ocelot.Production.json
Normal file
3
Blog.Core.Gateway/ocelot.Production.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
|
||||
}
|
3
Blog.Core.Gateway/ocelot.Staging.json
Normal file
3
Blog.Core.Gateway/ocelot.Staging.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user