JAVA开发(token过期续期)

article/2025/6/29 6:17:44

一、需求背景:

        在使用token进行登录的过程中,如果token过期了,需要重新输入用户名和密码登录,这种体验肯定是不好的,因为如果一直在使用系统,系统应该一直能够保持登录状态,而不是用着用着就突然退出系统重新登录。

二、解决方法:

生成token的接口需要提供,token和刷新的refresh_token ,过期时间。当时间快过期时,重新调用后端接口去获取新的token。

token:包含了凭证相关信息(过期时间相对refresh_token短)

refresh_token:包含凭证相关信息(过期时间相对token长)

过期时间:给客户端计算何时需要使用refresh_token来换取新的token

生成token参照,里面在加上 refresh_token和过期时间就可以。

JAVA开发(JWTUtil工具类实现token源码赏析)_茅河野人的博客-CSDN博客

三、后端验证token的示例:

既可以通过过滤器,也可以使用拦截器来拦截。

import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties;
import reactor.core.publisher.Mono;/*** 网关鉴权*/
@Component
public class AuthFilter implements GlobalFilter, Ordered
{private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;// 排除过滤的 uri 地址,nacos自行添加@Autowiredprivate IgnoreWhiteProperties ignoreWhite;@Resource(name = "stringRedisTemplate")private ValueOperations<String, String> sops;@Autowiredprivate RedisService redisService;@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain){String url = exchange.getRequest().getURI().getPath();// 跳过不需要验证的路径if (StringUtils.matches(url, ignoreWhite.getWhites())){return chain.filter(exchange);}String token = getToken(exchange.getRequest());if (StringUtils.isBlank(token)){return setUnauthorizedResponse(exchange, "令牌不能为空");}String userStr = sops.get(getTokenKey(token));if (StringUtils.isNull(userStr)){return setUnauthorizedResponse(exchange, "登录状态已过期");}JSONObject obj = JSONObject.parseObject(userStr);String userid = obj.getString("userid");String username = obj.getString("username");if (StringUtils.isBlank(userid) || StringUtils.isBlank(username)){return setUnauthorizedResponse(exchange, "令牌验证失败");}// 设置过期时间redisService.expire(getTokenKey(token), EXPIRE_TIME);// 设置用户信息到请求ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid).header(CacheConstants.DETAILS_USERNAME, username).build();ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();return chain.filter(mutableExchange);}private Mono<Void> setUnauthorizedResponse(ServerWebExchange exchange, String msg){ServerHttpResponse response = exchange.getResponse();response.getHeaders().setContentType(MediaType.APPLICATION_JSON);response.setStatusCode(HttpStatus.OK);log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath());return response.writeWith(Mono.fromSupplier(() -> {DataBufferFactory bufferFactory = response.bufferFactory();return bufferFactory.wrap(JSON.toJSONBytes(R.fail(msg)));}));}private String getTokenKey(String token){return CacheConstants.LOGIN_TOKEN_KEY + token;}/*** 获取请求token*/private String getToken(ServerHttpRequest request){String token = request.getHeaders().getFirst(CacheConstants.HEADER);if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX)){token = token.replace(CacheConstants.TOKEN_PREFIX, "");}return token;}@Overridepublic int getOrder(){return -200;}
}

 


http://chatgpt.dhexx.cn/article/HWy3FHon.shtml

相关文章

token系统讲解及过期处理

token系统讲解及过期处理 1. token是什么&#xff1f;用来做什么2. token存储在哪&#xff1f;过期了怎么办&#xff1f;3. 请求拦截与响应拦截执行时机&#xff08;面试重点&#xff09;4. 解决token过期方案一&#xff1a; 重新登录5. 方案二&#xff1a;使用 refresh_token…

前端token知识梳理:token如何存储?token过期如何处理?如何无感知刷新token?

在前后端是以token&#xff08;令牌&#xff09;的形式交互的&#xff0c;既然是token&#xff0c;是有过期时间的&#xff08;为了接口数据的安全&#xff0c; 服务器的token不会设置太久&#xff0c;根据需要一般是1-7天&#xff09;&#xff0c;没有一个token是永久的&…

处理token过期的问题,无感知刷新token

401错误的场景 有如下两种情况会出现401错误&#xff1a; 未登陆用户做一些需要权限才能做的操作&#xff0c;代码会报出401错误。这种情况下&#xff0c;应该让用户回到登陆页。登录用户的token过期了 整体目标是&#xff1a;通过axios响应拦截器来处理401问题。 理解toke…

前端怎么判断token过期或无token问题

有如下两种情况会出现401错误&#xff1a; 未登陆用户做一些需要权限才能做的操作&#xff08;例如&#xff1a;关注作者&#xff09;&#xff0c;代码会报出401错误。这种情况下&#xff0c;应该让用户回到登陆页。登录用户的token过期了 ( token会有有效期&#xff08;具体是…

react中 token过期,如何处理?

思路&#xff1a;首先在request.js中的响应拦截器这块写token失效的提示&#xff0c;然后再让它进行清空token和路由跳转 第一步&#xff1a;在request.js文件中 导入 import store from /store import { logout } from /store/actions/login 第二步&#xff1a;在响应拦截器…

token 过期解决

