You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using zzz.Common;
using log4net;
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.Runtime.InteropServices;
using zzz.AOP;
namespace zzz.Extensions
{
/// <summary>
/// Swagger 启动服务
/// </summary>
public static class SwaggerSetup
{
private static readonly ILog log = LogManager.GetLogger(typeof(SwaggerSetup));
public static void AddSwaggerSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
var basePath = AppContext.BaseDirectory;
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo()
{
Title = "后台Api文档",
Version = "1.0.0",
Description = "描述~~"
});
c.UseInlineDefinitionsForEnums();
try
{
//
var xmlPath = Path.Combine(basePath, "zzz.Api.xml");
c.IncludeXmlComments(xmlPath, true);
//这个就是Model层的xml文件名
var xmlModelPath = Path.Combine(basePath, "zzz.Model.xml");
c.IncludeXmlComments(xmlModelPath);
//这个就是Services层的xml文件名
var xmlServicesPath = Path.Combine(basePath, "zzz.Services.xml");
c.IncludeXmlComments(xmlServicesPath);
}
catch (Exception ex)
{
log.Error("zzz.xml和zzz.Model.xml 丢失,请检查并拷贝。\n" + ex.Message);
}
// 开启加权小锁
c.OperationFilter<AddResponseHeadersFilter>();
c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
// 在header中添加token传递到后台
c.OperationFilter<SecurityRequirementsOperationFilter>();
// Jwt Bearer 认证,必须是 oauth2
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"",
Name = "Authorization",//jwt默认的参数名称
In = ParameterLocation.Header,//jwt默认存放Authorization信息的位置(请求头中)
Type = SecuritySchemeType.ApiKey
});
});
services.AddSwaggerGenNewtonsoftSupport();
}
}
}