update 优化生成短链接标识符识别

This commit is contained in:
AprilWind 2024-04-12 14:44:08 +08:00
parent bf6102cf64
commit df6641a659
5 changed files with 16 additions and 21 deletions

View File

@ -195,7 +195,7 @@ sms:
sdk-app-id: 您的sdkAppId sdk-app-id: 您的sdkAppId
shortlink: shortlink:
enabled: true enabled: false
api: /dev-api api: /dev-api
address: http://localhost/dev-api/resource/short/link?shortUrl= address: http://localhost/dev-api/resource/short/link?shortUrl=
errorAddress: http://localhost/error errorAddress: http://localhost/error

View File

@ -126,7 +126,7 @@ security:
# EasyRetry Job 分派 @see com.aizuda.easy.retry.client.job.core.client.JobEndPoint # EasyRetry Job 分派 @see com.aizuda.easy.retry.client.job.core.client.JobEndPoint
- /job/dispatch/v1 - /job/dispatch/v1
- /job/stop/v1 - /job/stop/v1
- /resource/short/link - /resource/short/link/*
# 多租户配置 # 多租户配置
tenant: tenant:

View File

@ -30,6 +30,6 @@ public class ShortLinkProperties {
/** /**
* 错误页面的主机名或域名 * 错误页面的主机名或域名
*/ */
private String errorAddress="/error"; private String errorAddress = "/error";
} }

View File

@ -78,22 +78,22 @@ public class ShortLinkUtils {
/** /**
* 根据短链接获取对应的完整长链接如果找不到长链接则返回错误链接 * 根据短链接获取对应的完整长链接如果找不到长链接则返回错误链接
* *
* @param shortUrl 短链接标识符 * @param shortLinkIdentifier 短链接标识符
* @return 对应的完整长链接如果未找到则返回错误链接 * @return 对应的完整长链接如果未找到则返回错误链接
*/ */
public static String getLongLink(String shortUrl) { public static String getLongLink(String shortLinkIdentifier) {
return getLongLink(shortUrl, SHORT_LINK_PROPERTIES.getErrorAddress()); return getLongLink(shortLinkIdentifier, SHORT_LINK_PROPERTIES.getErrorAddress());
} }
/** /**
* 根据短链接获取对应的完整长链接如果找不到长链接则返回指定的错误链接 * 根据短链接获取对应的完整长链接如果找不到长链接则返回指定的错误链接
* *
* @param shortUrl 短链接标识符 * @param shortLinkIdentifier 短链接标识符
* @param errUrl 错误链接 * @param errUrl 错误链接
* @return 对应的完整长链接如果未找到则返回错误链接 * @return 对应的完整长链接如果未找到则返回错误链接
*/ */
public static String getLongLink(String shortUrl, String errUrl) { public static String getLongLink(String shortLinkIdentifier, String errUrl) {
String longLink = RedisUtils.getCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl); String longLink = RedisUtils.getCacheObject(GlobalConstants.SHORT_LINK_KEY + shortLinkIdentifier);
return StringUtils.isNotEmpty(longLink) ? longLink : errUrl; return StringUtils.isNotEmpty(longLink) ? longLink : errUrl;
} }

View File

@ -8,10 +8,7 @@ import org.dromara.common.shortlink.enums.ValidityType;
import org.dromara.common.shortlink.properties.ShortLinkProperties; import org.dromara.common.shortlink.properties.ShortLinkProperties;
import org.dromara.common.shortlink.utils.ShortLinkUtils; import org.dromara.common.shortlink.utils.ShortLinkUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView; import org.springframework.web.servlet.view.RedirectView;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -31,12 +28,12 @@ public class SysShortLinkController {
/** /**
* 根据短链接获取长链接并重定向 * 根据短链接获取长链接并重定向
* *
* @param shortUrl 短链接 * @param shortLinkIdentifier 短链接标识符
* @return 重定向视图 * @return 重定向视图
*/ */
@GetMapping("/link") @GetMapping("/link/{shortLinkIdentifier}")
public RedirectView getLongLink(@NotBlank(message = "参数不能为空") String shortUrl) { public RedirectView getLongLink(@PathVariable("shortLinkIdentifier") String shortLinkIdentifier) {
String longLink = ShortLinkUtils.getLongLink(shortUrl, shortLinkProperties.getErrorAddress()); String longLink = ShortLinkUtils.getLongLink(shortLinkIdentifier, shortLinkProperties.getErrorAddress());
return new RedirectView(longLink); return new RedirectView(longLink);
} }
@ -52,11 +49,9 @@ public class SysShortLinkController {
if (Boolean.FALSE.equals(shortLinkProperties.getEnabled())) { if (Boolean.FALSE.equals(shortLinkProperties.getEnabled())) {
shorturl = ShortLinkUtils.generateShortUrl(shortLinkProperties.getAddress(), longUrl, ValidityType.THREE_DAYS); shorturl = ShortLinkUtils.generateShortUrl(shortLinkProperties.getAddress(), longUrl, ValidityType.THREE_DAYS);
} else { } else {
String requestUrl = request.getRequestURL().toString(); String requestUrl = request.getRequestURL().toString().replace("shorturl", "link/");
String host = new URL(requestUrl).getHost(); String host = new URL(requestUrl).getHost();
String address = requestUrl String address = requestUrl.replace(host, host + shortLinkProperties.getApi());
.replace(host, host + shortLinkProperties.getApi())
.replace("shorturl", "link?shortUrl=");
shorturl = ShortLinkUtils.generateShortUrl(address, longUrl, ValidityType.THREE_DAYS); shorturl = ShortLinkUtils.generateShortUrl(address, longUrl, ValidityType.THREE_DAYS);
} }
return R.ok(shorturl); return R.ok(shorturl);