设为首页 加入收藏

TOP

Swagger如何访问Ocelot中带权限验证的API(一)
2019-09-17 18:09:44 】 浏览:53
Tags:Swagger 如何 访问 Ocelot 权限 验证 API

先亮源代码:https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/SwaggerDemo

这篇博文不是对asp.net core中使用Swagger作介绍,因为社区博客作了详细说明。

今天主要说一下Swagger在Ocelot网关权限验证模式下的访问,以及Swagger请求应答的数据格式。

首先创建四个项目:

SwaggerOcelot:asp.net core web api类型,api网关项目

SwaggerAuthorize:asp.net core web api类型,用户验证项目

SwaggerAPI01:asp.net core web api类型,api 1项目

SWaggerAPI02:asp.net core web api类型,api 2项目

首先在四个项目中添加基于Jwt的Toekn认证,参见https://www.cnblogs.com/axzxs2001/p/9250588.html

再在四个项目Nuget中引入Swashbuckle.AspNetCore,我的Demo中用的是2.5.0,再分别配置Swagger

 SwaggerAuthorize  Starup.cs配置

 1 public void ConfigureServices(IServiceCollection services)
 2 {
 3     services.AddTokenJwtAuthorize();
 4     services.AddMvc()
 5             .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
 6     services.AddSwaggerGen(options =>
 7     {
 8         options.SwaggerDoc("SwaggerAuthorize", new Info { Title = "Authorize", Version = "v1", Contact = new Contact { Email = "285130205@qq.com", Name = "Authorize", Url = "http://0.0.0.0" }, Description = "Authorize项目" });
 9         var basePath = PlatformServices.Default.Application.ApplicationBasePath;
10         var xmlPath = Path.Combine(basePath, "SwaggerAuthorize.xml");
11         options.IncludeXmlComments(xmlPath);
12     });
13 }
14 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
15 {
16     if (env.IsDevelopment())
17     {
18         app.UseDeveloperExceptionPage();
19     }
20  
21     app.UseMvc()
22         .UseSwagger(options =>
23         {
24             options.RouteTemplate = "{documentName}/swagger.json";
25         })
26         .UseSwaggerUI(options =>
27         {
28             options.SwaggerEndpoint("/SwaggerAuthorize/swagger.json", "Authorize");
29         });
30 }

SwaggerAPI01,SwaggerAPI02类似,Starup.cs配置,其中让Swagger支付Token验证,就是要在这部分添加Swagger配置

 1 public void ConfigureServices(IServiceCollection services)
 2 {
 3     services.AddApiJwtAuthorize((context) =>
 4     {
 5         return true;
 6     });
 7  
 8     services.AddSwaggerGen(options =>
 9     {
10         options.SwaggerDoc("SwaggerAPI01", new Info { Title = "API01", Version = "v1", Contact = new Contact { Email = "285130205@qq.com", Name = "API01", Url = "http://0.0.0.0" }, Description = "API01项目" });
11         var basePath = PlatformServices.Default.Application.ApplicationBasePath;
12         var xmlPath = Path.Combine(basePath, "SwaggerAPI01.xml");
13         options.IncludeXmlComments(xmlPath);
14  
15         //这里是给Swagger添加验证的部分
16         options.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "请输入带有Bearer的Token", Name = "Authorization", Type = "apiKey" });
17         options.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
18             {
19                 "Bearer",
20                 Enumerable.Empty<string>()
21             }
22         });
23     });
24     services
25         .AddMvc()
26         .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
27 }
28  
29 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
30 {
31     app.UseMvc()
32         .UseSwagger(options =>
33         {
34             options.RouteTemplate = "{documentName}/swagger.j
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ocelot.JwtAuthorize:一个基于网.. 下一篇入门设计模式之适配器模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目