上一篇已经构建好了例子,接下来将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":
},
{
"Host": "localhost",
"Port":
}
],
"UpstreamPathTemplate": "/test",
"UpstreamHttpMethod": [ "Get" ],
"LoadBalancer": "LeastConnection",
"ServiceName": "userservices",
"UseServiceDiscovery": true, "AuthenticationOptions": {
"AuthenticationProviderKey": "usergateway",
"AllowScopes": [ "UserServicesApi" ]
}
}
], "GlobalConfiguration": {
"BaseUrl": "http://localhost:20000",
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": } }
}

Ocelot

 "AuthenticationOptions": {
"AuthenticationProviderKey": "usergateway",
"AllowScopes": [ "UserServicesApi" ]
}
AuthenticationProviderKey 其实就是授权的authenticationscheme
allscopes 就是 apiresource中配置的授权访问范围,这里配置的即 ApiName

同时还需要在网关添加授权验证服务,配置好授权地址 ApiName(scope),以及对renefrence or jwt or both 和 secret 如果没有使用https 需要设置RequireHttpsMetadata =false

 services.AddAuthentication()
.AddIdentityServerAuthentication("usergateway", options => {
options.Authority = "http://localhost:30000";
options.ApiName = "UserServicesApi";
options.SupportedTokens = IdentityServer4.AccessTokenValidation.SupportedTokens.Both;
options.ApiSecret = "liyouming";
options.RequireHttpsMetadata = false; });
使用PostMan 来测试下功能
不清楚获取Token地址可以访问配置文件查看
http://localhost:30000/.well-known/openid-configuration
{
"issuer": "http://localhost:30000",
"jwks_uri": "http://localhost:30000/.well-known/openid-configuration/jwks",
"authorization_endpoint": "http://localhost:30000/connect/authorize",
"token_endpoint": "http://localhost:30000/connect/token",
"userinfo_endpoint": "http://localhost:30000/connect/userinfo",
"end_session_endpoint": "http://localhost:30000/connect/endsession",
"check_session_iframe": "http://localhost:30000/connect/checksession",
"revocation_endpoint": "http://localhost:30000/connect/revocation",
"introspection_endpoint": "http://localhost:30000/connect/introspect",
"frontchannel_logout_supported": true,
"frontchannel_logout_session_supported": true,
"backchannel_logout_supported": true,
"backchannel_logout_session_supported": true,
"scopes_supported": ["openid", "profile", "UserServicesApi", "offline_access"],
"claims_supported": [],
"grant_types_supported": ["authorization_code", "client_credentials", "refresh_token", "implicit"],
"response_types_supported": ["code", "token", "id_token", "id_token token", "code id_token", "code token", "code id_token token"],
"response_modes_supported": ["form_post", "query", "fragment"],
"token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post"],
"subject_types_supported": ["public"],
"id_token_signing_alg_values_supported": ["RS256"],
"code_challenge_methods_supported": ["plain", "S256"]
}
1、通过 http://localhost:30000/connect/token获取token

客户端模式需要四个参数 
client_id
client_secret
grant_type
scope

直接访问test提示401没授权

这里拿到了 access_token,接下来通过access_token访问GateWay中的test




