上一篇已经构建好了例子,接下来将IdentityServer4添加到Ocelot中去实现

配置一个客户端配置,可以构建一个简单的客户端信息,这里我用的混合模式,配置比较多,对于客户端模式而言实际很多都不需要设置

只需要构如下即可 

  ClientId="liyouming",
   ClientName="ChinaNetCore",
   ClientSecrets={new Secret("liyouming".Sha256()) },
   AllowedGrantTypes= GrantTypes.ClientCredentials,
  AccessTokenType= AccessTokenType.Jwt,
  AllowedScopes={
                       "openid",
                       "profile",
                       "UserServicesApi"
                    }

对于Ocelot而言你只需要在之前的配置中添加AuthenticationOptions节点参数配置

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/values/getuser",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 20001
        },
        {
          "Host": "localhost",
          "Port": 20001
        }
      ],
      "UpstreamPathTemplate": "/test",
      "UpstreamHttpMethod": [ "Get" ],
      "LoadBalancer": "LeastConnection",
      "ServiceName": "userservices",
      "UseServiceDiscovery": true,

      "AuthenticationOptions": {
        "AuthenticationProviderKey": "usergateway",
        "AllowScopes": [ "UserServicesApi" ]
      }
    }
  ],

  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:20000",
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500

    }

  }
}
Ocelot

相关文章: