问题描述

需要在APIM策略中对请求所携带的Cookie中的token值进行JWT验证,如果获取Cookie中的值并且作为变量保存,然后在JWT 验证中使用呢?

问题解答

第一步:获取Cookie中的Token值

使用C#语句 @(context.Request.Headers.GetValueOrDefault("cookie", "").Split(';').Select(x => x.Trim()).Select(cookie => cookie.Split('=')).SingleOrDefault(cookie => cookie[0] == "Token")?[1]) 获取到Token信息, 需要注意:Select中的lambda表达式需要根据实际情况进行修改。

示例Cookie信息如:

Cookie:    test=123; "test222=222222222222"; test222=222222222222; token=eyJ0**LCJhbGci**************************7Yat3****H5A; test111=ey***vDH5A

请求中所携带的Cookie截图:

第二步:把值保存为变量

使用set-variable 设置token变量,存储第一步中获取的值。

示例Policy为:

<set-variable name="token" 
value="@(context.Request.Headers.GetValueOrDefault("cookie", "").Split(';')
  .Select(x => x.Trim())
  .Select(cookie => cookie.Split('=')).SingleOrDefault(cookie => cookie[0] == "stored-token")?[1])"
/>

第三步:在JWT验证中获取变量值

使用 validate JWT 策略,使用 token-value="@(context.Variables.GetValueOrDefault<string>("token", "no value"))"  来代替 header-name="Authorization"

示例Policy为:

<validate-jwt 
  token-value="@(context.Variables.GetValueOrDefault<string>("token", "no value"))"
  failed-validation-httpcode="401"
  require-expiration-time="false"
  require-scheme="Bearer"
  require-signed-tokens="true">
<openid-config url=https://login.partner.microsoftonline.cn/<your azure tenant id>/v2.0/.well-known/openid-configuration />
  <audiences>
    <audience><your audience, GUID ></audience>
  </audiences>
</validate-jwt>

完整的Policy示例为:

<policies>
<inbound>
<base />
<set-variable name="token" value="@(context.Request.Headers.GetValueOrDefault("cookie", "").Split(';').Select(x => x.Trim()).Select(cookie => cookie.Split('=')).SingleOrDefault(cookie => cookie[0] == "token")?[1])" />
<validate-jwt token-value="@(context.Variables.GetValueOrDefault<string>("token", "no value"))" failed-validation-httpcode="401" require-expiration-time="false" require-scheme="Bearer" require-signed-tokens="true">
<openid-config url=https://login.partner.microsoftonline.cn/xxxx-xxxx-xxxx-xxxx-xxxx/v2.0/.well-known/openid-configuration />
<audiences>
<audience>xxxx-xxxx-xxxx-xxxx-xxxx</audience>
</audiences>
</validate-jwt>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
<choose>
<when condition="@(context.LastError.Source == "validate-jwt")">
<return-response>
<set-status code="302" reason="Unauthorized" />
<set-header name="Location" exists-action="override">
<value>https://login.partner.microsoftonline.cn/xxxx-xxxx-xxxx-xxxx-xxxx/oauth2/v2.0/authorize?response_type=code+id_token&amp;redirect_uri=<redirect_uri>&amp;client_id=%20xxxx-xxxx-xxxx-xxxx-xxxx&amp;scope=openid+profile+email&amp;response_mode=form_post&amp;nonce=eef3d47c873242ddb09b28ed1f997f1b_20230926163347&amp;state=redir%3D%252F</value>
</set-header>
</return-response>
</when>
</choose>
</on-error>
</policies>

参考资料

validate-jwt :https://learn.microsoft.com/en-us/azure/api-management/validate-jwt-policy

Set variable : https://learn.microsoft.com/en-us/azure/api-management/set-variable-policy#example

 

