Ocelot + IdentityServer4 构建 GateWay
上一篇已经构建好了例子,接下来将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的更多相关文章
- NET Core + Ocelot + IdentityServer4 + Consul
.NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现 先决条件 关于 Ocelot 针对使用 .NET 开发微服务架构或者面向服务架构提供一个统一访 ...
- 【转】.NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现
作者:Zhang_Xiang 原文地址:.NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现 先决条件 关于 Ocelot 针对使用 .NET 开发 ...
- 分享一个集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo+SkyWalking的微服务开发框架
集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo的微服务开发框架 Github源代码地址 htt ...
- 微服务项目整合Ocelot+IdentityServer4
项目搭建肯定少不了认证和授权,传统的单体应用基于cookie和session来完成的. 因为http请求是无状态的,每个请求都是完全独立的,服务端无法确认当前请求之前是否登陆过.所以第一次请求(登录) ...
- 使用Ocelot构建GateWay
添加Nuget包:Ocelot 添加配置文件Ocelot.json 具体配置可以看另一篇Ocelot配置 Json配置文件主要包含两个根节点: ReRoutes:路由重定向配置 都是数组结构 可以配置 ...
- .NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现
先决条件 关于 Ocelot 针对使用 .NET 开发微服务架构或者面向服务架构提供一个统一访问系统的组件. 参考 本文将使用 Ocelot 构建统一入口的 Gateway. 关于 IdentityS ...
- .Net Core 商城微服务项目系列(一):使用IdentityServer4构建基础登录验证
这里第一次搭建,所以IdentityServer端比较简单,后期再进行完善. 1.新建API项目MI.Service.Identity,NuGet引用IdentityServer4,添加类InMemo ...
- .Net Core 商城微服务项目系列(二):使用Ocelot + Consul构建具备服务注册和发现功能的网关
1.服务注册 在上一篇的鉴权和登录服务中分别通过NuGet引用Consul这个包,同时新增AppBuilderExtensions类: public static class AppBuilderEx ...
- Ocelot + IdentityServer4 坑自己
现像是 connect/userinfo 可以访问 但是api都提示401 后面发现是在appsettings.json "Options": {"Authority&q ...
随机推荐
- pgm6
有个比较有意思的想法是编码理论的反问题是 machine learning,这也是这部分学习的一个收获.这个其实很奇怪,编码理论其实是有 ground truth 的,然后通过编码产生“冗余”,这样才 ...
- 【bzoj1040】 ZJOI2008—骑士
http://www.lydsy.com/JudgeOnline/problem.php?id=1040 (题目链接) 题意 一个基环森林,从中选出不相邻的若干个点使得这些点的点权和最大. Solut ...
- svn cleanup
SVN 本地更新时,由于一些操作中断更新,如磁盘空间不够,用户取消. 可能会造成本地文件被锁定的情况.一般出现这种情况的解决方法: 1.可以使用SVN clean up来清除锁定. 2.如果不是本目录 ...
- RabbitMQ的安装部署
RabbitMQ安装部署 一.软件准备 wget http://erlang.org/download/otp_src_19.3.tar.gz wget http://www.rabbitmq.com ...
- bzoj千题计划218:bzoj2333: [SCOI2011]棘手的操作
http://www.lydsy.com/JudgeOnline/problem.php?id=2333 上次那个是线段树,再发一个左偏树 维护两种左偏树 第一种是对每个联通块维护一个左偏树 第二种是 ...
- kubeadm部署Kubernetes集群
Preface 通过kubeadm管理工具部署Kubernetes集群,相对离线包的二进制部署集群方式而言,更为简单与便捷.以下为个人学习总结: 两者区别在于前者部署方式使得大部分集群组件(Kube- ...
- FFprobe使用指南
http://blog.csdn.net/stone_wzf/article/details/45378759 http://blog.chinaunix.net/uid-26000296-id-42 ...
- Docker 入门 第二部分: 容器
目录 Docker 入门 第二部分: 容器 先决条件 介绍 你的新开发环境 使用 Dockerfile 定义一个容器 Dockerfile 应用本身 requirements.txt app.py 构 ...
- 如何解决Mac只能登QQ不能联网
如何解决Mac只能登QQ不能联网,路由正常,Wifi帐号密码正确,但wifi中断不能联网的问题.
- 使用Docx4j创建word文档
原文标题:Creating Word documents with Docx4j 原文链接:http://blog.iprofs.nl/2012/09/06/creating-word-documen ...