IdentityServer4之Clients、Scopes、Claims与Token关联
IdentityServer4之Clients、Scopes、Claims与Token关联
参考
官方文档:client、identity_resource、api_resource:三类配置项介绍描述。
打一个不恰当的比喻来描述一下
User:表示自己 。
Client:表示客户经理,能指引或者代办一些业务。
Resource:表示银行,包括identity_resource(银行基本业务)、api_resource(银行特色业务)。多个resource比作多个分行。
user中的
Claims:自身在银行已经有的业务(包括自己YY的业务)。
client中的
Claims、Scopes是客户经理会推荐给你(User)的业务需不需要看自己。
Claims:好比优惠client可以选择给你或者不给。
Scopes:但是推荐给你的某个Scope业务可能与银行已经下线了但是client不知道。
Resource中的
Claims、Scopes、Scopes->Claims:表示银行的业务。
Token:银行认可自己拥有的业务信息。
User、Client、Resource配置
User配置
new TestUser
{
SubjectId = "",
Username = "ddr",
Password = "",
Claims = new []
{
new Claim("name", "ddr"),
new Claim("get", "get_order"), //User Claim Type 与 Api Resource中的Claims、Scopes->Claims的Type匹配就会输出到Token
new Claim("add", "add_order"),
new Claim("add", "add_account"),
new Claim("del", "del_all"),
new Claim("website", "https://ddr.com")
}
},
Client配置


Identity Resources配置
一般不需要改变就是默认的OpenId、Profile、Email、Phone、Address。
{ IdentityServerConstants.StandardScopes.Profile, new[]
{
JwtClaimTypes.Name,
JwtClaimTypes.FamilyName,
JwtClaimTypes.GivenName,
JwtClaimTypes.MiddleName,
JwtClaimTypes.NickName,
JwtClaimTypes.PreferredUserName,
JwtClaimTypes.Profile,
JwtClaimTypes.Picture,
JwtClaimTypes.WebSite,
JwtClaimTypes.Gender,
JwtClaimTypes.BirthDate,
JwtClaimTypes.ZoneInfo,
JwtClaimTypes.Locale,
JwtClaimTypes.UpdatedAt
}},
{ IdentityServerConstants.StandardScopes.Email, new[]
{
JwtClaimTypes.Email,
JwtClaimTypes.EmailVerified
}},
{ IdentityServerConstants.StandardScopes.Address, new[]
{
JwtClaimTypes.Address
}},
{ IdentityServerConstants.StandardScopes.Phone, new[]
{
JwtClaimTypes.PhoneNumber,
JwtClaimTypes.PhoneNumberVerified
}},
{ IdentityServerConstants.StandardScopes.OpenId, new[]
{
JwtClaimTypes.Subject
}}
Api Resource配置


过程详解
使用正常方式获取的Token

获取的Token详细信息
[
{
"type": "nbf",
"value": ""
},
{
"type": "exp",
"value": ""
},
{
"type": "iss",
"value": "http://www.ids4.com"
},
{
"type": "aud",
"value": "http://www.ids4.com/resources"
},
{
"type": "aud",
"value": "shop"
},
{
"type": "client_id",
"value": "ro.client"
},
{
"type": "sub",
"value": ""
},
{
"type": "auth_time",
"value": ""
},
{
"type": "idp",
"value": "local"
},
{
"type": "get",
"value": "get_order"
},
{
"type": "add",
"value": "add_order"
},
{
"type": "add",
"value": "add_account"
},
{
"type": "scope",
"value": "account"
},
{
"type": "scope",
"value": "order"
},
{
"type": "amr",
"value": "pwd"
},
{
"type": "api1返回",
"value": "2018-01-18 12:13:15"
}
]
client没有把优惠给你,client客户经理的Claims中是有ro - get_account但是这项优惠没有取出来,Properties默认设置
不会返回到Token。
// check for client claims
if (request.ClientClaims != null && request.ClientClaims.Any())
{
if (subject == null || request.Client.AlwaysSendClientClaims)
{
foreach (var claim in request.ClientClaims)
{
var claimType = claim.Type; if (request.Client.ClientClaimsPrefix.IsPresent())
{
claimType = request.Client.ClientClaimsPrefix + claimType;
} outputClaims.Add(new Claim(claimType, claim.Value, claim.ValueType));
}
}
}
new Claim("del", "del_all") 是自己YY出来的Token里也不会有。
/// <summary>
/// Filters the claims based on requested claim types.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="claims">The claims.</param>
/// <returns></returns>
public static List<Claim> FilterClaims(this ProfileDataRequestContext context, IEnumerable<Claim> claims)
{
return claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList();
}
claims自己的claims信息。

context.RequestedClaimTypes是Api Resource中Claims、Scopes->Claims的信息。

