IdentityServer4之Clients、Scopes、Claims与Token关联

参考

官方文档clientidentity_resourceapi_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关联的更多相关文章

  1. postman设置token关联参数,其他接口直接读取token变量

    问题描述:有一个登录接口获取token,其他接口再次访问时都要带上token 解决方案: 步骤一:在登录接口访问后设置postman的环境变量,例如设置环境变量名:token,值为登录接口访问成功后, ...

  2. python+pytest接口自动化(13)-token关联登录

    在PC端登录公司的后台管理系统或在手机上登录某个APP时,经常会发现登录成功后,返回参数中会包含token,它的值为一段较长的字符串,而后续去请求的请求头中都需要带上这个token作为参数,否则就提示 ...

  3. 登录获取token,token参数关联至所有请求的请求体内

    问题描述: 有些系统接口判断用户是否登录,是校验登录接口成功后传的token值,也就是请求系统所有接口时,前端传参必带登录成功后接口返回的token,后台以此检验是否过期或是否有登录.所有接口都依赖登 ...

  4. python接口自动化-token参数关联登录(二)

    原文地址https://www.cnblogs.com/yoyoketang/p/9098096.html 原文地址https://www.cnblogs.com/yoyoketang/p/68866 ...

  5. python接口自动化-token参数关联登录(登录拉勾网)

    前言 登录网站的时候,经常会遇到传token参数,token关联并不难,难的是找出服务器第一次返回token的值所在的位置,取出来后就可以动态关联了 登录拉勾网 1.先找到登录首页https://pa ...

  6. 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 ...

  7. Spring Boot web API接口设计之token、timestamp、sign

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/vbirdbest/article/details/80789817一:token 简介Token:访 ...

  8. Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理

    token处理之一基本参数配置 处理token时间.存储策略,客户端配置等 以前的都是spring security oauth默认的token生成策略,token默认在org.springframe ...

  9. IdentityServer4笔记整理(更新中)

    1 OAuth 2.0 1.1 OAuth 2.0协议流程图 1.2 授权码模式 1.3 简化模式 1.4 资源所有者密码模式 1.5 客户端凭证模式 2 OpenID Connect(OIDC) 2 ...

随机推荐

  1. 延时、输入输出接口P0~P3

    1.寄存器 为了知道延时程序是如何工作的,我们必需首先了解延时程序中出现的一些符号,就从R1开始,R1被称之为工作寄存器.什么是工作寄存器呢?让我们从现实生活中来找找答案.如果出一道数学题:123+5 ...

  2. day28(ajax之js原生代码实现)

    ajax ajax:异步页面无刷新技术 AJAX:异步的 JavaScript And XML. * 使用的是老的技术,用的是新的思想. AJAX的功能:完成页面的局部刷新,不中断用户的体验. XML ...

  3. noip第33课作业

    1.    排座椅 [问题描述] 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的D对同 ...

  4. python标准库及其它应用

    一: sys模块的介绍: 程序如下: #coding:utf-8import sysprint sys.argv[0]print sys.argv[1]print sys.argv[2] 打开cmd窗 ...

  5. hdu 2838 Cow Sorting (树状数组+逆序对)

    题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...

  6. hdu 5094 状压bfs+深坑

    http://acm.hdu.edu.cn/showproblem.php?pid=5094 给出n*m矩阵 给出k个障碍,两坐标之间存在墙或门,门最多10种,状压可搞 给出s个钥匙位置及编号,相应的 ...

  7. Scala_基本语法

    基本语法 声明值和变量 Scala有两种类型的变量: val:是不可变的(变量的引用不可变),在声明时就必须被初始化,而且初始化以后就不能再赋值: var:声明的时候需要进行初始化,初始化以还可以再对 ...

  8. POJ3666 线性dp_离散化_贪心

    POJ3666 线性dp_离散化_贪心 就DP而言这个题不算难,但是难就难在贪心,还有离散化的思想上 题目大意:n个土堆,问你最少移动多少单位的图,可以使得这n个土堆变成单调的 dp[i][j]表示前 ...

  9. zabbix已恢复正常,但是报警信息一直出现,求大佬解答。

    手动关闭时提醒如下:无法确认问题,事件不是问题状态 模板设置是允许手动关闭,内网机器,ping没有问题.

  10. jar包获取资源文件

    背景 写的一个spring boot项目打成jar包部署运行下,打成jar包,提示找不到资源文件,如下图: 直接通过idea是可以运行的,但打成jar包后提示找不到资源文件,简单查阅后了解到是因为ja ...