我使用由我的应用程序的身份验证服务生成的令牌。没有问题。现在我已经介绍了Swashbuckle以记录我的API,我可以通过使用此代码向JWT发送每个请求来进行身份验证;
services.AddSwaggerGen(c =>
{
var a = new ApiKeyScheme();
//c.AddSecurityDefinition("Bearer", new ApiKeyScheme()
//{ In = "header", Description = "Please insert JWT with Bearer into field", Name = "Authorization", Type = "apiKey" });
c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
c.SwaggerDoc("v2", new Info
{
Version = "v2",
Title = "MyTitle",
Description = "An interface for ...",
TermsOfService = "None",
Contact = new Contact() { Name = "MyApp", Email = "a@example.com", Url = "www.example.com" }
});
// Set the comments path for the Swagger JSON and UI.
var basePath = AppContext.BaseDirectory;
var xmlPath = Path.Combine(basePath, "cpDataCore.xml");
c.IncludeXmlComments(xmlPath);
});
public class AuthorizationHeaderParameterOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthorizeFilter);
var allowAnonymous = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is IAllowAnonymousFilter);
if (isAuthorized && !allowAnonymous)
{
if (operation.Parameters == null)
operation.Parameters = new List<IParameter>();
operation.Parameters.Add(new NonBodyParameter
{
Name = "Authorization",
In = "header",
Description = "access token",
Required = true,
Type = "string"
});
}
}
}
这给了我以下标题 - 正如预期的那样
accept:application/json
Accept-Encoding:gzip, deflate, br
Accept-Language:en-AU,en;q=0.9
Authorization:Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9naXZlbm5hbWUiOiJEZW5uaXMiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9zdXJuY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1IiwiUmVmcmVzaFRva2VuIjoiMTNhNzRmNDQtNmVmOC00MDQ3LTlmYWYtOWQ3MzI4MmNhZjQ4IiwiUHJvamVjdElEIjoiLTEiLCJuYmYiOjE1MDUwOTc3MjEsImV4cCI6MTUwNTA5ODYyMSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo2MDAwMC8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjYwMDAwLyJ9.8You0XiUlvdHb2TRuDzaiXv6r74v7ga1Av_Z3ikmblU
Host:localhost:60000
Referer:http://localhost:60000/swagger/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36
虽然,我不确定Cookie的来源。这与我的代码无关。我只是忽略它 - 到目前为止一直很好。
问题在于这意味着必须在每个请求中输入令牌,这是一个痛苦。理想情况下,我想使用内置的swagger接口进行身份验证 - 根据几篇文章,我应该能够做到这一点;
c.AddSecurityDefinition("Bearer", new ApiKeyScheme()
{ In = "header", Description = "Please insert JWT with Bearer into field", Name = "Authorization", Type = "apiKey" });
这很好用,我可以添加令牌,似乎是我缺少的一个步骤,将令牌添加到每个请求的标头中。如果我只是添加auth,那么这会给我以下标题,这当然不能通过身份验证。
GET /api/ApprovalItemTypes HTTP/1.1
Host: localhost:60000
Connection: keep-alive
accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36
Referer: http://localhost:60000/swagger/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-AU,en;q=0.9
Cookie: username=demo; jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW5_xxx__LTEiLCJuYmYiOjE1MDUwOTc3MjEsImV4cCI6MTUwNTA5ODYyMSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo2MDAwMC8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjYwMDAwLyJ9.8You0XiUlvdHb2TRuDzaiXv6r74v7ga1Av_Z3ikmblU
为了获得为每个后续请求包含令牌的请求,我还需要做些什么?
答案 0 :(得分:1)
如果您在方法上指定了过滤器,则Swagger会添加授权标头。如果你在全球范围内需要授权,我的猜测就是招摇不能识别它们。
您需要在ConfigureServices中添加类似这样的SecurityRequirement:
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>()
{
{ "Bearer", new string[]{ } }
});
如果设置了令牌,则需要使用每个请求发送标头。如果您在未发送标题之前没有设置标题,但您仍然会在api说明旁边显示挂锁标记。
答案 1 :(得分:0)
如果您在代码中定义
If tblHotels.Rows.Count = 0 Then
,然后在.Parameters中而不是.Security中使用它
import rootReducer from './reducers/index';
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
export function configureStore(){
const store = createStore(
rootReducer,
compose(
applyMiddleware(thunk),
window.__REDUX_DEVTOOLS_EXTENSION__ ? window.__REDUX_DEVTOOLS_EXTENSION__() : f => f
));
return store;
}
然后一切正常:
我的操作与您相同,但是您应该添加如下内容(对于oauth2或jwt承载令牌auth):
c.AddSecurityDefinition("jwt", new ApiKeyScheme()
{
In = "header", Description = "Please insert JWT with Bearer into field", Name = "Authorization", Type = "apiKey" });
用法:
operation.Security = new List<IDictionary<string, IEnumerable<string>>> {
new Dictionary<string, IEnumerable<string>>
{
{"jwt", _scopes }
}
答案 2 :(得分:0)
最后,我搬到了NSwag,所以我不确定最初的问题是什么。 Nswag中的解决方案如下;
services.AddSwaggerDocument(document =>
{
document.DocumentName = "CPSwagger";
document.Title = "My Mobile API";
document.Version = "v10.3.4";
document.Description = "An interface for some software.";
document.DocumentProcessors.Add(
new SecurityDefinitionAppender("JWT token", new NSwag.OpenApiSecurityScheme
{
Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
Name = "Authorization",
Description = "Copy 'Bearer ' + valid JWT token into field",
In = NSwag.OpenApiSecurityApiKeyLocation.Header
}));
document.OperationProcessors.Add(new OperationSecurityScopeProcessor("JWT token"));
});