feat:ocelot swagger

This commit is contained in:
anjoy8 2022-02-05 12:10:50 +08:00
parent 615f2338a0
commit b9dc6deb7a
7 changed files with 83 additions and 82 deletions

View File

@ -273,7 +273,7 @@ namespace Blog.Core.Controllers
/// <param name="loginRequest"></param>
/// <returns></returns>
[HttpPost]
[Route("swgLogin")]
[Route("/api/Login/swgLogin")]
public dynamic SwgLogin([FromBody] SwaggerLoginRequest loginRequest)
{
// 这里可以查询数据库等各种校验

View File

@ -7,7 +7,7 @@ using System.Linq;
namespace Blog.Core.Gateway.Controllers
{
[Authorize(Permissions.GWName)]
[Authorize(AuthenticationSchemes = Permissions.GWName)]
[Route("/gateway/[controller]/[action]")]
public class UserController : ControllerBase
{

View File

@ -0,0 +1,48 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace Blog.Core.AdminMvc
{
public class BlogAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public BlogAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock) : base(options, logger, encoder, clock)
{
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
var claims = new List<Claim>()
{
new Claim("gw", "gw")
};
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, Scheme.Name));
var ticket = new AuthenticationTicket(principal, Scheme.Name);
await Task.CompletedTask;
return AuthenticateResult.Success(ticket);
}
protected virtual string GetTokenStringFromHeader()
{
var token = string.Empty;
string authorization = Request.Headers[HeaderNames.Authorization];
if (!string.IsNullOrEmpty(authorization) && authorization.StartsWith($"Bearer ", StringComparison.OrdinalIgnoreCase))
{
token = authorization["Bearer ".Length..].Trim();
}
return token;
}
}
}

View File

@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using System;
using System.Collections.Generic;
using System.IO;
namespace Blog.Core.Gateway.Extensions
@ -45,11 +46,18 @@ namespace Blog.Core.Gateway.Extensions
{
if (app == null) throw new ArgumentNullException(nameof(app));
var apis = new List<string> { "blog-svc" };
app.UseSwagger();
app.UseSwaggerUI(c =>
app.UseSwaggerUI(options =>
{
c.SwaggerEndpoint($"/swagger/v1/swagger.json", $"Blog.Core.Gateway-v1");
c.RoutePrefix = "";
options.SwaggerEndpoint($"/swagger/v1/swagger.json", $"Blog.Core.Gateway-v1");
apis.ForEach(m =>
{
options.SwaggerEndpoint($"/swagger/apiswg/{m}/swagger.json", m);
});
options.RoutePrefix = "";
});
}

View File

@ -1,6 +1,7 @@
using Blog.Core.Common;
using Blog.Core.Extensions;
using Blog.Core.Gateway.Extensions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
@ -35,10 +36,9 @@ namespace Blog.Core.AdminMvc
services.AddAuthentication_JWTSetup();
services.AddAuthorization(options =>
{
options.AddPolicy("GW", policy => policy.RequireRole("AdminTest").Build());
});
services.AddAuthentication()
.AddScheme<AuthenticationSchemeOptions, BlogAuthenticationHandler>(Permissions.GWName, _ => { });
services.AddCustomSwaggerSetup();

View File

@ -1,45 +1,39 @@
{
"Routes": [
// blog-svc
{
"UpstreamPathTemplate": "/gateway/api/{url}",
"UpstreamHttpMethod": [
"Get",
"Post",
"Put",
"Delete"
],
"UpstreamPathTemplate": "/svc/blog/{url}",
"UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
"DownstreamPathTemplate": "/api/{url}",
"DownstreamPathTemplate": "/svc/blog/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 9291
}
]
},
{
"UpstreamPathTemplate": "/gateway/is4api/{url}",
"UpstreamHttpMethod": [
"Get",
"Post",
"Put",
"Delete"
],
"LoadBalancerOptions": {
"Type": "RoundRobin"
"AuthenticationOptions": {
"AuthenticationProviderKey": "GW"
}
},
"DownstreamPathTemplate": "/is4api/{url}",
// blog-svc-swagger
{
"UpstreamPathTemplate": "/swagger/apiswg/blog-svc/swagger.json",
"UpstreamHttpMethod": [ "GET" ],
"DownstreamPathTemplate": "/swagger/V2/swagger.json",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5004
"Port": 9291
}
]
],
"LoadBalancer": "RoundRobin"
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:9000",

View File

@ -1,52 +1,3 @@
{
"Routes": [
{
"UpstreamPathTemplate": "/gateway/api/{url}",
"UpstreamHttpMethod": [
"Get",
"Post",
"Put",
"Delete"
],
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 9291
}
]
},
{
"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"
}
}
}