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.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

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

View File

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

View File

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