From 80ab569fa7e16fadf22331ddb265b809a6e0b210 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Fri, 6 Jan 2023 17:47:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=AF=E6=9C=A8=E6=8A=A5=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E9=83=A8=E5=88=86=E8=AF=B7=E6=B1=82=E4=BC=9A=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/JmReportTokenServiceImpl.java | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/core/service/JmReportTokenServiceImpl.java b/yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/core/service/JmReportTokenServiceImpl.java index 511465786..be26ce2e8 100644 --- a/yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/core/service/JmReportTokenServiceImpl.java +++ b/yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/core/service/JmReportTokenServiceImpl.java @@ -11,6 +11,8 @@ import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespD import lombok.RequiredArgsConstructor; import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; +import java.util.Objects; + /** * {@link JmReportTokenServiceI} 实现类,提供积木报表的 Token 校验、用户信息的查询等功能 * @@ -29,8 +31,40 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { */ @Override public Boolean verifyToken(String token) { + Long userId = SecurityFrameworkUtils.getLoginUserId(); + if (!Objects.isNull(userId)) { + return true; + } + return injectUserInfoByToken(token) != null; + } + + /** + * 获得用户编号 + *
+ * 虽然方法名获得的是 username,实际对应到项目中是用户编号 + * + * @param token JmReport 前端传递的 token + * @return 用户编号 + */ + @Override + public String getUsername(String token) { + Long userId = SecurityFrameworkUtils.getLoginUserId(); + if (Objects.isNull(userId)) { + LoginUser user = injectUserInfoByToken(token); + return user == null ? null : String.valueOf(user.getId()); + } + return String.valueOf(userId); + } + + /** + * 注入用户信息 + * + * @param token token + * @return 返回 token 对应的用户信息 + */ + private LoginUser injectUserInfoByToken(String token) { if (StrUtil.isEmpty(token)) { - return false; + return null; } // TODO 如下的实现不算特别优雅,主要咱是不想搞的太复杂,所以参考对应的 Filter 先实现了 @@ -41,7 +75,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { try { OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token); if (accessToken == null) { - return false; + return null; } user = new LoginUser().setId(accessToken.getUserId()).setUserType(accessToken.getUserType()) .setTenantId(accessToken.getTenantId()).setScopes(accessToken.getScopes()); @@ -49,7 +83,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { // do nothing:如果报错,说明认证失败,则返回 false 即可 } if (user == null) { - return false; + return null; } SecurityFrameworkUtils.setLoginUser(user, WebFrameworkUtils.getRequest()); @@ -57,21 +91,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { // 目的:基于 LoginUser 获得到的租户编号,设置到 Tenant 上下文,避免查询数据库时的报错 TenantContextHolder.setIgnore(false); TenantContextHolder.setTenantId(user.getTenantId()); - return true; - } - - /** - * 获得用户编号 - * - * 虽然方法名获得的是 username,实际对应到项目中是用户编号 - * - * @param token JmReport 前端传递的 token - * @return 用户编号 - */ - @Override - public String getUsername(String token) { - Long userId = SecurityFrameworkUtils.getLoginUserId(); - return userId != null ? String.valueOf(userId) : null; + return user; } }