IdentityServer4 负载均衡配置
在不用到负载之前,一切都很好,但是部署多个实例之后,问题挺多的:session问题、令牌签发后的校验问题。
在此之前,先自查官方文档:Deployment — IdentityServer4 1.0.0 documentation
把必要的东西都配置正确,然后继续查。
session问题
在需要经常需要与统一身份认证平台进行交互的情况(比如,A站点和B站点都是统一身份认证平台下的子系统,希望A站点登录后,在B进行登录的时候可以免登录的情形),建议不用继续使用sessionID,如果不需要,则存放在redis。
令牌签发后的校验问题
部署后启动:
2021-11-01 12:01:50.098 [WRN] Using an in-memory repository. Keys will not be persisted to storage
然后在A站点登录之后,关闭A站点,启动B站点进行测试是否正常可以验证通过,会发现提示去登录,查看日志发现:
2021-11-01 12:01:50.276 [ERR] cookie Showing login: User is not authenticated
然后F12查看cookie已经被删除,讲白了就是AB两个站点并不互认,官方文档里面写着
IdentityServer itself is stateless and does not require server affinity - but there is data that needs to be shared between the instances.
我真想抽它鸭子的。
解决:
因为ids4是有引用到微软库的一个 Microsoft.AspNetCore.DataProtection ,所以可以不用引用,你需要引用一个包:
Microsoft.AspNetCore.DataProtection.StackExchangeRedis
然后在使用:
var redis = ConnectionMultiplexer.Connect( Configuration["Redis:HostPort"]);
services.AddDataProtection()
.SetApplicationName(Configuration["Redis:ApplicationName"])
.PersistKeysToStackExchangeRedis( redis, "DataProtection-Keys");
就ok了,或者不想用StackExchangeRedis,就自己实现:
public class CustomRedisXmlRepository : Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository
{
protected readonly IRedisCache redisCache;
protected readonly int DBIndex;
private readonly string key; public CustomRedisXmlRepository(IRedisCache redisCache, int dbIndex, string key)
{
this.redisCache = redisCache;
this.DBIndex = dbIndex;
this.key = key;
} /// <inheritdoc />
public IReadOnlyCollection<XElement> GetAllElements()
{
return GetAllElementsCore().ToList().AsReadOnly();
} private IEnumerable<XElement> GetAllElementsCore()
{
foreach (var value in redisCache.GetList<string>(DBIndex,key))
{
yield return XElement.Parse(value);
}
} public void StoreElement(XElement element, string friendlyName)
{
redisCache.AddList(DBIndex,key, element.ToString(SaveOptions.DisableFormatting));
}
}
扩展:
public static class RedisDataProtectionBuilderExtensions
{
private const string DataProtectionKeysName = "DataProtection-Keys";
private const int DefaultDBIndex = 0; public static IDataProtectionBuilder PersistKeysToRedis(this IDataProtectionBuilder builder, IRedisCache redisCache,int dbIndex, string key)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (redisCache == null)
{
throw new ArgumentNullException(nameof(IRedisCache));
}
return PersistKeysToRedisInternal(builder, redisCache, dbIndex, key);
} public static IDataProtectionBuilder PersistKeysToRedis(this IDataProtectionBuilder builder, IRedisCache redisCache)
{
return PersistKeysToRedis(builder, redisCache, DefaultDBIndex, DataProtectionKeysName);
} private static IDataProtectionBuilder PersistKeysToRedisInternal(IDataProtectionBuilder builder, IRedisCache redisCache, int dbIndex, string key)
{
builder.Services.Configure<KeyManagementOptions>(options =>
{
options.XmlRepository = new CustomRedisXmlRepository(redisCache, dbIndex, key);
});
return builder;
}
}
引用
var redis = services.BuildServiceProvider().GetService<IRedisCache>();
services.AddDataProtection()
.SetApplicationName(config.RedisOptions.KeyPrefix+ "-Identityserver4-Production-Key")
.PersistKeysToRedis(redis, Application.Configs.RedisKey.Application, config.RedisOptions.KeyPrefix + "-DataProtection-Keys");
然后重新测试,会发现token已经校验通过:
2021-11-01 12:02:04.515 [INF] Token request validation success, {"ClientId":"zhicheng","ClientName":"**","GrantType":"authorization_code","Scopes":null,"AuthorizationCode":"****93B3","RefreshToken":"********","UserName":null,"AuthenticationContextReferenceClasses":null,"Tenant":null,"IdP":null,"Raw":{"client_id":"***","client_secret":"***REDACTED***","grant_type":"authorization_code","code":"153AF90A4B84AC5C0C8ABA1E22483A05936B6009E123AA8937015F26FD6A93B3","redirect_uri":"http://localhost:6001/Home/Index"},"$type":"TokenRequestValidationLog"}
坐等下班!
IdentityServer4 负载均衡配置的更多相关文章
- Nginx + Tomcat Windows下的负载均衡配置
Nginx + Tomcat Windows下的负载均衡配置 一.为什么需要对Tomcat服务器做负载均衡? Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果超过50 ...
- nginx安装及负载均衡配置
Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第二 ...
- Nginx负载均衡配置实例详解
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- Nginx 简单的负载均衡配置示例(转载)
原文地址:Nginx 简单的负载均衡配置示例(转载) 作者:水中游于 www.s135.com 和 blog.s135.com 域名均指向 Nginx 所在的服务器IP. 用户访问http://www ...
- Nginx负载均衡配置实例详解(转)
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- Nginx做NodeJS应用负载均衡配置实例
这篇文章主要介绍了Nginx做NodeJS应用负载均衡配置实例,本文直接给出配置实例,需要的朋友可以参考下. 负载均衡可以把用户的请求分摊到多个服务器上进行处理,从而实现了对海量用户的访问支持.负载均 ...
- Nginx+tomcat负载均衡配置
Nginx+tomcat是目前主流的java web架构,如何让nginx+tomcat同时工作呢,也可以说如何使用nginx来反向代理tomcat后端均衡呢?直接安装配置如下: 1.JAVA JDK ...
- CentOS6.5安装nginx及负载均衡配置
所有的安装包可以去以下地址下载,或者自行去官网下载,下面都有介绍. 所有安装包地址:http://download.csdn.net/detail/carboncomputer/9238037 原文地 ...
- Flume负载均衡配置
flume负载均衡配置 集群DNS配置如下: hadoop-maser 192.168.177.162 machine-0192.168.177.158 machine-1191.168.177.16 ...
随机推荐
- MapReduce 示例:减少 Hadoop MapReduce 中的侧连接
摘要:在排序和reducer 阶段,reduce 侧连接过程会产生巨大的网络I/O 流量,在这个阶段,相同键的值被聚集在一起. 本文分享自华为云社区<MapReduce 示例:减少 Hadoop ...
- 3.15学习总结(Python爬取网站数据并存入数据库)
在官网上下载了Python和PyCharm,并在网上简单的学习了爬虫的相关知识. 结对开发的第一阶段要求: 网上爬取最新疫情数据,并存入到MySql数据库中 在可视化显示数据详细信息 项目代码: im ...
- C#导出数据—使用Word模板
前言 本文主要介绍C#使用标签替换的方法导出数据,导出的数据模板使用Word文档. 模板建立 首先创建一个Word文档,然后建立一个基础模板.然后将上方菜单切换到插入菜单. 然后在想填充数据的地方添加 ...
- 性能再提升70%?大咖前瞻带你揭开.NET6的神秘面纱!
本月初微软官宣.NET 6 的RC1即将在11月正式发布,这意味着.NET6正式版跟我们见面的时间又近了一步.在之前的.NET6预览版本中,微软加入了大量新功能特性,而在最终版本中将不再额外加入新的内 ...
- mysql给数据库表里某个字段赋随机值
UPDATE sxz_goods set sales_volume_base = round(rand() * 50) + 1 where sales_volume_base =0 ORDER BY ...
- stmt.executeQuery不执行解决办法
感谢博主分享:https://blog.csdn.net/lxmky/article/details/4705698 今天在Eclipse下编写jsp网页时,出现一个问题,主要是stmt.execut ...
- python序列类型及一些操作
序列分类 1.按存放的数据类型分类: 容器类型(能存放不同类型的数据):list.tuple.coolections.deque 扁平序列(只能存放一种类型的数据):str.bytes.bytearr ...
- HTML 网页开发、CSS 基础语法——二.互联网原理
1. 互联网的运行过程 ①用户通过输入网址,发送一个HTTP请求到服务器中去,服务器里面存储了程序员上传的所有网页文件. ② 服务器一旦接收到请求,就会将我们所有的相关网页文件,回传到客户端,通过HT ...
- 项目部署(ubuntu+uwsgi+nginx+supervisor+django)
一.在开发机上的准备工作 1. 确认项目没有bug. 2.设置`ALLOW_HOST`为你的域名,以及ip地址. 4.设置`DEBUG=False`,避免如果你的网站产生错误,而将错误信息暴漏给用户. ...
- P6177-Count on a tree II/[模板]树分块
正题 题目链接:https://www.luogu.com.cn/problem/P6177 题目大意 \(n\)个点的一棵树\(m\)次询问树上颜色. 强制在线 \(1\leq n\leq 4\ti ...