Ocelot + IdentityServer4 构建 GateWay的更多相关文章

  1. NET Core + Ocelot + IdentityServer4 + Consul

    .NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现 先决条件 关于 Ocelot 针对使用 .NET 开发微服务架构或者面向服务架构提供一个统一访 ...

  2. 【转】.NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现

    作者:Zhang_Xiang 原文地址:.NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现 先决条件 关于 Ocelot 针对使用 .NET 开发 ...

  3. 分享一个集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo+SkyWalking的微服务开发框架

    集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo的微服务开发框架 Github源代码地址 htt ...

  4. 微服务项目整合Ocelot+IdentityServer4

    项目搭建肯定少不了认证和授权,传统的单体应用基于cookie和session来完成的. 因为http请求是无状态的,每个请求都是完全独立的,服务端无法确认当前请求之前是否登陆过.所以第一次请求(登录) ...

  5. 使用Ocelot构建GateWay

    添加Nuget包:Ocelot 添加配置文件Ocelot.json 具体配置可以看另一篇Ocelot配置 Json配置文件主要包含两个根节点: ReRoutes:路由重定向配置 都是数组结构 可以配置 ...

  6. .NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现

    先决条件 关于 Ocelot 针对使用 .NET 开发微服务架构或者面向服务架构提供一个统一访问系统的组件. 参考 本文将使用 Ocelot 构建统一入口的 Gateway. 关于 IdentityS ...

  7. .Net Core 商城微服务项目系列(一):使用IdentityServer4构建基础登录验证

    这里第一次搭建,所以IdentityServer端比较简单,后期再进行完善. 1.新建API项目MI.Service.Identity,NuGet引用IdentityServer4,添加类InMemo ...

  8. .Net Core 商城微服务项目系列(二):使用Ocelot + Consul构建具备服务注册和发现功能的网关

    1.服务注册 在上一篇的鉴权和登录服务中分别通过NuGet引用Consul这个包,同时新增AppBuilderExtensions类: public static class AppBuilderEx ...

  9. Ocelot + IdentityServer4 坑自己

    现像是 connect/userinfo 可以访问 但是api都提示401 后面发现是在appsettings.json "Options": {"Authority&q ...

随机推荐

  1. 【刷题】BZOJ 4977 [Lydsy1708月赛]跳伞求生

    Description 小Q最近沉迷于<跳伞求生>游戏.他组建了一支由n名玩家(包括他自己)组成的战队,编号依次为1到n.这个游 戏中,每局游戏开始时,所有玩家都会从飞机上跳伞,选择一个目 ...

  2. 【刷题】LOJ 6006 「网络流 24 题」试题库

    题目描述 假设一个试题库中有 \(n\) 道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性.现要从题库中抽取 \(m\) 道题组成试卷.并要求试卷包含指定类型的试题.试设计一个满足要求的组 ...

  3. 在Mac上快速Kill掉Tomcat

    最近IDEA总是会莫名其妙的挂掉,而挂掉之后通过IDEA开启的Tomcat却没有同步给关掉,等我再在IDEA里要启动的时候,就不行了.... 这时,就需要手动去kill掉tomcat,每次先 ps - ...

  4. C源程序怎么变成可执行文件

    本文所有内容都是来自网上的转载,文末有转载链接. 电子计算机所使用的是由“0”和“1”组成的二进制数,二进制是计算机的语言的基础.计算机发明之初,人们只能降贵纡尊,用计算机的语言去命令计算机干这干那, ...

  5. 洛谷 P1852 [国家集训队]跳跳棋 解题报告

    P1852 [国家集训队]跳跳棋 题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在\(a\),\(b\), ...

  6. 「Vue」JS方法学习

    1.构造函数 大写开头的,能被NEW一个新实例,实例即执行回调函数 异步返回数据.then指定回调函数的时候,成功的回调函数必须传,失败的回调可以不传 var fs = require('fs') f ...

  7. spring JMS在接收消息的时候总是报错

    spring JMS在接收消息的时候总是报错 org.springframework.jms.UncategorizedJmsException: Uncategorized exception oc ...

  8. 《高性能MySQL》——第五章创建高性能索引

    1.创建索引基本语法格 在MySQL中,在已经存在的表上,可以通过ALTER TABLE语句直接为表上的一个或几个字段创建索引.基本语法格式如下: ALTER TABLE 表名 ADD [UNIQUE ...

  9. html5 canvas arcTo()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 【整理】HTML5游戏开发学习笔记(2)- 弹跳球

    1.预备知识(1)在画布上绘制外部图片资源(2)梯度(gradient):HTML5中的对象类型,包括线性梯度和径向梯度.如createLinearGradient,绘制梯度需要颜色组http://w ...