From 42c0be8093b78acafb4aa95aefb85d44ec47b505 Mon Sep 17 00:00:00 2001 From: Argo Windows Date: Fri, 18 Oct 2019 22:03:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20swagger=20=E5=A2=9E=E5=8A=A0=20Authoriz?= =?UTF-8?q?ation=20=E5=A4=B4=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Bootstrap.Admin/Bootstrap.Admin.csproj | 2 +- .../Controllers/Api/LoginController.cs | 3 +- .../Extensions/HttpHeaderOperation.cs | 31 ----------------- .../Extensions/SwaggerExtensions.cs | 33 ++++++++++++++----- src/admin/Bootstrap.Admin/Startup.cs | 2 +- .../Bootstrap.Client/Bootstrap.Client.csproj | 2 +- src/client/Bootstrap.Client/Startup.cs | 2 +- .../Api/HttpHeaderOperationTest.cs | 21 ------------ 8 files changed, 30 insertions(+), 66 deletions(-) delete mode 100644 src/admin/Bootstrap.Admin/Extensions/HttpHeaderOperation.cs delete mode 100644 test/UnitTest/Bootstrap.Admin/Api/HttpHeaderOperationTest.cs diff --git a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj index 699ee993..64ca91b4 100644 --- a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs index 5e5eae8f..5aaea6cf 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs @@ -1,5 +1,6 @@ using Bootstrap.Admin.Query; using Bootstrap.DataAccess; +using Bootstrap.Security.Authentication; using Longbow.Web.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -38,7 +39,7 @@ namespace Bootstrap.Admin.Controllers.Api string password = user.Password; if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password)) { - token = "NOT Support yet!"; // BootstrapAdminJwtTokenHandler.CreateToken(userName); + token = BootstrapAdminJwtTokenHandler.CreateToken(userName); } HttpContext.Log(userName, token != null); return token; diff --git a/src/admin/Bootstrap.Admin/Extensions/HttpHeaderOperation.cs b/src/admin/Bootstrap.Admin/Extensions/HttpHeaderOperation.cs deleted file mode 100644 index f479233d..00000000 --- a/src/admin/Bootstrap.Admin/Extensions/HttpHeaderOperation.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Microsoft.AspNetCore.Authorization; -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; -using System.Collections.Generic; - -namespace Bootstrap.Admin -{ - /// - /// IOperationFilter 实现类 - /// - public class HttpHeaderOperation : IOperationFilter - { - /// - /// 应用方法 增加 Authorization 授权头设置 - /// - /// - /// - public void Apply(OpenApiOperation operation, OperationFilterContext context) - { - if (context.MethodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 0) - { - operation.Parameters.Add(new OpenApiParameter() - { - Name = "Authorization", //添加Authorization头部参数 - In = ParameterLocation.Header, - Required = false - }); - } - } - } -} diff --git a/src/admin/Bootstrap.Admin/Extensions/SwaggerExtensions.cs b/src/admin/Bootstrap.Admin/Extensions/SwaggerExtensions.cs index 98ad918e..337ff6a1 100644 --- a/src/admin/Bootstrap.Admin/Extensions/SwaggerExtensions.cs +++ b/src/admin/Bootstrap.Admin/Extensions/SwaggerExtensions.cs @@ -19,14 +19,6 @@ namespace Bootstrap.Admin /// 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 => { @@ -51,7 +43,30 @@ namespace Bootstrap.Admin //Set the comments path for the swagger json and ui. var xmlPath = Path.Combine(AppContext.BaseDirectory, "Bootstrap.Admin.xml"); options.IncludeXmlComments(xmlPath); - options.OperationFilter(); // 添加httpHeader参数 + + options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + Name = "Authorization", + Type = SecuritySchemeType.ApiKey, + Scheme = "Bearer", + BearerFormat = "JWT", + In = ParameterLocation.Header, + Description = "JWT Authorization header using the Bearer scheme." + }); + options.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + new string[]{ } + } + }); }); } } diff --git a/src/admin/Bootstrap.Admin/Startup.cs b/src/admin/Bootstrap.Admin/Startup.cs index d6efee4b..f733c896 100644 --- a/src/admin/Bootstrap.Admin/Startup.cs +++ b/src/admin/Bootstrap.Admin/Startup.cs @@ -107,7 +107,7 @@ namespace Bootstrap.Admin app.UseStaticFiles(); app.UseAutoGenerateDatabase(); app.UseRouting(); - app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); + app.UseBootstrapAdminAuthentication("", RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseAuthorization(); app.UseSwagger(Configuration["SwaggerPathBase"].TrimEnd('/')); app.UseOnlineUsers(TraceHelper.Filter, TraceHelper.Save); diff --git a/src/client/Bootstrap.Client/Bootstrap.Client.csproj b/src/client/Bootstrap.Client/Bootstrap.Client.csproj index d20a4c14..40292255 100644 --- a/src/client/Bootstrap.Client/Bootstrap.Client.csproj +++ b/src/client/Bootstrap.Client/Bootstrap.Client.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/client/Bootstrap.Client/Startup.cs b/src/client/Bootstrap.Client/Startup.cs index 07eaccb1..57750501 100644 --- a/src/client/Bootstrap.Client/Startup.cs +++ b/src/client/Bootstrap.Client/Startup.cs @@ -86,7 +86,7 @@ namespace Bootstrap.Client app.UseCookiePolicy(); app.UseRouting(); - app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); + app.UseBootstrapAdminAuthentication(Configuration.GetValue("AppId", "2"), RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseAuthorization(); app.UseCacheManager(); app.UseOnlineUsers(callback: TraceHelper.Save); diff --git a/test/UnitTest/Bootstrap.Admin/Api/HttpHeaderOperationTest.cs b/test/UnitTest/Bootstrap.Admin/Api/HttpHeaderOperationTest.cs deleted file mode 100644 index 12b34cec..00000000 --- a/test/UnitTest/Bootstrap.Admin/Api/HttpHeaderOperationTest.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.AspNetCore.Mvc.ApiExplorer; -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; -using Xunit; - -namespace Bootstrap.Admin.Api -{ - public class HttpHeaderOperationTest - { - [Fact] - public void Apply_Ok() - { - var oper = new HttpHeaderOperation(); - var api = new OpenApiOperation(); - var desc = new ApiDescription(); - var mi = typeof(HttpHeaderOperationTest).GetMethod("Apply_Ok"); - var context = new OperationFilterContext(desc, null, null, mi); - oper.Apply(api, context); - } - } -}