上一篇已经构建好了例子,接下来将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

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

AuthenticationProviderKey 其实就是授权的authenticationschemeallscopes 就是 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_idclient_secretgrant_typescope

直接访问test提示401没授权

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



转载于:https://www.cnblogs.com/liyouming/p/9025084.html

Ocelot + IdentityServer4 构建 GateWay相关推荐

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

    .NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现 原文:.NET Core + Ocelot + IdentityServer4 + Consu ...

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

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

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

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

  4. 「拥抱开源, 又见 .NET」系列第三次线下活动简报

    「拥抱开源, 又见 .NET」 随着 .NET Core的发布和开源,.NET又重新回到人们的视野.自2016年 .NET Core 1.0 发布以来,其强大的生命力让越来越多技术爱好者对她的未来满怀 ...

  5. 【西安活动】 | 4月20日「拥抱开源,又见.NET:云时代 • 新契机」

    云计算日渐兴起,成为提升企业效率和生产力的最终解决方案,而云时代也为软件开发模式带来了翻天覆地的变化.可以说 .NET Core就是这个时代催生的产物.自2016年 .NET Core 1.0 发布以 ...

  6. Ocelot 资源汇总

    前言 最近一两年.NET Core的关注度持续上升, 微服务及云原生应用开发上采用.NET Core也越来越多,Ocelot 作为.NET Core平台下一款开源的API 网关开发库越来越得到社区的认 ...

  7. 《深入理解Spring Cloud与微服务构建》出版啦!

    作者简介 方志朋,毕业于武汉理工大学,CSDN博客专家,专注于微服务.大数据等领域,乐于分享,爱好开源,活跃于各大开源社区.著有<史上最简单的Spring Cloud教程>,累计访问量超过 ...

  8. 使用 Ocelot 匹配路由的方法匹配路由

    使用 Ocelot 匹配路由的方法匹配路由 Intro 之前我们在 Ocelot 网关的基础上自定义了一个认证授权的 Ocelot 中间件,根据请求的路径和 Method 进行匹配,找到对应的权限配置 ...

  9. NET Core微服务之路:基于Ocelot的API网关Relay实现--RPC篇

    前言 我们都知道,API网关是工作在应用层上网关程序,为何要这样设计呢,而不是将网关程序直接工作在传输层.或者网络层等等更底层的环境呢?让我们先来简单的了解一下TCP/IP的五层模型. (图片出自ht ...

最新文章

  1. [C#反射]C#中的反射解析及使用.
  2. php中一个经典的!==的用法
  3. citrix 路径遍历 rce漏洞(CVE-2019-19781)
  4. Linux空间过满无法登录,linux下磁盘空间不足导致oracle无法登录的解决方案
  5. [003]Reference in C++---C++引用基础知识篇
  6. 职场社交:做职场版微信不如做职场版微博
  7. C# -WinForm 中英文实现, 国际化实现的简单方法
  8. linux rar命令没找到,Linux没有rar解压命令
  9. 微服务升级_SpringCloud Alibaba工作笔记0023---Nacos之Namespace空间方案
  10. python学习(三)
  11. js中eval的用法
  12. Android tv upnp播放器,TV Connect: AirPlay, DLNA
  13. IP子网划分(3分钟让你学会套用公式划分)!
  14. Ubuntu 软件安装方法(入门必看)
  15. 项目合同管理 考纲与考试要点
  16. 简简单单汇率换算HTML代码
  17. 下载好的IDEA双击打不开,解决方法
  18. FileZillaClient连接虚拟机教程
  19. Ventrilo初学者指南,面向游戏玩家的VoIP应用
  20. 不懂就问,机器人做核酸是一种什么体验?|一周AI新闻

热门文章

  1. Swift - 使用NSNotificationCenter发送通知,接收通知
  2. 更改android AVD模拟器创建路径位置的方法
  3. Linux环境下c语言静态链接库和动态链接库创建和使用
  4. 是‘登陆’还是‘登录’
  5. [JavaScript]牛人的JS是怎么玩的
  6. android 2.0 软件,Apeaksoft Android Toolkit
  7. mysql binlog_checksum_【原创】研发应该懂的binlog知识(下)
  8. java显示服务器端ip,Java服务器获取客户端的ip
  9. Excel数组公式从入门到精通之精通篇
  10. U型管铁球或将代替现代火箭喷射装置