IdentityServer4之Clients、Scopes、Claims与Token关联

时间:2022-09-22 23:43:11

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配置

IdentityServer4之Clients、Scopes、Claims与Token关联

IdentityServer4之Clients、Scopes、Claims与Token关联

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配置

IdentityServer4之Clients、Scopes、Claims与Token关联

IdentityServer4之Clients、Scopes、Claims与Token关联

过程详解

使用正常方式获取的Token

IdentityServer4之Clients、Scopes、Claims与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默认设置IdentityServer4之Clients、Scopes、Claims与Token关联不会返回到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信息。

IdentityServer4之Clients、Scopes、Claims与Token关联

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

IdentityServer4之Clients、Scopes、Claims与Token关联

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. 登录获取token,token参数关联至所有请求的请求体内

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

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

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

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

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

  5. Unable to find a constructor to use for type System&period;Security&period;Claims&period;Claim&period; A class should either have a default constructor

    Newtonsoft.Json DeserializeObject 反序列化  IdentityServer4.Models Cliecnt 错误: Newtonsoft.Json.JsonSeria ...

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

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

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

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

  8. IdentityServer4笔记整理&lpar;更新中&rpar;

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

  9. Ocelot网关&plus;IdentityServer4实现API权限认证

    Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butterfly ...

随机推荐

  1. 《Entity Framework 6 Recipes》中文翻译系列 &lpar;25&rpar; ------ 第五章 加载实体和导航属性之加载完整的对象图和派生类型上的导航属性

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-5  加载完整的对象图 问题 你有一个包含许多关联实体的模型,你想在一次查询中, ...

  2. 该用 QGraphicsView &quest; QtQuick-QML &quest;

    目前QtQuick (2014/3/6) 已经发展了有一段时间了,很多人在用因此我也想看看是否适合我目前的项目. 我要做的是一个类似3DMax中的材质编辑器的东西,里面有成千上万的”表单“(不知道怎么 ...

  3. redis主从复制操作

    1. 33.10服务器上 启动三个redis bin/redis-server etc/redis.conf bin/redis-server etc/6380conf bin/redis-serve ...

  4. 用delphiXE7 dbExpress Framework提供的功能获取数据表信息

    uses +  Data.DBXMetaDataNames procedure TMainForm.Button2Click(Sender: TObject);var  Cmd: TDBXComman ...

  5. 调用test case集,并生成测试报告

    结构是 test_all.py 进行配置,执行所有测试用例集,并合并测试报告到同一个文件 #test_all.py 进行配置,执行所有测试用例集 # coding = utf-8 from time ...

  6. ng-options

    tr td 地点 td select.form-control(required="true" ng-model="addkc.dd" ng-options=& ...

  7. linux下使用vi操作

    ESC : 进入命令模式 linux下使用vi后,怎样跳转到文件结尾 pagedown键连续按 虽然我也这么用,但还是太笨了.问了高手,方法是按shift+g,另外,到文件开头是gg.   linux ...

  8. JS的 try catch使用心得

    try{ //正常执行 }catch(e/*你感觉会出错的 错误类型*/){ // 可能出现的意外 eg:用户自己操作失误 或者 函数少条件 不影响下面的函数执行 // 有时也会用在 比如 focus ...

  9. JavaScript中DOM(第二天)

    DOM document object model,文档对象模型,也叫dom树:dom是由节点组成的.html标签称为标签节点,属性称为属性节点: console.log(docment);即可输出d ...

  10. 【代码笔记】iOS-自定义loading

    一,效果图. 二,工程图. 三, 代码. ViewController.h #import <UIKit/UIKit.h> //loading #import "GPLoadin ...