|
@@ -2,6 +2,7 @@
|
|
|
// <date></date>
|
|
|
// <description></description>
|
|
|
|
|
|
+using ERP.Framework.Constants;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
@@ -20,10 +21,27 @@ namespace ERP.Framework.Extensions
|
|
|
{
|
|
|
services.AddSwaggerGen(options =>
|
|
|
{
|
|
|
+ var openApiSecurityScheme = new OpenApiSecurityScheme
|
|
|
+ {
|
|
|
+ Reference = new OpenApiReference
|
|
|
+ {
|
|
|
+ Type = ReferenceType.SecurityScheme,
|
|
|
+ Id = AuthConstant.HEADER
|
|
|
+ },
|
|
|
+ Scheme = "oauth2",
|
|
|
+ Name = AuthConstant.HEADER,
|
|
|
+ In = ParameterLocation.Header,
|
|
|
+ Type = SecuritySchemeType.ApiKey
|
|
|
+ };
|
|
|
+
|
|
|
+ var securityRequirement = new OpenApiSecurityRequirement { [openApiSecurityScheme] = new List<string>() };
|
|
|
+
|
|
|
// 设置 Swagger UI 的标题和版本 (描述,联系人,许可证)
|
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "ERP", Version = "v1" });
|
|
|
// 安全方案
|
|
|
- //options.AddSecurityDefinition()
|
|
|
+ options.AddSecurityDefinition(AuthConstant.HEADER, openApiSecurityScheme);
|
|
|
+ // 请求头
|
|
|
+ options.AddSecurityRequirement(securityRequirement);
|
|
|
// 自定义逻辑
|
|
|
options.OperationFilter<SwaggerDescriptionFilter>();
|
|
|
options.OperationFilter<SwaggerSummaryFilter>();
|
|
@@ -57,8 +75,8 @@ namespace ERP.Framework.Extensions
|
|
|
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
|
|
{
|
|
|
var attribute = context.MethodInfo
|
|
|
- .GetCustomAttributes(typeof(SwaggerOperationAttribute), false)
|
|
|
- .FirstOrDefault() as SwaggerOperationAttribute;
|
|
|
+ .GetCustomAttributes(typeof(SwaggerOperationAttribute), false)
|
|
|
+ .FirstOrDefault() as SwaggerOperationAttribute;
|
|
|
|
|
|
if (attribute != null && !string.IsNullOrEmpty(attribute.Description))
|
|
|
{
|