From 9ddf9961bb4d1e9e6cac8557b20154f4b62796ec Mon Sep 17 00:00:00 2001 From: Parker Date: Sat, 16 Sep 2023 10:39:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E8=BF=87=E7=A8=8B=E4=B8=AD=E9=AA=8C=E8=AF=81Jwt=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=90=8E=20=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/JwtAuthenticationTokenFilter.java | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/security/filter/JwtAuthenticationTokenFilter.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/security/filter/JwtAuthenticationTokenFilter.java index 2bbb0ac..c483e2b 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/security/filter/JwtAuthenticationTokenFilter.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/security/filter/JwtAuthenticationTokenFilter.java @@ -15,6 +15,7 @@ */ package org.opsli.core.security.filter; +import cn.hutool.json.JSONUtil; import lombok.AllArgsConstructor; import org.opsli.core.base.dto.LoginUserDto; import org.opsli.core.security.service.UidUserDetailDetailServiceImpl; @@ -22,6 +23,8 @@ import org.opsli.core.utils.UserTokenUtil; import org.opsli.plugins.security.authentication.AfterAuthenticationToken; import org.opsli.plugins.security.exception.AuthException; import org.opsli.plugins.security.exception.errorcode.AuthErrorCodeEnum; +import org.opsli.plugins.security.utils.WebUtils; +import org.opsli.plugins.security.vo.AuthResultWrapper; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.util.StringUtils; @@ -56,26 +59,41 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter { return; } - // 验证Token - UserTokenUtil.verify(token); + try { + // 验证Token + UserTokenUtil.verify(token); - // 获得登陆用户信息 - LoginUserDto loginUserDto = UserTokenUtil.getLoginUserDto(token) - // 认证无效 - .orElseThrow(() -> new AuthException(AuthErrorCodeEnum.AUTH_AUTH_INVALID)); + // 获得登陆用户信息 + LoginUserDto loginUserDto = UserTokenUtil.getLoginUserDto(token) + // 认证无效 + .orElseThrow(() -> new AuthException(AuthErrorCodeEnum.AUTH_AUTH_INVALID)); - // 这里用Uid 获取用户信息,因为涉及到超管切换租户身份 - // 非 租户系统 可以直接使用 用户名获取信息 - UserDetails userDetails = uidUserDetailDetailService.loadUserByPrincipal(loginUserDto.getUid()) - // 认证无效 - .orElseThrow(() -> new AuthException(AuthErrorCodeEnum.AUTH_AUTH_INVALID)); + // 这里用Uid 获取用户信息,因为涉及到超管切换租户身份 + // 非 租户系统 可以直接使用 用户名获取信息 + UserDetails userDetails = uidUserDetailDetailService.loadUserByPrincipal(loginUserDto.getUid()) + // 认证无效 + .orElseThrow(() -> new AuthException(AuthErrorCodeEnum.AUTH_AUTH_INVALID)); - AfterAuthenticationToken authenticationToken = - new AfterAuthenticationToken(userDetails, null, userDetails.getAuthorities()); - - SecurityContextHolder.getContext().setAuthentication(authenticationToken); + AfterAuthenticationToken authenticationToken = + new AfterAuthenticationToken(userDetails, null, userDetails.getAuthorities()); + SecurityContextHolder.getContext().setAuthentication(authenticationToken); + }catch (AuthException ae){ + // 权校验Token异常 + Integer code = ae.getCode(); + String errorMessage = ae.getErrorMessage(); + AuthResultWrapper customResultWrapper = + AuthResultWrapper.getCustomResultWrapper(code, errorMessage); + WebUtils.renderString(request, response, JSONUtil.toJsonStr(customResultWrapper)); + return; + }catch (Exception e){ + // 其他异常 + AuthResultWrapper customResultWrapper = AuthResultWrapper.getErrorResultWrapper(); + WebUtils.renderString(request, response, JSONUtil.toJsonStr(customResultWrapper)); + return; + } //放行 filterChain.doFilter(request, response); } + }