Commit 80ab569fa7e16fadf22331ddb265b809a6e0b210

Authored by gaibu
1 parent 24f0e4dd

fix: 积木报表的部分请求会报错

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