zoukankan      html  css  js  c++  java
  • 微服务网关从零搭建——(四)添加多个服务(需要被身份验证)的写法

    准备工作 

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

    唯一不同在于 appsettings.json 内的 ServiceName 改为demoAPi3  然后 Port  改为 1003、

    修改网关配置

    configuration.json 内的 ReRoutes添加节点

     // 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 

      //添加第二个身份验证
                    .AddIdentityServerAuthentication("OcelotKey3", options =>
                    {
                        options.Authority = Configuration.GetSection("Setting")["AuthUrl"];
                        options.ApiName = "demoAPi3";
                        options.SupportedTokens = SupportedTokens.Both;
                        options.RequireHttpsMetadata = false;
                    })
    添加节点

    如图所示:

    此处 未考虑优化配置 demo级别

    注意 :

    1.apiName内的内容区分大小写 必须和身份验证服务内定义的ApiResources的APIName 名称一致

    2.例子中第二个身份验证中的ocelotkey3  必须和第一个不同 即  这个值必须与之前定义的不同

    且需要和configuration.json 中 此处的key值相同

     身份验证服务修改

    完成后即可

    测试

  • 相关阅读:
    学习Python的一些Tips
    读书笔记《深入理解计算机系统》(第三版) 第二章 信息的表示
    读书笔记《深入理解计算机系统》(第三版) 第一章 计算机系统漫游
    C 标准库系列之locale.h
    C 标准库系列之limits.h
    C 标准库系列之float.h
    C 标准库系列之errno.h
    C 标准库系列之ctype.h
    C 标准库系列之assert.h
    C 标准库系列之概述
  • 原文地址:https://www.cnblogs.com/nontracey/p/9968317.html
Copyright © 2011-2022 走看看