Commit c958d9b659e0eca7f4e112c3cb3bd5e11b1154e5
Committed by
Gitee
!358 积木报表的部分请求会报错:JmReportTokenServices实现类getUsername方法返回值不允许为空
Merge pull request !358 from 与或非/issues/I5NQLD
Showing
1 changed file
with
39 additions
and
18 deletions
yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/core/service/JmReportTokenServiceImpl.java
| 1 | package cn.iocoder.yudao.module.visualization.framework.jmreport.core.service; | 1 | package cn.iocoder.yudao.module.visualization.framework.jmreport.core.service; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.util.ObjectUtil; | ||
| 3 | import cn.hutool.core.util.StrUtil; | 4 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.iocoder.yudao.framework.common.exception.ServiceException; | 5 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 5 | import cn.iocoder.yudao.framework.security.core.LoginUser; | 6 | import cn.iocoder.yudao.framework.security.core.LoginUser; |
| @@ -11,6 +12,8 @@ import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespD | @@ -11,6 +12,8 @@ import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespD | ||
| 11 | import lombok.RequiredArgsConstructor; | 12 | import lombok.RequiredArgsConstructor; |
| 12 | import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; | 13 | import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; |
| 13 | 14 | ||
| 15 | +import java.util.Objects; | ||
| 16 | + | ||
| 14 | /** | 17 | /** |
| 15 | * {@link JmReportTokenServiceI} 实现类,提供积木报表的 Token 校验、用户信息的查询等功能 | 18 | * {@link JmReportTokenServiceI} 实现类,提供积木报表的 Token 校验、用户信息的查询等功能 |
| 16 | * | 19 | * |
| @@ -29,8 +32,40 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | @@ -29,8 +32,40 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | ||
| 29 | */ | 32 | */ |
| 30 | @Override | 33 | @Override |
| 31 | public Boolean verifyToken(String token) { | 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 | if (StrUtil.isEmpty(token)) { | 67 | if (StrUtil.isEmpty(token)) { |
| 33 | - return false; | 68 | + return null; |
| 34 | } | 69 | } |
| 35 | // TODO 如下的实现不算特别优雅,主要咱是不想搞的太复杂,所以参考对应的 Filter 先实现了 | 70 | // TODO 如下的实现不算特别优雅,主要咱是不想搞的太复杂,所以参考对应的 Filter 先实现了 |
| 36 | 71 | ||
| @@ -41,7 +76,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | @@ -41,7 +76,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | ||
| 41 | try { | 76 | try { |
| 42 | OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token); | 77 | OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token); |
| 43 | if (accessToken == null) { | 78 | if (accessToken == null) { |
| 44 | - return false; | 79 | + return null; |
| 45 | } | 80 | } |
| 46 | user = new LoginUser().setId(accessToken.getUserId()).setUserType(accessToken.getUserType()) | 81 | user = new LoginUser().setId(accessToken.getUserId()).setUserType(accessToken.getUserType()) |
| 47 | .setTenantId(accessToken.getTenantId()).setScopes(accessToken.getScopes()); | 82 | .setTenantId(accessToken.getTenantId()).setScopes(accessToken.getScopes()); |
| @@ -49,7 +84,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | @@ -49,7 +84,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | ||
| 49 | // do nothing:如果报错,说明认证失败,则返回 false 即可 | 84 | // do nothing:如果报错,说明认证失败,则返回 false 即可 |
| 50 | } | 85 | } |
| 51 | if (user == null) { | 86 | if (user == null) { |
| 52 | - return false; | 87 | + return null; |
| 53 | } | 88 | } |
| 54 | SecurityFrameworkUtils.setLoginUser(user, WebFrameworkUtils.getRequest()); | 89 | SecurityFrameworkUtils.setLoginUser(user, WebFrameworkUtils.getRequest()); |
| 55 | 90 | ||
| @@ -57,21 +92,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | @@ -57,21 +92,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | ||
| 57 | // 目的:基于 LoginUser 获得到的租户编号,设置到 Tenant 上下文,避免查询数据库时的报错 | 92 | // 目的:基于 LoginUser 获得到的租户编号,设置到 Tenant 上下文,避免查询数据库时的报错 |
| 58 | TenantContextHolder.setIgnore(false); | 93 | TenantContextHolder.setIgnore(false); |
| 59 | TenantContextHolder.setTenantId(user.getTenantId()); | 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 | } |