Skip to content

Commit

Permalink
feat:更新gateway,去除nacos相关配置
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoy8 committed Dec 3, 2023
1 parent c4a6c84 commit 5f132f0
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 321 deletions.
8 changes: 8 additions & 0 deletions Blog.Core.Gateway/Blog.Core.Gateway.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
<Compile Remove="Helper\HeaderDelegatingHandler.cs" />
</ItemGroup>

<ItemGroup>
<None Remove="index.html" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="index.html" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" NoWarn="NU1605" />
Expand Down
45 changes: 0 additions & 45 deletions Blog.Core.Gateway/Blog.Core.Gateway.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Blog.Core.Gateway/Extensions/CustomOcelotSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public static void AddCustomOcelotSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));

var basePath = AppContext.BaseDirectory;

services.AddAuthentication_JWTSetup();
services.AddOcelot().AddDelegatingHandler<CustomResultHandler>().AddNacosDiscovery().AddPolly();
//.AddConsul().AddPolly();
services.AddOcelot()
.AddDelegatingHandler<CustomResultHandler>()
//.AddNacosDiscovery()
//.AddConsul()
.AddPolly();
}

public static async Task<IApplicationBuilder> UseCustomOcelotMildd(this IApplicationBuilder app)
Expand Down
30 changes: 21 additions & 9 deletions Blog.Core.Gateway/Extensions/CustomSwaggerSetup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using Microsoft.AspNetCore.Builder;
using Blog.Core.Common;
using Blog.Core.Extensions.Middlewares;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using static Blog.Core.Extensions.CustomApiVersion;
namespace Blog.Core.Gateway.Extensions
{
public static class CustomSwaggerSetup
Expand Down Expand Up @@ -44,23 +49,30 @@ public static void AddCustomSwaggerSetup(this IServiceCollection services)
});
}

public static void UseCustomSwaggerMildd(this IApplicationBuilder app)
public static void UseCustomSwaggerMildd(this IApplicationBuilder app, Func<Stream> streamHtml)
{
if (app == null) throw new ArgumentNullException(nameof(app));

var apis = new List<string> { "blog-svc" };
app.UseMvc().UseSwagger();
app.UseSwaggerUI(options =>
app.UseSwagger();
app.UseSwaggerUI(c =>
{
options.SwaggerEndpoint($"/swagger/v1/swagger.json", $"Blog.Core.Gateway-v1");

c.SwaggerEndpoint($"/swagger/v1/swagger.json", "gateway");
apis.ForEach(m =>
{
options.SwaggerEndpoint($"/swagger/apiswg/{m}/swagger.json", m);
options.IndexStream = () => app.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Blog.Core.ApiGateway.index.html");
c.SwaggerEndpoint($"/swagger/apiswg/{m}/swagger.json", m);
});

options.RoutePrefix = "";

if (streamHtml.Invoke() == null)
{
var msg = "index.html的属性,必须设置为嵌入的资源";
throw new Exception(msg);
}

c.IndexStream = streamHtml;

c.RoutePrefix = "";
});
}

Expand Down
38 changes: 3 additions & 35 deletions Blog.Core.Gateway/Helper/CustomJwtTokenAuthMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using System;
using System.Net;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Blog.Core.Common;
using Blog.Core.Common.Caches;
using Blog.Core.Common.Helper;
using Nacos.V2;
using Newtonsoft.Json.Linq;

namespace Blog.Core.AuthHelper
{
Expand All @@ -23,7 +16,6 @@ public class CustomJwtTokenAuthMiddleware
{
private readonly ICaching _cache;

private readonly INacosNamingService NacosServClient;

/// <summary>
/// 验证方案提供对象
Expand All @@ -36,13 +28,11 @@ public class CustomJwtTokenAuthMiddleware
private readonly RequestDelegate _next;


public CustomJwtTokenAuthMiddleware(INacosNamingService serv, RequestDelegate next, IAuthenticationSchemeProvider schemes, AppSettings appset,ICaching cache)
public CustomJwtTokenAuthMiddleware(RequestDelegate next, IAuthenticationSchemeProvider schemes, AppSettings appset,ICaching cache)
{
NacosServClient = serv;
_cache = cache;
_next = next;
Schemes = schemes;
List<PermissionItem> Permissions = _cache.Cof_AsyncGetICaching<List<PermissionItem>>("Permissions", GetPermitionData, 10).GetAwaiter().GetResult();
}

/// <summary>
Expand All @@ -66,7 +56,7 @@ public async Task Invoke(HttpContext httpContext)
return;
}

List<PermissionItem> Permissions= await _cache.Cof_AsyncGetICaching<List<PermissionItem>>("Permissions", GetPermitionData, 10);
List<PermissionItem> Permissions= new();

httpContext.Features.Set<IAuthenticationFeature>(new AuthenticationFeature
{
Expand Down Expand Up @@ -126,28 +116,6 @@ public async Task Invoke(HttpContext httpContext)
await _next.Invoke(httpContext);
}

private async Task<List<PermissionItem>> GetPermitionData()
{
try
{
string PermissionServName = AppSettings.GetValue("ApiGateWay:PermissionServName");
string PermissionServGroup = AppSettings.GetValue("ApiGateWay:PermissionServGroup");
string PermissionServUrl = AppSettings.GetValue("ApiGateWay:PermissionServUrl");

string requestdata = await NacosServClient.Cof_NaoceGet(PermissionServName, PermissionServGroup, PermissionServUrl);
if (string.IsNullOrEmpty(requestdata)) return null;
JToken perJt = JToken.Parse(requestdata);
if(perJt["response"]!=null) return perJt["response"].ToObject<List<PermissionItem>>();
return perJt["data"].ToObject<List<PermissionItem>>();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

return null;
}

/// <summary>
/// 返回相应
/// </summary>
Expand Down
147 changes: 0 additions & 147 deletions Blog.Core.Gateway/Helper/OcelotConfigurationTask.cs

This file was deleted.

Loading

0 comments on commit 5f132f0

Please sign in to comment.