client客户经理推荐的123实际在银行已经下线了。
如果获取Token请求包含了 "123" 的scope,但是实际上Resource又不存在就会提示invalid_scope。
foreach (var scope in requestedScopes)
{
var identity = resources.IdentityResources.FirstOrDefault(x => x.Name == scope);
if (identity != null)
{
if (!client.AllowedScopes.Contains(scope))
{
_logger.LogError("Requested scope not allowed: {scope}", scope);
return false;
}
}
else
{
var api = resources.FindApiScope(scope);
if (api == null || !client.AllowedScopes.Contains(scope))
{
_logger.LogError("Requested scope not allowed: {scope}", scope);
return false;
}
}
}
Scope对模块鉴权
参考:https://docs.microsoft.com/en-us/aspnet/core/security/authorization/claims
Token中带有 scope:order或者scope:account的请求都能访问IdentityController。
Api项目配置
services.AddMvcCore()
.AddAuthorization(options =>
{
options.AddPolicy("Order", policy => policy.RequireClaim("scope","order","account"));
})
.AddJsonFormatters();
[Authorize(Policy = "Order")]
public class IdentityController : ControllerBase
IdentityServer4配置后台管理,在github找到一个随机生成数据的后台改成使用读数据库,数据库使用ids4示例生成。时间紧做出来并不好凑合用。
https://github.com/ddrsql/IdentityServer4.Admin
IdentityServer4之Clients、Scopes、Claims与Token关联的更多相关文章
- postman设置token关联参数,其他接口直接读取token变量
问题描述:有一个登录接口获取token,其他接口再次访问时都要带上token 解决方案: 步骤一:在登录接口访问后设置postman的环境变量,例如设置环境变量名:token,值为登录接口访问成功后, ...
- python+pytest接口自动化(13)-token关联登录
在PC端登录公司的后台管理系统或在手机上登录某个APP时,经常会发现登录成功后,返回参数中会包含token,它的值为一段较长的字符串,而后续去请求的请求头中都需要带上这个token作为参数,否则就提示 ...
- 登录获取token,token参数关联至所有请求的请求体内
问题描述: 有些系统接口判断用户是否登录,是校验登录接口成功后传的token值,也就是请求系统所有接口时,前端传参必带登录成功后接口返回的token,后台以此检验是否过期或是否有登录.所有接口都依赖登 ...
- python接口自动化-token参数关联登录(二)
原文地址https://www.cnblogs.com/yoyoketang/p/9098096.html 原文地址https://www.cnblogs.com/yoyoketang/p/68866 ...
- python接口自动化-token参数关联登录(登录拉勾网)
前言 登录网站的时候,经常会遇到传token参数,token关联并不难,难的是找出服务器第一次返回token的值所在的位置,取出来后就可以动态关联了 登录拉勾网 1.先找到登录首页https://pa ...
- Unable to find a constructor to use for type System.Security.Claims.Claim. A class should either have a default constructor
Newtonsoft.Json DeserializeObject 反序列化 IdentityServer4.Models Cliecnt 错误: Newtonsoft.Json.JsonSeria ...
- Spring Boot web API接口设计之token、timestamp、sign
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/vbirdbest/article/details/80789817一:token 简介Token:访 ...
- Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理
token处理之一基本参数配置 处理token时间.存储策略,客户端配置等 以前的都是spring security oauth默认的token生成策略,token默认在org.springframe ...
- IdentityServer4笔记整理(更新中)
1 OAuth 2.0 1.1 OAuth 2.0协议流程图 1.2 授权码模式 1.3 简化模式 1.4 资源所有者密码模式 1.5 客户端凭证模式 2 OpenID Connect(OIDC) 2 ...
随机推荐
- crontab使用环境变量
两种方式: 1)直接在crontab中定义变量,如: A=123 * * * * * echo $A > /tmp/a.txt 注意在定义变量时不能使用$引用其它变量,如下面的做法错误: A=1 ...
- C#-.Net Framework和.Net Compact Framework-摘
.Net Compact Framework 一种独立于硬件的环境,用于在资源受限制的计算设备上运行程序.它继承了公共语言运行时的完整 .NET Framework 结构,支持 .NET Framew ...
- java基础-day21
第10天 IO流 今日内容介绍 u 标准输入流 & 转换流 & 打印流 u 对象操作流 u Properties集合 第1章 标准输入流 & 转换流 & 打 ...
- POJ2112 Optimal Milking
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 17811 Accepted: 6368 ...
- 数组中超过N分之一的数字
寻找数组中超过一半的元素,这是一道十分经典和普遍的面试题了,实现起来比较容易,只是需要写技巧,将问题扩展就可以衍生到求数组中几个超过N分一的元素,例如找出数组中3个出现次数超过1/4的元素. /*** ...
- hive 函数 current_date()
current_date() 获取当天时间 select current_date() 结果是 实际操作中,这种写法只能获取日期是今天的数据,不适合数据重跑. 更合适的写法是 ,将日期作为外部参数传 ...
- DevExpress GridControl 关于使用CardView的一点小结
最近项目里需要显示商品的一系列图片,打算用CardView来显示,由于第一次使用,遇到许多问题,发现网上这方面的资源很少,所以把自己的一点点实际经验小结一下,供自己和大家以后参考. 1.选择CardV ...
- 【xcode中添加pch全局引用文件】
前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6之前的版本建项目就自动添加了是这样的: xcode6后的版本要自己手动的添加步骤如下: 1) 2) 3) ...
- Partition--使用分区切换来增加修改列的自增属性
使用分区来将非自增表改为自增表 ------------------------------------------------- --创建测试表TestTable001和TestTable002 C ...
- 实验5 IIC通讯与AD/接DA接口
1.利用单片机控制PCF8591的AD转换,控制AD0和AD1电位器,在数码光上显示DA转换的值. 2.利用单片机控制PCF8591的DA转换,让发光二极管D1由暗到亮变化,整个过程时间差不多2s左右 ...