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

Merge pull request !358 from 与或非/issues/I5NQLD
pull/2/head
芋道源码 2023-01-07 01:15:49 +00:00 committed by Gitee
commit c958d9b659
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 39 additions and 18 deletions

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.visualization.framework.jmreport.core.service; package cn.iocoder.yudao.module.visualization.framework.jmreport.core.service;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.framework.security.core.LoginUser;
@ -11,6 +12,8 @@ import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespD
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; import org.jeecg.modules.jmreport.api.JmReportTokenServiceI;
import java.util.Objects;
/** /**
* {@link JmReportTokenServiceI} Token * {@link JmReportTokenServiceI} Token
* *
@ -29,8 +32,40 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI {
*/ */
@Override @Override
public Boolean verifyToken(String token) { public Boolean verifyToken(String token) {
Long userId = SecurityFrameworkUtils.getLoginUserId();
if (!Objects.isNull(userId)) {
return true;
}
return buildLoginUserByToken(token) != null;
}
/**
*
* <p>
* username
*
* @param token JmReport token
* @return
*/
@Override
public String getUsername(String token) {
Long userId = SecurityFrameworkUtils.getLoginUserId();
if (ObjectUtil.isNotNull(userId)) {
return String.valueOf(userId);
}
LoginUser user = buildLoginUserByToken(token);
return user == null ? null : String.valueOf(user.getId());
}
/**
* token
*
* @param token token
* @return token
*/
private LoginUser buildLoginUserByToken(String token) {
if (StrUtil.isEmpty(token)) { if (StrUtil.isEmpty(token)) {
return false; return null;
} }
// TODO 如下的实现不算特别优雅,主要咱是不想搞的太复杂,所以参考对应的 Filter 先实现了 // TODO 如下的实现不算特别优雅,主要咱是不想搞的太复杂,所以参考对应的 Filter 先实现了
@ -41,7 +76,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI {
try { try {
OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token); OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token);
if (accessToken == null) { if (accessToken == null) {
return false; return null;
} }
user = new LoginUser().setId(accessToken.getUserId()).setUserType(accessToken.getUserType()) user = new LoginUser().setId(accessToken.getUserId()).setUserType(accessToken.getUserType())
.setTenantId(accessToken.getTenantId()).setScopes(accessToken.getScopes()); .setTenantId(accessToken.getTenantId()).setScopes(accessToken.getScopes());
@ -49,7 +84,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI {
// do nothing如果报错说明认证失败则返回 false 即可 // do nothing如果报错说明认证失败则返回 false 即可
} }
if (user == null) { if (user == null) {
return false; return null;
} }
SecurityFrameworkUtils.setLoginUser(user, WebFrameworkUtils.getRequest()); SecurityFrameworkUtils.setLoginUser(user, WebFrameworkUtils.getRequest());
@ -57,21 +92,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI {
// 目的:基于 LoginUser 获得到的租户编号,设置到 Tenant 上下文,避免查询数据库时的报错 // 目的:基于 LoginUser 获得到的租户编号,设置到 Tenant 上下文,避免查询数据库时的报错
TenantContextHolder.setIgnore(false); TenantContextHolder.setIgnore(false);
TenantContextHolder.setTenantId(user.getTenantId()); TenantContextHolder.setTenantId(user.getTenantId());
return true; return user;
}
/**
*
*
* username
*
* @param token JmReport token
* @return
*/
@Override
public String getUsername(String token) {
Long userId = SecurityFrameworkUtils.getLoginUserId();
return userId != null ? String.valueOf(userId) : null;
} }
} }