vue如何在token过期之后跳转到登录页面&#xff0c;且不影响其他无需携带token的接口数据访问 事情是这样的&#xff0c;最近做了一个类似于商城的项目。本来测试是没有问题的&#xff0c;后来过了大概三四天的时间没有在浏览器中打开过&#xff0c;再打开以后&#xff0c;在未…

前端token知识梳理:token如何存储?token过期如何处理?如何无感刷新token?

在前后端是以token的形式交互&#xff0c;既然是token&#xff0c;那么肯定有它的过期时间(为了接口数据的安全&#xff0c;服务器的token一般不会设置太长&#xff0c;根据需要一般是1-7天的样子)&#xff0c;没有一个token是永久的&#xff0c;永久的token就相当于一串永久的…

token过期怎么办 无感刷新token

&#xff08;1&#xff09;可以通过响应拦截器或者全局前置守卫强制跳转登录页 // 全局前置守卫 router.beforeEach((to, from) > {let token sessionStorage.token;if (token) {return true} else {return { name: Login, query: { redirect: to.fullPath } };} }) // 添…

关于 Token 过期问题的两种解决方案

对于token过期&#xff0c;我们有两种方案&#xff1a; 方案一&#xff1a;当我们操作某个需要token作为请求头的接口时&#xff0c;返回的数据错误error.response.status 401&#xff0c;说明我们的token已经过期了。 我们希望当响应返回的数据是401身份过期时&#xff0c;让…

Latex 符号(Symbols)

Latex符号广泛用于数百个类别的不同主题中。对于数学或其他学科中使用的每个符号&#xff0c;将使用相应的命令。本主题将为您提供有关符号的详细概念和说明&#xff0c;以及以哪种方式可以使用所有符号。 使用标准文本&#xff0c;您可以使用任何符号。这种方法不仅节省了精力…

Latex特殊符号汇集

原文链接&#xff1a;https://blog.csdn.net/ying_xu/article/details/51240291 这段时间用Latex很多&#xff0c;常常需要查阅相关特殊的符号&#xff0c;这里做一个整理&#xff0c;也方便大家查阅。 摘自&#xff1a;《一份不太简短的LATEX2介绍》或112分钟学会LATEX2 原…

(13.1)Latex符号、公式及伪代码

文章目录 一、符号大全和小工具1、符号大全2、小工具&#xff08;1&#xff09;图片转Latex&#xff08;2&#xff09;在线Latex公式编辑 二、公式格式三、伪代码四、注意事项1、符号宏包2、颜色宏包3、换段未缩进4、单引号和双引号 一、符号大全和小工具 1、符号大全 Latex …

LaTex常见数学符号与示例

在写文档或者博客过程中&#xff0c;数学公式是最难表示的&#xff0c;比如根号&#xff0c;下标号&#xff0c;分号&#xff0c;还有绝对值的表示&#xff0c;复杂一些的比如矩阵的表示&#xff0c;积分的表示&#xff0c;求和公式。 LaTex提供了丰富的表示方法&#xff0c;用…

Latex数学符号对应表

Latex数学符号对应表 - 叮叮当当sunny - 博客园 目录 1. 希腊字母2. 运算符符号3. 关系符号4. 箭头符号5. 括号符号6. 其他符号7. MATLAB 回到顶部 1. 希腊字母 字母实现字母实现αα\alphaAA\Alphaββ\betaBB\Betaγγ\gammaΓΓ\Gammaδδ\deltaΔΔ\Deltaϵϵ\epsilon…

LateX各种命令符号

函数、符号及特殊字符 声调 语法效果语法效果语法效果\bar{x}\acute{\eta}\check{\alpha}\grave{\eta}\breve{a}\ddot{y}\dot{x}\hat{\alpha}\tilde{\iota} 函数 语法效果语法效果语法效果\sin\theta\cos\theta\tan\theta\arcsin\frac{L}{r}\arccos\frac{T}{r}\arctan\frac{L}{…

Latex特殊符号大全(高清)

Latex符号大全 转载内容供自己阅读 原文&#xff1a;Latex特殊符号大全(高清)

Latex常见符号对照表

摘要: Latex可以很方便的利用命令来生成各式各样的特殊符号. 这里根据官方的文档将这些常见符号列出, 以备查用. B.1 希伯来和希腊字母(Hebrew and Greek letters) 希伯来语: 希腊语: 数学表达式中常常用下面的这些符号, 分小写/大写 B.2 二元关系符(Binary relations) …

LaTeX最全的数学符号大全(更新中…… )

文章目录 1 基本字符常用数集字母字母加标表示希腊字母 2 四则运算运算符号等号比较范围 3 常用函数4 离散数学运算符5 集合集合表示集合操作集合关系 最后更新于2020/12/02 1 基本字符 常用数集字母 符号 LaTeX \LaTeX LATE​X符号说明示例 N \mathbb{N} N\mathbb{N}自然数集…

Latex所有常用数学符号吐血整理(包含大括号、等式对齐、矩阵)

果然是自己搞一个查起来会方便一些&#xff0c;最近天天写数学题解&#xff0c;全是公式 ~ 希腊字母&#xff1a; 字母名称国际音标大写字母小写字母字母名称国际音标大写字母小写字母alpha/lfə/Ααnu/nju:/Ννbeta/bi:tə/或 /beɪtə/Ββxi希腊 /ksi/&#xff1b;英美 …