From acd2ed7b7f7a431415bfeeffa293ce83a6638265 Mon Sep 17 00:00:00 2001 From: haozq <1611483981@qq.com> Date: Wed, 20 Aug 2025 09:34:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9C=8D=E5=8A=A1=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bonus/common/core/utils/JwtUtils.java | 7 +++++- .../com/bonus/gateway/filter/AuthFilter.java | 2 +- .../bmw/src/main/resources/static/js/jq.js | 17 +++++++++---- .../bmw/src/main/resources/static/js/main.js | 24 +++++++++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/common/common-core/src/main/java/com/bonus/common/core/utils/JwtUtils.java b/common/common-core/src/main/java/com/bonus/common/core/utils/JwtUtils.java index e19405b..630a457 100644 --- a/common/common-core/src/main/java/com/bonus/common/core/utils/JwtUtils.java +++ b/common/common-core/src/main/java/com/bonus/common/core/utils/JwtUtils.java @@ -39,7 +39,12 @@ public class JwtUtils */ public static Claims parseToken(String token) { - return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); + try{ + return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); + }catch (Exception e){ + System.err.println("token不正确--->"+token); + return null; + } } /** diff --git a/gateway/src/main/java/com/bonus/gateway/filter/AuthFilter.java b/gateway/src/main/java/com/bonus/gateway/filter/AuthFilter.java index 688e832..4669dbf 100644 --- a/gateway/src/main/java/com/bonus/gateway/filter/AuthFilter.java +++ b/gateway/src/main/java/com/bonus/gateway/filter/AuthFilter.java @@ -63,7 +63,7 @@ public class AuthFilter implements GlobalFilter, Ordered return chain.filter(exchange); } String token = getToken(request); - if (StringUtils.isEmpty(token)) + if (StringUtils.isEmpty(token) ) { return unauthorizedResponse(exchange, "令牌不能为空"); } diff --git a/modules/bmw/src/main/resources/static/js/jq.js b/modules/bmw/src/main/resources/static/js/jq.js index 4633c7b..1feb459 100644 --- a/modules/bmw/src/main/resources/static/js/jq.js +++ b/modules/bmw/src/main/resources/static/js/jq.js @@ -1,5 +1,14 @@ let Authorization = localStorage.getItem("smz-token"); + +$(document).ajaxSuccess(function (event, xhr, settings, data) { + if(data.code===401){ + localStorage.removeItem("smz-token"); + top.location.href = ctxPath + '/login.html'; + } + return data; +}); + $.ajaxSetup({ cache : false, headers : { @@ -18,12 +27,12 @@ $.ajaxSetup({ var response = JSON.parse(msg); var code = response.code; var message = response.message; - if (code == 400) { + if (code === 400) { layer.msg(message); - } else if (code == 401) { + } else if (code === 401) { localStorage.removeItem("smz-token"); - location.href = '/login.html'; - } else if (code == 403) { + top.location.href = ctxPath + '/login.html'; + } else if (code === 403) { console.log("未授权:" + message); layer.msg('未授权'); } else if (code == 500) { diff --git a/modules/bmw/src/main/resources/static/js/main.js b/modules/bmw/src/main/resources/static/js/main.js index 1371a30..029b503 100644 --- a/modules/bmw/src/main/resources/static/js/main.js +++ b/modules/bmw/src/main/resources/static/js/main.js @@ -20,6 +20,12 @@ function initMenu() { location.href = ctxPath + '/login.html'; return; } + if (data.code==401) { + //redis的token未获取到权限 + // alert("权限已过期,请重新登录"); + location.href = ctxPath + '/login.html'; + return; + } var menu = $("#menu"); $.each(data, function (i, item) { var a = $(""); @@ -48,6 +54,9 @@ function initMenu() { setChild(li, item.child) }); + }, + error : function (e){ + location.href = ctxPath + '/login.html'; } }); } @@ -93,6 +102,12 @@ function showLoginInfo() { }, async: false, success: function (data) { + if (data.code==401) { + //redis的token未获取到权限 + // alert("权限已过期,请重新登录"); + location.href = ctxPath + '/login.html'; + return; + } $(".admin-header-user span").text(data.nickname); var pro = window.location.protocol; @@ -130,8 +145,17 @@ function logout() { "Authorization": token }, success: function (data) { + if (data.code==401) { + //redis的token未获取到权限 + // alert("权限已过期,请重新登录"); + location.href = ctxPath + '/login.html'; + return; + } localStorage.removeItem("smz-token"); location.href = ctxPath + '/login.html'; + }, + error : function (e){ + location.href = ctxPath + '/login.html'; } }); // var userAgent = navigator.userAgent.toLowerCase();