feat: 增加 swagger 支持 NETCore 3.0

This commit is contained in:
Argo Zhang 2019-09-28 20:59:51 +08:00
parent fc59d26725
commit 260b9b0481
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
4 changed files with 89 additions and 89 deletions

View File

@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc3" />
</ItemGroup>
<ItemGroup>

View File

@ -1,33 +1,33 @@
//using Microsoft.AspNetCore.Authorization;
//using Microsoft.OpenApi.Models;
//using Swashbuckle.AspNetCore.SwaggerGen;
//using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
//namespace Bootstrap.Admin
//{
// /// <summary>
// ///
// /// </summary>
// public class HttpHeaderOperation : IOperationFilter
// {
// /// <summary>
// ///
// /// </summary>
// /// <param name="operation"></param>
// /// <param name="context"></param>
// public void Apply(OpenApiOperation operation, OperationFilterContext context)
// {
// if (operation.Parameters == null) operation.Parameters = new List<OpenApiParameter>();
namespace Bootstrap.Admin
{
/// <summary>
/// IOperationFilter 实现类
/// </summary>
public class HttpHeaderOperation : IOperationFilter
{
/// <summary>
/// 应用方法 增加 Authorization 授权头设置
/// </summary>
/// <param name="operation"></param>
/// <param name="context"></param>
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.Parameters == null) operation.Parameters = new List<OpenApiParameter>();
// if (context.MethodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 0)
// {
// operation.Parameters.Add(new OpenApiParameter()
// {
// Name = "Authorization", //添加Authorization头部参数
// In = ParameterLocation.Header,
// Required = false
// });
// }
// }
// }
//}
if (context.MethodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 0)
{
operation.Parameters.Add(new OpenApiParameter()
{
Name = "Authorization", //添加Authorization头部参数
In = ParameterLocation.Header,
Required = false
});
}
}
}
}

View File

@ -64,7 +64,7 @@ namespace Bootstrap.Admin
services.AddResponseCompression();
services.AddBootstrapAdminAuthentication().AddGitee(OAuthHelper.Configure).AddGitHub(OAuthHelper.Configure);
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireBootstrapAdminAuthorizate().Build());
//services.AddSwagger();
services.AddSwagger();
services.AddButtonAuthorization(MenuHelper.AuthorizateButtons);
services.AddBootstrapAdminBackgroundTask();
services.AddHttpClient<GiteeHttpClient>();
@ -115,10 +115,10 @@ namespace Bootstrap.Admin
app.UseAuthentication();
app.UseAuthorization();
app.UseBootstrapAdminAuthorization(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName);
app.UseSwagger(Configuration["SwaggerPathBase"].TrimEnd('/'));
app.UseBootstrapHealthChecks();
app.UseOnlineUsers(TraceHelper.Filter, TraceHelper.Save);
app.UseCacheManager();
//app.UseSwagger(Configuration["SwaggerPathBase"].TrimEnd('/'));
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<SignalRHub>("/NotiHub");

View File

@ -1,59 +1,58 @@
//using Microsoft.AspNetCore.Authentication;
//using Microsoft.AspNetCore.Builder;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.OpenApi.Models;
//using Swashbuckle.AspNetCore.Swagger;
//using System;
//using System.IO;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
//namespace Bootstrap.Admin
//{
// /// <summary>
// ///
// /// </summary>
// internal static class SwaggerExtensions
// {
// /// <summary>
// ///
// /// </summary>
// /// <param name="app"></param>
// /// <param name="pathBase"></param>
// public static void UseSwagger(this IApplicationBuilder app, string pathBase)
// {
// app.UseWhen(context => context.Request.Path.StartsWithSegments("/swagger"), builder =>
// {
// builder.Use(async (context, next) =>
// {
// if (!context.User.Identity.IsAuthenticated) await context.ChallengeAsync();
// else await next();
// });
// });
// app.UseSwagger();
// app.UseSwaggerUI(c =>
// {
// c.SwaggerEndpoint($"{pathBase}/swagger/v1/swagger.json", "BootstrapAdmin API V1");
// });
// }
namespace Bootstrap.Admin
{
/// <summary>
/// Swagger 扩展方法
/// </summary>
internal static class SwaggerExtensions
{
/// <summary>
/// Swagger 中间件
/// </summary>
/// <param name="app"></param>
/// <param name="pathBase"></param>
public static void UseSwagger(this IApplicationBuilder app, string pathBase)
{
app.UseWhen(context => context.Request.Path.StartsWithSegments("/swagger"), builder =>
{
builder.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated) await context.ChallengeAsync();
else await next();
});
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{pathBase}/swagger/v1/swagger.json", "BootstrapAdmin API V1");
});
}
// /// <summary>
// ///
// /// </summary>
// /// <param name="services"></param>
// public static void AddSwagger(this IServiceCollection services)
// {
// services.AddSwaggerGen(options =>
// {
// options.SwaggerDoc("v1", new OpenApiInfo
// {
// Version = "v1",
// Title = "BootstrapAdmin API"
// });
/// <summary>
/// 注入 Swagger 服务到容器内
/// </summary>
/// <param name="services"></param>
public static void AddSwagger(this IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "BootstrapAdmin API"
});
// //Set the comments path for the swagger json and ui.
// var xmlPath = Path.Combine(AppContext.BaseDirectory, "Bootstrap.Admin.xml");
// options.IncludeXmlComments(xmlPath);
// options.OperationFilter<HttpHeaderOperation>(); // 添加httpHeader参数
// });
// }
// }
//}
//Set the comments path for the swagger json and ui.
var xmlPath = Path.Combine(AppContext.BaseDirectory, "Bootstrap.Admin.xml");
options.IncludeXmlComments(xmlPath);
options.OperationFilter<HttpHeaderOperation>(); // 添加httpHeader参数
});
}
}
}