diff --git a/backend/src/main/java/io/metersphere/api/controller/ShareInfoController.java b/backend/src/main/java/io/metersphere/api/controller/ShareInfoController.java index 142cc42741..b87badae71 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ShareInfoController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ShareInfoController.java @@ -81,9 +81,9 @@ public class ShareInfoController { return returnDTO; } - @PostMapping("/generateShareInfo") + @PostMapping("/generateShareInfoWithExpired") public ShareInfoDTO generateShareInfo(@RequestBody ShareInfo request) { - ShareInfo apiShare = shareInfoService.generateShareInfo(request); + ShareInfo apiShare = shareInfoService.createShareInfo(request); ShareInfoDTO returnDTO = shareInfoService.conversionShareInfoToDTO(apiShare); return returnDTO; } diff --git a/backend/src/main/java/io/metersphere/api/service/ShareInfoService.java b/backend/src/main/java/io/metersphere/api/service/ShareInfoService.java index 4347a8b2f7..1826ae004f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ShareInfoService.java +++ b/backend/src/main/java/io/metersphere/api/service/ShareInfoService.java @@ -10,11 +10,13 @@ import io.metersphere.base.mapper.ext.ExtShareInfoMapper; import io.metersphere.commons.constants.ShareType; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; +import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.track.service.TestPlanApiCaseService; import io.metersphere.track.service.TestPlanScenarioCaseService; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @@ -403,25 +405,34 @@ public class ShareInfoService { return new ShareInfo(); } + /** + * 生成分享连接 + * 如果该数据有连接则,返回已有的连接,不做有效期判断 + * @param request + * @return + */ public ShareInfo generateShareInfo(ShareInfo request) { - ShareInfo shareInfo = null; List shareInfos = extShareInfoMapper.selectByShareTypeAndShareApiIdWithBLOBs(request.getShareType(), request.getCustomData()); if (shareInfos.isEmpty()) { - long createTime = System.currentTimeMillis(); - shareInfo = new ShareInfo(); - shareInfo.setId(UUID.randomUUID().toString()); - shareInfo.setCustomData(request.getCustomData()); - shareInfo.setCreateUserId(SessionUtils.getUserId()); - shareInfo.setCreateTime(createTime); - shareInfo.setUpdateTime(createTime); - shareInfo.setShareType(request.getShareType()); - shareInfoMapper.insert(shareInfo); - return shareInfo; + return createShareInfo(request); } else { return shareInfos.get(0); } } + public ShareInfo createShareInfo(ShareInfo request) { + long createTime = System.currentTimeMillis(); + ShareInfo shareInfo = new ShareInfo(); + shareInfo.setId(UUID.randomUUID().toString()); + shareInfo.setCustomData(request.getCustomData()); + shareInfo.setCreateUserId(SessionUtils.getUserId()); + shareInfo.setCreateTime(createTime); + shareInfo.setUpdateTime(createTime); + shareInfo.setShareType(request.getShareType()); + shareInfoMapper.insert(shareInfo); + return shareInfo; + } + private List findByShareTypeAndShareApiIdWithBLOBs(String shareType, List shareApiIdList) { String shareApiIdString = this.genShareIdJsonString(shareApiIdList); return extShareInfoMapper.selectByShareTypeAndShareApiIdWithBLOBs(shareType, shareApiIdString); @@ -454,6 +465,8 @@ public class ShareInfoService { public void validate(String shareId, String customData) { ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(shareId); + ShareInfoService shareInfoService = CommonBeanFactory.getBean(ShareInfoService.class); + shareInfoService.validateExpired(shareInfo); if (shareInfo == null) { MSException.throwException("shareInfo not exist!"); } else { @@ -463,6 +476,28 @@ public class ShareInfoService { } } + public void validateExpired(String shareId) { + ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(shareId); + ShareInfoService shareInfoService = CommonBeanFactory.getBean(ShareInfoService.class); + shareInfoService.validateExpired(shareInfo); + } + + /** + * 不加入事务,抛出异常不回滚 + * 若在当前类中调用请使用如下方式调用,否则该方法的事务注解不生效 + * ShareInfoService shareInfoService = CommonBeanFactory.getBean(ShareInfoService.class); + * shareInfoService.validateExpired(shareInfo); + * @param shareInfo + */ + @Transactional(propagation = Propagation.NOT_SUPPORTED) + public void validateExpired(ShareInfo shareInfo) { + // 有效期24小时 + if (shareInfo == null || System.currentTimeMillis() - shareInfo.getUpdateTime() > 1000*60*60*24) { + shareInfoMapper.deleteByPrimaryKey(shareInfo.getId()); + MSException.throwException("连接已失效,请重新获取!"); + } + } + public void apiReportValidate(String shareId, String testId) { TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getById(testId); if (!StringUtils.equals(getPlanId(shareId), testPlanApiCase.getTestPlanId())) { diff --git a/backend/src/main/java/io/metersphere/commons/utils/ShiroUtils.java b/backend/src/main/java/io/metersphere/commons/utils/ShiroUtils.java index 82452159ee..6c7539101d 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/ShiroUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/ShiroUtils.java @@ -50,7 +50,7 @@ public class ShiroUtils { filterChainDefinitionMap.put("/anonymous/**", "anon"); //分享相关接口 - filterChainDefinitionMap.put("/share/info/generateShareInfo", "anon"); + filterChainDefinitionMap.put("/share/info/generateShareInfoWithExpired", "anon"); filterChainDefinitionMap.put("/share/info/selectApiInfoByParam", "anon"); filterChainDefinitionMap.put("/share/get/**", "anon"); filterChainDefinitionMap.put("/share/info", "apikey, csrf, authc"); // 需要认证 diff --git a/frontend/src/business/components/track/plan/view/comonents/report/detail/TestPlanReportButtons.vue b/frontend/src/business/components/track/plan/view/comonents/report/detail/TestPlanReportButtons.vue index b248002e81..4b4d1905d7 100644 --- a/frontend/src/business/components/track/plan/view/comonents/report/detail/TestPlanReportButtons.vue +++ b/frontend/src/business/components/track/plan/view/comonents/report/detail/TestPlanReportButtons.vue @@ -5,6 +5,7 @@ placement="right" width="300">

{{shareUrl}}

+ 24小时有效
{{ $t("commons.copy") }} @@ -37,7 +38,7 @@