【Azure APIM】APIM 策略语句如何读取请求头中所携带的Cookie信息并保存为变量的更多相关文章

  1. shiro 获取请求头中的 sessionId

    前言: 在前后端项目中, 前端有可能会要求, 后台返回一个 sessionId 给他, 然后他在请求后台接口时, 把这个sessionId 带给后台, 后台拿到这个sessionId , 就能识别, ...

  2. post请求头中常见content-type(非常重要)

    定义和用法 enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码.默认地,表单数据会编码为 "application/x-www-form-urlencoded". ...

  3. 解决SpringCloud使用Feign跨服调用时header请求头中的信息丢失

    在使用SpringCloud进行Feign跨服调用时header请求头中的信息会丢失,是因为Feign是不会带上当前请求的Cookie信息和头信息的,这个时候就需要重写请求拦截. 1.需要重写Requ ...

  4. Http 请求头中的 Proxy-Connection

    平时用 Chrome 开发者工具抓包时,经常会见到 Proxy-Connection 这个请求头.之前一直没去了解什么情况下会产生它,也没去了解它有什么含义.最近看完<HTTP 权威指南> ...

  5. js 中ajax请求时设置 http请求头中的x-requestd-with= ajax

    今天发现 AngularJS 框架的$http服务提供的$http.get() /$http.post()的ajax请求中没有带 x-requested-with字段. 这样的话,后端的php 就无法 ...

  6. HTTP 请求头中的 Remote_Addr,X-Forwarded-For,X-Real-IP

    REMOTE_ADDR 表示发出请求的远程主机的 IP 地址,remote_addr代表客户端的IP,但它的值不是由客户端提供的,而是服务端根据客户端的ip指定的,当你的浏览器访问某个网站时,假设中间 ...

  7. shiro + jwt 实现 请求头中的 rememberMe 时间限制功能

    前言: 上一篇提出, 通过修改 rememberMe 的编码来实现 rememberMe的功能的设想, 事后我去尝试实现了一番, 发现太麻烦, 还是不要那么做吧. 程序还是要越简单越好. 那功能总是要 ...

  8. shiro 获取请求头中的 rememberMe

    前言: 上一篇提到了, 将 sessionId 放到请求头中去, 那rememberMe是否也可以放到请求头中去呢. 其实不管是sessionId还是rememberMe, shiro都会默认往coo ...

  9. Ajax 请求头中常见content-type

    四种常见的 POST 提交数据方式 HTTP 协议是以 ASCII 码传输,建立在 TCP/IP 协议之上的应用层规范.规范把 HTTP 请求分为三个部分:状态行.请求头.消息主体.协议规定 POST ...

  10. WebAPi获取请求头中对应键值

    /// <summary> /// 依据键获取请求头中值数据 /// </summary> /// <param name="request"> ...

随机推荐

  1. 基于javaPoet的缓存key优化实践

    一. 背景 在一次系统opsreview中,发现了一些服务配置了@Cacheable注解.@cacheable 来源于spring cache框架中,作用是使用aop的方式将数据库中的热数据缓存在re ...

  2. 【验证码逆向专栏】最新某验三代滑块逆向分析,干掉所有的 w 参数!

    声明 本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关! 本文章未经许 ...

  3. Unity2019使用Gradle命令行(编译)出安卓包

    在我所经历的项目组中有这几种方法来生成APK 直接在Unity生成APK,可以接入SDK 使用Unity导出Android Studio工程手动生成APK 使用Unity导出Android Studi ...

  4. python随机种子seed的作用(强化学习常用到)

    先上代码 import math import gym from gym import spaces, logger from gym.utils import seeding import nump ...

  5. 3.1 C/C++ 使用字符与指针

    C/C++语言是一种通用的编程语言,具有高效.灵活和可移植等特点.C语言主要用于系统编程,如操作系统.编译器.数据库等:C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统.图形用户界面 ...

  6. SpringCloud-01-Eureka Ribbon

    1.微服务技术 2.SpringCloud SpringCloud是目前国内使用最广泛的微服务框架.官网地址:https://spring.io/projects/spring-cloud. Spri ...

  7. Windows 7 Ultimate with Service Pack 1 (x64)

    Windows 7 Ultimate with Service Pack 1 (x64) 链接:https://pan.baidu.com/s/1ZHODDFlJPLQ3ydxZ4rfc3g 提取码: ...

  8. P9989 [Ynoi Easy Round 2023] TEST_69 题解

    题目链接: [Ynoi Easy Round 2023] TEST_69 首先GCD有比较良好的一些性质.我们观察到一次 \(GCD(a_i,x)\) 操作,会有以下两种变化. 如果 \(x \bmo ...

  9. 解决docker push镜像到私有仓库时的报错【http: server gave HTTP response to HTTPS client】

    一:解决docker push镜像到私有仓库时的报错[http: server gave HTTP response to HTTPS client]

  10. Oracle查询存在外键约束但未创建对应索引的情况

    1.Oracle提供的脚本 2.网络搜索到的脚本 3.改为可以指定用户的脚本 4.测试脚本使用 1.Oracle提供的脚本 如果要求管控严格,可以考虑使用Oracle官方提供的脚本. Script t ...