Commit c958d9b659e0eca7f4e112c3cb3bd5e11b1154e5

Authored by 芋道源码
Committed by Gitee
2 parents 24f0e4dd a8001a11

!358 积木报表的部分请求会报错:JmReportTokenServices实现类getUsername方法返回值不允许为空

Merge pull request !358 from 与或非/issues/I5NQLD
yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/core/service/JmReportTokenServiceImpl.java
1 1 package cn.iocoder.yudao.module.visualization.framework.jmreport.core.service;
2 2  
  3 +import cn.hutool.core.util.ObjectUtil;
3 4 import cn.hutool.core.util.StrUtil;
4 5 import cn.iocoder.yudao.framework.common.exception.ServiceException;
5 6 import cn.iocoder.yudao.framework.security.core.LoginUser;
... ... @@ -11,6 +12,8 @@ import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespD
11 12 import lombok.RequiredArgsConstructor;
12 13 import org.jeecg.modules.jmreport.api.JmReportTokenServiceI;
13 14  
  15 +import java.util.Objects;
  16 +
14 17 /**
15 18 * {@link JmReportTokenServiceI} 实现类,提供积木报表的 Token 校验、用户信息的查询等功能
16 19 *
... ... @@ -29,8 +32,40 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI {
29 32 */
30 33 @Override
31 34 public Boolean verifyToken(String token) {
  35 + Long userId = SecurityFrameworkUtils.getLoginUserId();
  36 + if (!Objects.isNull(userId)) {
  37 + return true;
  38 + }
  39 + return buildLoginUserByToken(token) != null;
  40 + }
  41 +
  42 + /**
  43 + * 获得用户编号
  44 + * <p>
  45 + * 虽然方法名获得的是 username,实际对应到项目中是用户编号
  46 + *
  47 + * @param token JmReport 前端传递的 token
  48 + * @return 用户编号
  49 + */
  50 + @Override
  51 + public String getUsername(String token) {
  52 + Long userId = SecurityFrameworkUtils.getLoginUserId();
  53 + if (ObjectUtil.isNotNull(userId)) {
  54 + return String.valueOf(userId);
  55 + }
  56 + LoginUser user = buildLoginUserByToken(token);
  57 + return user == null ? null : String.valueOf(user.getId());
  58 + }
  59 +
  60 + /**
  61 + * 基于 token 构建登录用户
  62 + *
  63 + * @param token token
  64 + * @return 返回 token 对应的用户信息
  65 + */
  66 + private LoginUser buildLoginUserByToken(String token) {
32 67 if (StrUtil.isEmpty(token)) {
33   - return false;
  68 + return null;
34 69 }
35 70 // TODO 如下的实现不算特别优雅,主要咱是不想搞的太复杂,所以参考对应的 Filter 先实现了
36 71  
... ... @@ -41,7 +76,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI {
41 76 try {
42 77 OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token);
43 78 if (accessToken == null) {
44   - return false;
  79 + return null;
45 80 }
46 81 user = new LoginUser().setId(accessToken.getUserId()).setUserType(accessToken.getUserType())
47 82 .setTenantId(accessToken.getTenantId()).setScopes(accessToken.getScopes());
... ... @@ -49,7 +84,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI {
49 84 // do nothing:如果报错,说明认证失败,则返回 false 即可
50 85 }
51 86 if (user == null) {
52   - return false;
  87 + return null;
53 88 }
54 89 SecurityFrameworkUtils.setLoginUser(user, WebFrameworkUtils.getRequest());
55 90  
... ... @@ -57,21 +92,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI {
57 92 // 目的:基于 LoginUser 获得到的租户编号,设置到 Tenant 上下文,避免查询数据库时的报错
58 93 TenantContextHolder.setIgnore(false);
59 94 TenantContextHolder.setTenantId(user.getTenantId());
60   - return true;
61   - }
62   -
63   - /**
64   - * 获得用户编号
65   - *
66   - * 虽然方法名获得的是 username,实际对应到项目中是用户编号
67   - *
68   - * @param token JmReport 前端传递的 token
69   - * @return 用户编号
70   - */
71   - @Override
72   - public String getUsername(String token) {
73   - Long userId = SecurityFrameworkUtils.getLoginUserId();
74   - return userId != null ? String.valueOf(userId) : null;
  95 + return user;
75 96 }
76 97  
77 98 }
... ...