微服務網關從零搭建——(四)添加多個服務(須要被身份驗證)的寫法

準備工做 

搭建DemoApi_III 過程和第一篇中的DemoApi_I 一致json

惟一不一樣在於 appsettings.json 內的 ServiceName 改成demoAPi3  而後 Port  改成 100三、api

修改網關配置

configuration.json 內的 ReRoutes添加節點app

 // API:demo3
    {
      "UseServiceDiscovery": true,
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "http",
      "ServiceName": "demoAPi3",
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      },
      "UpstreamPathTemplate": "/demo3/{url}",
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "ReRoutesCaseSensitive": false, // non case sensitive
      //添加身份驗證
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "OcelotKey3",
        "AllowedScopes": [ "demoAPi3" ]
      }
    }
節點

修改 Startup.cs ide

  //添加第二個身份驗證
                .AddIdentityServerAuthentication("OcelotKey3", options =>
                {
                    options.Authority = Configuration.GetSection("Setting")["AuthUrl"];
                    options.ApiName = "demoAPi3";
                    options.SupportedTokens = SupportedTokens.Both;
                    options.RequireHttpsMetadata = false;
                })
添加節點

如圖所示:測試

此處 未考慮優化配置 demo級別優化

注意 :ui

1.apiName內的內容區分大小寫 必須和身份驗證服務內定義的ApiResources的APIName 名稱一致url

2.例子中第二個身份驗證中的ocelotkey3  必須和第一個不一樣 即  這個值必須與以前定義的不一樣spa

且須要和configuration.json 中 此處的key值相同code

 身份驗證服務修改

 

完成後便可

測試

相關文章
相關標籤/搜索