mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 10:13:28 +08:00
update 优化短连接重定向
This commit is contained in:
parent
6acc6587c1
commit
ddc78eb548
@ -22,45 +22,57 @@ import java.time.Duration;
|
|||||||
public class ShortLinkUtils {
|
public class ShortLinkUtils {
|
||||||
|
|
||||||
private static final IdentifierGenerator IDENTIFIER_GENERATOR = SpringUtils.getBean(IdentifierGenerator.class);
|
private static final IdentifierGenerator IDENTIFIER_GENERATOR = SpringUtils.getBean(IdentifierGenerator.class);
|
||||||
private static final ShortLinkProperties shortLinkProperties = SpringUtils.getBean(ShortLinkProperties.class);
|
private static final ShortLinkProperties SHORT_LINK_PROPERTIES = SpringUtils.getBean(ShortLinkProperties.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成带有短链接主机名或域名的完整短链接
|
* 根据默认主机名(或域名)生成完整短链接
|
||||||
*
|
*
|
||||||
* @param longUrl 完整的长链接
|
* @param longUrl 完整的长链接
|
||||||
* @param validityType 有效期类型枚举
|
* @param validityType 有效期类型枚举
|
||||||
* @return 带有短链接主机名或域名的完整短链接
|
* @return 带有默认主机名(或域名)的完整短链接
|
||||||
*/
|
*/
|
||||||
public static String generateShortUrlHost(String longUrl, ValidityType validityType) {
|
public static String generateShortUrl(String longUrl, ValidityType validityType) {
|
||||||
String url = generateShortUrl(longUrl, validityType);
|
return generateShortUrl(SHORT_LINK_PROPERTIES.getHost(), longUrl, validityType);
|
||||||
return shortLinkProperties.getHost() + url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成短链接并将长链接存储到缓存中
|
* 根据指定主机名(或域名)生成完整短链接
|
||||||
|
*
|
||||||
|
* @param host 主机名(或域名)
|
||||||
|
* @param longUrl 完整的长链接
|
||||||
|
* @param validityType 有效期类型枚举
|
||||||
|
* @return 带有指定主机名(或域名)的完整短链接
|
||||||
|
*/
|
||||||
|
public static String generateShortUrl(String host, String longUrl, ValidityType validityType) {
|
||||||
|
String url = generateShortLinkIdentifier(longUrl, validityType);
|
||||||
|
return host + url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成短链接标识符并将长链接存储到缓存中
|
||||||
*
|
*
|
||||||
* @param longUrl 完整的长链接
|
* @param longUrl 完整的长链接
|
||||||
* @param validityType 有效期类型枚举
|
* @param validityType 有效期类型枚举
|
||||||
* @return 生成的短链接标识符
|
* @return 生成的短链接标识符
|
||||||
*/
|
*/
|
||||||
public static String generateShortUrl(String longUrl, ValidityType validityType) {
|
public static String generateShortLinkIdentifier(String longUrl, ValidityType validityType) {
|
||||||
// 使用标识符生成器生成短链接
|
// 使用标识符生成器生成短链接标识符
|
||||||
String shortUrl = IDENTIFIER_GENERATOR.nextId(null).toString();
|
String shortLinkIdentifier = IDENTIFIER_GENERATOR.nextId(null).toString();
|
||||||
return generateShortUrl(shortUrl, longUrl, validityType);
|
return generateShortLinkIdentifier(shortLinkIdentifier, longUrl, validityType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成指定短链接并将长链接存储到缓存中
|
* 生成指定短链接标识符并将长链接存储到缓存中
|
||||||
*
|
*
|
||||||
* @param shortUrl 指定的短链接标识符
|
* @param shortLinkIdentifier 指定的短链接标识符
|
||||||
* @param longUrl 完整的长链接
|
* @param longUrl 完整的长链接
|
||||||
* @param validityType 有效期类型枚举
|
* @param validityType 有效期类型枚举
|
||||||
* @return 生成的指定短链接标识符
|
* @return 生成的指定短链接标识符
|
||||||
*/
|
*/
|
||||||
public static String generateShortUrl(String shortUrl, String longUrl, ValidityType validityType) {
|
public static String generateShortLinkIdentifier(String shortLinkIdentifier, String longUrl, ValidityType validityType) {
|
||||||
Validator.validateUrl(longUrl, "请输入有效的url");
|
Validator.validateUrl(longUrl, "请输入有效的url");
|
||||||
RedisUtils.setCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl, longUrl, Duration.ofSeconds(validityType.getValidityType()));
|
RedisUtils.setCacheObject(GlobalConstants.SHORT_LINK_KEY + shortLinkIdentifier, longUrl, Duration.ofSeconds(validityType.getValidityType()));
|
||||||
return shortUrl;
|
return shortLinkIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -70,7 +82,7 @@ public class ShortLinkUtils {
|
|||||||
* @return 对应的完整长链接,如果未找到则返回错误链接
|
* @return 对应的完整长链接,如果未找到则返回错误链接
|
||||||
*/
|
*/
|
||||||
public static String getLongLink(String shortUrl) {
|
public static String getLongLink(String shortUrl) {
|
||||||
return getLongLink(shortUrl, shortLinkProperties.getErroHost());
|
return getLongLink(shortUrl, SHORT_LINK_PROPERTIES.getErroHost());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -43,6 +43,12 @@
|
|||||||
<artifactId>ruoyi-common-oss</artifactId>
|
<artifactId>ruoyi-common-oss</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 短链接功能模 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara</groupId>
|
||||||
|
<artifactId>ruoyi-common-shortlink</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.dromara</groupId>
|
<groupId>org.dromara</groupId>
|
||||||
<artifactId>ruoyi-common-log</artifactId>
|
<artifactId>ruoyi-common-log</artifactId>
|
||||||
|
|||||||
@ -1,12 +1,9 @@
|
|||||||
package org.dromara.system.controller.system;
|
package org.dromara.system.controller.system;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.dromara.common.core.constant.GlobalConstants;
|
import org.dromara.common.shortlink.enums.ValidityType;
|
||||||
import org.dromara.common.core.utils.SpringUtils;
|
import org.dromara.common.shortlink.utils.ShortLinkUtils;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
|
||||||
import org.dromara.common.redis.utils.RedisUtils;
|
|
||||||
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -14,8 +11,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.servlet.view.RedirectView;
|
import org.springframework.web.servlet.view.RedirectView;
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 短链接 控制层
|
* 短链接 控制层
|
||||||
*/
|
*/
|
||||||
@ -24,7 +19,6 @@ import java.time.Duration;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/resource/short")
|
@RequestMapping("/resource/short")
|
||||||
public class SysShortLinkController {
|
public class SysShortLinkController {
|
||||||
private final IdentifierGenerator identifierGenerator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据短链接获取长链接并重定向
|
* 根据短链接获取长链接并重定向
|
||||||
@ -34,15 +28,9 @@ public class SysShortLinkController {
|
|||||||
*/
|
*/
|
||||||
@GetMapping("/link")
|
@GetMapping("/link")
|
||||||
public RedirectView getLongLink(@NotBlank(message = "参数不能为空") String shortUrl) {
|
public RedirectView getLongLink(@NotBlank(message = "参数不能为空") String shortUrl) {
|
||||||
// 从 Redis 获取长链接
|
String longLink = ShortLinkUtils.getLongLink(shortUrl, "/error");
|
||||||
String longLink = RedisUtils.getCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl);
|
|
||||||
if (StringUtils.isNotEmpty(longLink)) {
|
|
||||||
// 如果长链接存在,则重定向到长链接
|
|
||||||
return new RedirectView(longLink);
|
return new RedirectView(longLink);
|
||||||
}
|
}
|
||||||
// 如果长链接不存在,则重定向到错误页面
|
|
||||||
return new RedirectView("/error");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成短链接并返回
|
* 生成短链接并返回
|
||||||
@ -52,14 +40,7 @@ public class SysShortLinkController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/shorturl")
|
@PostMapping("/shorturl")
|
||||||
public String generateShortUrl(@NotBlank(message = "参数不能为空") String longUrl) {
|
public String generateShortUrl(@NotBlank(message = "参数不能为空") String longUrl) {
|
||||||
// 使用标识符生成器生成短链接
|
return ShortLinkUtils.generateShortUrl(longUrl, ValidityType.THREE_DAYS);
|
||||||
String shortUrl = identifierGenerator.nextId(null).toString();
|
|
||||||
// 将短链接与长链接的映射关系存储到 Redis 中,并设置过期时间为 3 分钟
|
|
||||||
RedisUtils.setCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl, longUrl, Duration.ofMinutes(3));
|
|
||||||
// 获取短链接的域名部分
|
|
||||||
String host = SpringUtils.getProperty("short.host");
|
|
||||||
// 拼接完整的短链接并返回
|
|
||||||
return host + shortUrl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user