Skip to content

Commit

Permalink
修复引入多租户后,前端 <img /> 读取图片报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Dec 15, 2021
1 parent b99f364 commit abf61bf
Show file tree
Hide file tree
Showing 6 changed files with 671 additions and 727 deletions.
1,363 changes: 637 additions & 726 deletions sql/ruoyi-vue-pro.sql

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.adminserver.modules.infra.convert.file.InfFileConvert;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
Expand Down Expand Up @@ -64,6 +65,7 @@ public CommonResult<Boolean> deleteFile(@RequestParam("id") String id) {
@ApiOperation("下载文件")
@ApiImplicitParam(name = "path", value = "文件附件", required = true, dataTypeClass = MultipartFile.class)
public void getFile(HttpServletResponse response, @PathVariable("path") String path) throws IOException {
TenantContextHolder.setNullTenantId();
InfFileDO file = fileCoreService.getFile(path);
if (file == null) {
log.warn("[getFile][path({}) 文件不存在]", path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file.InfFileDO;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface InfFileCoreMapper extends BaseMapperX<InfFileDO> {

default Integer selectCountById(String id) {
return selectCount("id", id);
}

/**
* 基于 Path 获取文件
* 实际上,是基于 ID 查询
* 由于前端使用 <img /> 的方式获取图片,所以需要忽略租户的查询
*
* @param path 路径
* @return 文件
*/
@InterceptorIgnore(tenantLine = "true")
default InfFileDO selectByPath(String path) {
return selectById(path);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void validateFileExists(String id) {

@Override
public InfFileDO getFile(String path) {
return fileMapper.selectById(path);
return fileMapper.selectByPath(path);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public class TenantContextHolder {

private static final ThreadLocal<Long> TENANT_ID = new TransmittableThreadLocal<>();

/**
* 租户编号 - 空
*/
private static final Long TENANT_ID_NULL = 0L;

/**
* 获得租户编号。
*
Expand All @@ -33,6 +38,15 @@ public static Long getRequiredTenantId() {
return tenantId;
}

/**
* 在一些前端场景下,可能无法请求带上租户。例如说,<img /> 方式获取图片等
* 此时,暂时的解决方案,是在该接口的 Controller 方法上,调用该方法
* TODO 芋艿:思考有没更合适的方案,目标是去掉该方法
*/
public static void setNullTenantId() {
TENANT_ID.set(TENANT_ID_NULL);
}

public static void setTenantId(Long tenantId) {
TENANT_ID.set(tenantId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
throws ServletException, IOException {
LoginUser user = SecurityFrameworkUtils.getLoginUser();
assert user != null; // shouldNotFilter 已经校验
// 校验租户是否匹配。
if (!Objects.equals(user.getTenantId(), TenantContextHolder.getTenantId())) {
log.error("[doFilterInternal][租户({}) User({}/{}) 越权访问租户({}) URL({}/{})]",
user.getTenantId(), user.getId(), user.getUserType(),
Expand Down

0 comments on commit abf61bf

Please sign in to comment.