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.
52 lines
2.0 KiB
52 lines
2.0 KiB
3 years ago
|
using Microsoft.AspNetCore.Authorization;
|
||
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||
|
using Microsoft.OpenApi.Models;
|
||
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
|
||
|
namespace learun.webapi
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 版 本 Learun-ADMS-Core 力软管理后台
|
||
|
/// Copyright (c) 2019-present 力软信息技术(苏州)有限公司
|
||
|
/// 创建人:tobin
|
||
|
/// 日 期:2020.04.17
|
||
|
/// 描 述:给api添加权限验证信息参数
|
||
|
/// </summary>
|
||
|
|
||
|
public class AddAuthTokenHeaderParameter : IOperationFilter
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 给接口添加隐性参数
|
||
|
/// </summary>
|
||
|
/// <param name="operation">api操作接口</param>
|
||
|
/// <param name="context">上下文</param>
|
||
|
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||
|
{
|
||
|
//var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;
|
||
|
//先判断是否是匿名访问,
|
||
|
var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
|
||
|
if (descriptor != null)
|
||
|
{
|
||
|
var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);
|
||
|
bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
|
||
|
//非匿名的方法,链接中添加accesstoken值
|
||
|
if (!isAnonymous)
|
||
|
{
|
||
|
if (operation.Parameters == null) operation.Parameters = new List<OpenApiParameter>();
|
||
|
operation.Parameters.Add(new OpenApiParameter()
|
||
|
{
|
||
|
Name = "token",
|
||
|
Description = "登录密钥",
|
||
|
In = ParameterLocation.Header,
|
||
|
Schema = new OpenApiSchema() { Type = "string" },
|
||
|
Required = true //是否必选
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|