问题描述
我有3个API被swagger记录了下来。 Api1 使用不记名令牌。 Api2 使用两个ApiKey, Api3 使用一个ApiKey
我有我的SecureDefinition设置:
c.AddSecurityDefinition(SwaggerConstants.SECURITY_DEFINITION_APIID,new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.ApiKey,In = ParameterLocation.Header,Name = Constants.ApiSecurity.ApiIdHeaderName,Description = Constants.ApiSecurity.ApiIdHeaderName,});
c.AddSecurityDefinition(SwaggerConstants.SECURITY_DEFINITION_APIKEY,Name = Constants.ApiSecurity.ApiKeyHeaderName,Description = Constants.ApiSecurity.ApiKeyHeaderName,});
c.AddSecurityDefinition(SwaggerConstants.SECURITY_DEFINITION_ANTIFORGERY,new OpenApiSecurityScheme
{
Type = SecuritySchemeType.ApiKey,Name = Constants.AntiForgeryHeaderName,In = ParameterLocation.Header
});
c.AddSecurityDefinition(SwaggerConstants.SECURITY_DEFINITION_BEARER,new OpenApiSecurityScheme
{
Description = "Bearer token",BearerFormat = "Must be prefixed with 'Bearer '",Type = SecuritySchemeType.Http,Scheme = "bearer",});
和我的SecureDefinitionFilter(使用GroupName对API进行分组。不包括此逻辑):
public class SecurityDefinitionFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation,OperationFilterContext context)
{
if (context.ApiDescription.GroupName.Equals(SwaggerConstants.API1,StringComparison.OrdinalIgnoreCase))
{
var bearerScheme = new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,Id = SwaggerConstants.SECURITY_DEFINITION_BEARER
}
};
operation.Security = new List<OpenApiSecurityRequirement>
{
{
new OpenApiSecurityRequirement()
{
{ bearerScheme,new List<string>() }
}
}
};
}
else if (context.ApiDescription.GroupName.Equals(SwaggerConstants.API2,StringComparison.OrdinalIgnoreCase))
{
var apiIdScheme = new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,Id = SwaggerConstants.SECURITY_DEFINITION_APIID
}
};
var apiKeyScheme = new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,Id = SwaggerConstants.SECURITY_DEFINITION_APIKEY
}
};
operation.Security = new List<OpenApiSecurityRequirement>
{
{
new OpenApiSecurityRequirement()
{
{ apiIdScheme,new List<string>() }
}
},{
new OpenApiSecurityRequirement()
{
{ apiKeyScheme,new List<string>() }
}
}
};
}
else
{
var tokenScheme = new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,Id = SwaggerConstants.SECURITY_DEFINITION_ANTIFORGERY
}
};
operation.Security = new List<OpenApiSecurityRequirement>()
{
{
new OpenApiSecurityRequirement()
{
{
tokenScheme,new List<string>()
}
}
}
};
}
}
}
一切正常,除了我始终在auth模态上具有所有授权选项。有没有办法只显示可用于API的授权选项?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)