IdentityServer4 配置负载均衡
如果使用 IdentityServer4 做授权服务的负载均衡,默认情况下是不可以的,比如有两个授权服务站点,一个资源服务绑定其中一个授权服务(Authority配置),如果通过另外一个授权服务获取access_token,然后拿这个access_token去访问资源服务,会报 401 未授权错误,为什么?原因在这:
By default an access token will contain claims about the scope, lifetime (nbf and exp), the client ID (client_id) and the issuer name (iss).
归纳一下,生成access_token受影响的因素:
- scope(授权范围):服务包含在 scope 内,生成的
access_token,才能访问本服务。 - lifetime(生命周期):过期的
access_token,无效访问。 - client ID (client_id):不同的客户端 ID,生成不同对应的
access_token。 - issuer name (iss):翻译过来“发行者名称”,类似于主机名。
- RSA 加密证书(补充):不同的加密证书,生成不同对应的
access_token。
要让负载均衡下的两个授权服务,可以正常使用的话,需要确保两台授权服务以上五种因素完全一致,除了 issuer name (iss),其他因素都是一样的。
IdentityServer4 怎么设置 issuer name (iss)呢?答案是通过IssuerUri:
IssuerUri:Set the issuer name that will appear in the discovery document and the issued JWT tokens. It is recommended to not set this property, which infers the issuer name from the host name that is used by the clients, If not set, the issuer name is inferred from the request.
说明中不建议我们进行设置,默认情况下,IdentityServer4 会从客户端的主机名中获取,可以认为,默认情况下,issuer name(IssuerUri)就是授权服务的主机名(比如http://10.9.1.1:5000)。
手动设置IssuerUri代码:
var builder = services.AddIdentityServer(x => x.IssuerUri = "http://111.12.2.21:8000"); //slb 地址
资源服务授权配置修改:
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://111.12.2.21:8000", //slb 地址
ApiName = "trade_refund",
RequireHttpsMetadata = false
});
获取access_token示例代码:
var client = new DiscoveryClient("http://111.12.2.21:8000"); //必须是 slb 地址,如果是单独的授权服务地址,会报错误(Value cannot be null. Parameter name: address)
client.Policy.RequireHttps = false;
var disco = await client.GetAsync();
var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, clientSecret);
var tokenResponse = tokenClient.RequestClientCredentialsAsync(scope);
var accessToken = tokenResponse.AccessToken;
通过 HTTP Post 获取access_token(不通过 slb,直接请求单独的授权服务),可以授权访问资源服务,获取access_token示例:

参考资料:
IdentityServer4 配置负载均衡的更多相关文章
- 从编译安装Keepalived 到 配置 负载均衡(LVS-DR)
最近在研究服务器高可用集群 (HA)…… Keepalived 是用C写的软路由.提供负载均衡与高可用特性. 负载均衡利用IPVS技术 高可用通过VRRP协议实现 更难能的贵的是,一直到最近还在更新 ...
- 解决nginx配置负载均衡时invalid host in upstream报错
当前平台: windows nginx版本: 1.11.5 前言: 在配置负载均衡时,同时也需要设置反向代理,当修改了nginx.conf时,发现nginx服务无法开启. 1. 打开"ngi ...
- Nginx学习笔记(三)--- Nginx实现反向代理和配置负载均衡
1.反向代理 2.Nginx反向代理流程图 3.安装多个tomcat 3.1把tomcat的压缩包传到Linux上 3.2 解压tomcat 3.3 给压缩好的tomcat改个名字用来区分一下 3.4 ...
- Apache配置负载均衡-实例
公司两台服务器都安装了tomcat,配置apache作为负载均衡,当一台服务器出现故障时还能保证业务正常运行. Server1:192.168.1.100 Server2:192.168.1.200 ...
- nginx配置 负载均衡
配置nginx #配置虚拟主机 server { listen 80; server_name www.testaaa.com; location / { #root /usr/local/nginx ...
- Apache和Tomcat 配置负载均衡(mod-proxy方式)-无session共享、无粘性session
转:https://blog.csdn.net/wangjunjun2008/article/details/38268483 mod-proxy方式实现负载均衡是利用了Apache 2.x版本自带的 ...
- dubbo配置负载均衡、集群环境
再用dubbo作为项目架构的时候,给consumer消费者用nginx提供了负载均衡策略和集群的实现, 但是想了下,consumer再多,但是提供者还是一个,最后还不都是落到了这一个provider上 ...
- office web apps安装部署,配置https,负载均衡(六)配置负载均衡
owa可以采用任何的负载均衡方案,我们这里采用阿里云提供的负载均衡解决方案 前提条件,你已经配置了一台域控制器,两台域服务器[即安装了owa相关软件,并将计算机隶属于域]: 如果您不清楚怎么做,那么请 ...
- LVS实现负载均衡原理及安装配置 负载均衡
LVS实现负载均衡原理及安装配置 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F ...
随机推荐
- GCD之死锁
GCD相当好用,但用不好就会死锁,始终要记着这样一句秘籍: 不要在串行队列放dispatch_sync.dispatch_apply 下面看几个例子 1 2 3 4 5 6 7 8 9 10 11 1 ...
- Codeforces Round #436 (Div. 2) C. Bus
http://codeforces.com/contest/864/problem/C 题意: 坐标轴上有x = 0和 x = a两点,汽车从0到a之后掉头返回,从a到0之后又掉头驶向a...从0到a ...
- 使用百度ueditor的插件使得代码高亮显示
一.在show.html模板中,引入ueditor的插件,并调用 <link rel="stylesheet" href="__ROOT__/Data/uedito ...
- 浅析前端开发中的 MVC/MVP/MVVM 模式
MVC,MVP和MVVM都是常见的软件架构设计模式(Architectural Pattern),它通过分离关注点来改进代码的组织方式.不同于设计模式(Design Pattern),只是为了解决一类 ...
- An Introduction to Variational Methods (5.1)
在这篇文章中,我引用Bishop书中的一个例子,来简单介绍一下Variational Methods的应用.想要更详细地理解这个例子,可以参考Bishop的书Pattern Recongnition ...
- JS方法总结
1.普通的方法定义 function(){ } 2.变量方法定义 var text=function(){ } 3.对象方法定义 text:function(){ } 4.ES6 text(x=0,y ...
- pycharm(windows)安装及其设置中文菜单
pycharm(windows)安装及其设置中文菜单 1.下载 在官网(http://www.jetbrains.com/pycharm/download/#section=windows)进行下载 ...
- 22.Linux-块设备驱动之框架详细分析(详解)
本节目的: 通过分析块设备驱动的框架,知道如何来写驱动 1.之前我们学的都是字符设备驱动,先来回忆一下 字符设备驱动: 当我们的应用层读写(read()/write())字符设备驱动时,是按字节/字符 ...
- ConvertUtils.register注册转换器
当用到BeanUtils的populate.copyProperties方法或者getProperty,setProperty方法其实都会调用convert进行转换 但Converter只支持一些基本 ...
- RESTful API 架构解读
RESTful API 架构解读 首先我们还是先介绍下 RESTful api 的来龙去脉. 首先, RESTful (下文都简称 RESTful api 为 RESTful ) 1.RESTful ...