Consul+Ocelot搭建微服务实践--IdentityServer集成

这篇博客介绍了如何结合IdentityServer4和Ocelot搭建微服务实践,包括IdentityServer4的安装、配置,以及如何将IdentityServer集成到Ocelot中。内容涵盖IdentityServer的基本概念、配置中心的定义(如Client、ApiResource和IdentityResource)、证书生成、数据库迁移和自定义验证。此外,还讨论了如何在Ocelot中配置IdentityServer以实现授权认证。
摘要由CSDN通过智能技术生成

本篇幅所涉及的内容比较多,请园友有耐心的品味下去。

1、IdentityServer介绍

IdentityServer4 是asp.netcore 的OpenID Connect和OAuth 2.0框架。官方文档:http://docs.identityserver.io/en/latest/
我主要进行对自己所做的进行总结,不会介绍很多的理论类容。

2、建立IdentityServer

2.1 安装IdentityServer4

可以使用PS进行命令安装: Install-Package IdentityServer4。也可以在Nuget管理中心进行搜索安装。

2.2 定义配置中心

网上很多都是使用的TestUser进行的测试,我这个案例中使用sqlserver持久化进行验证,对于用户这块我后面详细介绍。(要是觉得麻烦那么也可以使用TestUser和)

2.2.1 定义Client

可以查看 官方文档–Client 进行了解Client相关知识。

public class InMemoryConfiguration
{
   
	public static IEnumerable<Client> GetAllClients()
	{
   
		return new[]
		{
   
		//采用密码模式
		new Client
		 {
   
		     ClientId = "api.service1",
		     ClientSecrets = new [] {
    new Secret("secret".Sha256()) },
		     AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
		     AllowedScopes = new [] {
   
		         "service1",
		     },
		     //支持刷新token
		     AllowOfflineAccess=true,
		     //toke过期时间。默认一小时
		     //AccessTokenLifetime=60,
		     //滑动刷新token的时间。默认一天。
		     //SlidingRefreshTokenLifetime=60,
		     //刷新token的模式,固定刷新时间和滑动刷新时间
		     //RefreshTokenExpiration=TokenExpiration.Sliding,                    
		     //详情文档:https://identityserver4-zh-cn.readthedocs.io/zh_CN/release/topics/refresh_tokens.html
		 },
		//采用客户端模式
		 new Client
		 {
   
		     ClientId = "api.service2",
		     ClientSecrets = new [] {
    new Secret("secret".Sha256()) },
		     AllowedGrantTypes = GrantTypes.ClientCredentials,
		     AllowedScopes = new [] {
   
		         "service2",
		     },
		 }
		};
	}
}

2.2.2 定义ApiResource

可以查看 官方文档–ApiResouce 进行了解ApiResouce相关知识。

public class InMemoryConfiguration
{
   
	public static IEnumerable<ApiResource> GetAllResources()
	{
   
	    return new[]
	    {
   
	        //向Cliam中添加Role、Email。这样在获取HttpContext.User.Cliam时才能获取到role、email
	        new ApiResource("service1", "微服务架构设计,Service1",new List<string>{
   
	            JwtClaimTypes.Role,
	            JwtClaimTypes.Email,
	        }),
	        new ApiResource("service2", "微服务架构设计,Service2",new List<string>{
   
	            JwtClaimTypes.Role,
	            JwtClaimTypes.Email,
	        })
	    };
	}
}

2.2.3定义IdentityResource

可以查看 官方文档–IdentityResource 进行了解IdentityResource相关知识。

public class InMemoryConfiguration
{
   
	public static IEnumerable<IdentityResource> GetIdentityResources()
	 {
   
	     //定义支持的资源类型。SuportScope的类型
	     return new List<IdentityResource>
	     {
   
	         new IdentityResources.OpenId(),
	         new IdentityResources.Profile()
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值