msal auzer 强制刷新获取令牌

news/2024/7/15 17:14:58 标签: flask, python, 后端, react.js, java, javascript, 前端

背景:msal auzer token 过期时间微软默认事60至90分钟,普遍取中间值,现渗透测试部分(Qtester)要求30分token 过期。且不可使用msal的安全机制。

解决方案:‘

后端,解析token 获取发证时间  iat或nbf计算token 过期时间

msal token 都是提前生产好的。为了解决高并发,所以这里获取的token 大概事5分钟前生成的。

前端前置刷新token ,我在官网找了半天也没找到。还是在源码找到的

刷新token 的 api【 acquireTokenSilent】

源码:

1、点击acquireTokenSilent进入如图代码

再点击SilentRequest

javascript">import { AccountInfo, CommonSilentFlowRequest, StringDict } from "@azure/msal-common";
import { CacheLookupPolicy } from "../utils/BrowserConstants";
/**
 * SilentRequest: Request object passed by user to retrieve tokens from the
 * cache, renew an expired token with a refresh token, or retrieve a code (first leg of authorization code grant flow)
 * in a hidden iframe.
 *
 * - scopes                 - Array of scopes the application is requesting access to.
 * - authority              - Url of the authority which the application acquires tokens from.
 * - correlationId          - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
 * - account                - Account entity to lookup the credentials.
 * - forceRefresh           - Forces silent requests to make network calls if true.
 * - extraQueryParameters   - String to string map of custom query parameters added to the /authorize call. Only used when renewing the refresh token.
 * - tokenQueryParameters   - String to string map of custom query parameters added to the /token call. Only used when renewing access tokens.
 * - redirectUri            - The redirect URI where authentication responses can be received by your application. It must exactly match one of the redirect URIs registered in the Azure portal. Only used for cases where refresh token is expired.
 * - cacheLookupPolicy      - Enum of different ways the silent token can be retrieved.
 * - prompt                 - Indicates the type of user interaction that is required.
 *          none:  will ensure that the user isn't presented with any interactive prompt. if request can't be completed via single-sign on, the endpoint will return an interaction_required error
 *          no_session: will not read existing session token when authenticating the user. Upon user being successfully authenticated, EVO won’t create a new session for the user. FOR INTERNAL USE ONLY.
 */
export declare type SilentRequest = Omit<CommonSilentFlowRequest, "authority" | "correlationId" | "forceRefresh" | "account" | "requestedClaimsHash"> & {
    redirectUri?: string;
    extraQueryParameters?: StringDict;
    authority?: string;
    account?: AccountInfo;
    correlationId?: string;
    forceRefresh?: boolean;
    cacheLookupPolicy?: CacheLookupPolicy;
    prompt?: string;
};
//# sourceMappingURL=SilentRequest.d.ts.map

实现代码

javascript">export const onAcquireTokenSilent = async (dispatch,userInfo) => {
  // 通过用户名获取用户信息
  const account = msalInstance.getAccountByUsername(userInfo?.username);
  const accessTokenRequest = {
    scopes: [process.env.SCOPE],
    account: account,
    forceRefresh :true //  - forceRefresh - Forces silent requests to make network calls if true.
  };
  // 有了用户信息可以获取token
  const accessTokenResponse = await msalInstance.acquireTokenSilent(accessTokenRequest);
  if(accessTokenResponse && accessTokenResponse?.accessToken){

    const accessToken = accessTokenResponse.accessToken;
  return accessToken;
  }

};

这样就实现强制刷新。。 


http://www.niftyadmin.cn/n/5277519.html

相关文章

UNet医学图像分割网络

UNet网络结构 对于医学图像的分割任务&#xff0c;这里使用UNet网络实现CT影响的病灶区域分割任务。记一篇学习笔记。 1、UNet网络结构 原始图片大小为(512, 512), 根据CT数据像素值分布的特征&#xff0c;对于image保留[-1024, 1024]范围内的像素&#xff0c;并归一 化处理到…

DRF从入门到精通二(Request源码分析、DRF之序列化组件)

文章目录 一、Request对象源码分析区分原生request和新生request新的request还能像原来的reqeust一样使用吗源码片段分析总结&#xff1a; 二、DRF之序列化组件序列化介绍序列化步骤序列化组件的基本使用反序列化基本使用反序列化的新增反序列化的新增删除单条 反序列化的校验 …

Element UI各种使用问题汇总(Input、Form篇)

Input回车导致页面刷新的问题 Element UI为了遵守W3C规范特意设置的&#xff0c;就是当Form中只有一个Input的时候&#xff0c;Form把这个事件当成了是提交表单的操作&#xff0c;所以页面会刷新。 解决方法&#xff1a; 凡是<el-form>里面只有一个Input&#xff0c;就…

【正点原子STM32连载】第十七章 通用定时器中断实验 摘自【正点原子】APM32E103最小系统板使用指南

第十七章 通用定时器中断实验 本章介绍APM32E103通用定时器的使用&#xff0c;通用定时器相较于基本定时器&#xff0c;拥有输入捕获和输出比较等功能&#xff0c;这些功能可以用来测量脉冲宽度、频率和占空比&#xff0c;并且可以产生并输出波形等。通过本章的学习&#xff0…

Git版本控制-基本命令

基本命令 branch切换 创建 branch 分支名切换到指定分支 git checkout 分支名pull代码 拉取代码从远程仓库的master分支拉取最新更改到本地的master分支。 git pull origin mastercommit本地 记录文件所做的更改添加到缓存区->将这些更改提交到本地仓库并附带消息。 …

【Python】—— pandas 数据分析

pandas 数据分析 相关知识了解1. 数据清理2. 探索性数据分析&#xff08;EDA&#xff09;3. 数据过滤和选择4. 数据分组和聚合5. 数据合并和连接6. 时间序列分析7. 统计分析 第1关&#xff1a;了解数据集特征第2关&#xff1a;DataFrame 的 CRUD第3关&#xff1a;利用 pandas 实…

VMware虚拟机安装CentOS 7操作系统的步骤及方法

目录 1、安装前的准备工作 2、VMware虚拟机创建CentOS 7操作系统 步骤一&#xff1a;以管理员的身份运行VMware虚拟机 步骤二&#xff1a;新建虚拟机 步骤三&#xff1a;选择类型配置 步骤四&#xff1a;选择安装客户机操作系统 步骤五&#xff1a;命名虚拟机 步骤六&a…

JVM中的虚拟机栈的动态链接部分存放到底是什么

在Java虚拟机&#xff08;JVM&#xff09;中&#xff0c;每个线程在执行一个方法时都会创建一个栈帧&#xff08;Stack Frame&#xff09;&#xff0c;栈帧中包含了方法的运行时数据。栈帧通常包括局部变量表、操作数栈、动态链接、方法返回地址等部分。 动态链接 动态链接&a…