优化swagger 接口授权验证
This commit is contained in:
57
ZR.Admin.WebApi/Extensions/SwaggerExtension.cs
Normal file
57
ZR.Admin.WebApi/Extensions/SwaggerExtension.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.Filters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZR.Admin.WebApi.Extensions
|
||||
{
|
||||
public static class SwaggerExtension
|
||||
{
|
||||
public static void AddSwaggerConfig(this IServiceCollection services)
|
||||
{
|
||||
if (services == null) throw new ArgumentNullException(nameof(services));
|
||||
IWebHostEnvironment hostEnvironment = App.GetRequiredService<IWebHostEnvironment>();
|
||||
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo
|
||||
{
|
||||
Title = "ZrAdmin.NET Api - .NET5",
|
||||
Version = "v1",
|
||||
Description = "",
|
||||
});
|
||||
//if (CurrentEnvironment.IsDevelopment())
|
||||
//{
|
||||
//添加文档注释
|
||||
c.IncludeXmlComments(Path.Combine(hostEnvironment.ContentRootPath, "ZRAdmin.xml"), true);
|
||||
//}
|
||||
//参考文章:http://www.zyiz.net/tech/detail-134965.html
|
||||
//需要安装包Swashbuckle.AspNetCore.Filters
|
||||
// 开启权限小锁 需要在对应的Action上添加[Authorize]才能看到
|
||||
c.OperationFilter<AddResponseHeadersFilter>();
|
||||
c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
|
||||
|
||||
//在header 中添加token,传递到后台
|
||||
c.OperationFilter<SecurityRequirementsOperationFilter>();
|
||||
|
||||
c.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme,
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
In = ParameterLocation.Header,
|
||||
Description = "请输入Login接口返回的Token,前置Bearer。示例:Bearer {Token}",
|
||||
Name = "Authorization",//jwt默认的参数名称,
|
||||
Type = SecuritySchemeType.ApiKey, //指定ApiKey
|
||||
BearerFormat = "JWT",//标识承载令牌的格式 该信息主要是出于文档目的
|
||||
Scheme = JwtBearerDefaults.AuthenticationScheme//授权中要使用的HTTP授权方案的名称
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using SqlSugar.IOC;
|
||||
using Swashbuckle.AspNetCore.Filters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
@@ -74,44 +75,19 @@ namespace ZR.Admin.WebApi
|
||||
o.TokenValidationParameters = JwtUtil.ValidParameters();
|
||||
});
|
||||
|
||||
InjectRepositories(services);
|
||||
InjectServices(services);
|
||||
|
||||
services.AddMvc(options =>
|
||||
{
|
||||
options.Filters.Add(typeof(GlobalActionMonitor));//ȫ<><C8AB>ע<EFBFBD><D7A2><EFBFBD>쳣
|
||||
})
|
||||
.AddMvcLocalization()
|
||||
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
|
||||
});
|
||||
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo
|
||||
{
|
||||
Title = "ZrAdmin.NET Api - .NET5",
|
||||
Version = "v1",
|
||||
Description = "",
|
||||
});
|
||||
//if (CurrentEnvironment.IsDevelopment())
|
||||
//{
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ע<EFBFBD><D7A2>
|
||||
c.IncludeXmlComments(Path.Combine(CurrentEnvironment.ContentRootPath, "ZRAdmin.xml"), true);
|
||||
//}
|
||||
c.AddSecurityDefinition("Bearer",
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
In = ParameterLocation.Header,
|
||||
Description = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OAuth<74>ӿڷ<D3BF><DAB7>ص<EFBFBD>Token<65><6E>ǰ<EFBFBD><C7B0>Bearer<65><72>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>Bearer {Token}",
|
||||
Name = "Authorization",//jwtĬ<74>ϵIJ<CFB5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
|
||||
Type = SecuritySchemeType.ApiKey, //ָ<><D6B8>ApiKey
|
||||
BearerFormat = "JWT",//<2F><>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶĸ<C6B5>ʽ <20><><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>Ҫ<EFBFBD>dz<EFBFBD><C7B3><EFBFBD><EFBFBD>ĵ<EFBFBD>Ŀ<EFBFBD><C4BF>
|
||||
Scheme = JwtBearerDefaults.AuthenticationScheme//<2F><>Ȩ<EFBFBD><C8A8>Ҫʹ<D2AA>õ<EFBFBD>HTTP<54><50>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
});
|
||||
});
|
||||
services.AddSwaggerConfig();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
@@ -163,7 +139,7 @@ namespace ZR.Admin.WebApi
|
||||
/// ע<><D7A2>Services<65><73><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
private void InjectRepositories(IServiceCollection services)
|
||||
private void InjectServices(IServiceCollection services)
|
||||
{
|
||||
services.AddAppService();
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
<PackageReference Include="Snowflake.Core" Version="2.0.0" />
|
||||
<PackageReference Include="SqlSugar.IOC" Version="1.7.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.2" />
|
||||
<PackageReference Include="UAParser" Version="3.1.46" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user