Skip to content

Commit 91901fd

Browse files
committed
feat: 配置化短链域名 & 改善前端短链展示域名
1 parent ae9d060 commit 91901fd

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

src/main/java/com/layor/tinyflow/config/SecurityConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
5757
"/api/auth/**", // 认证接口(注册、登录)
5858
"/api/redirect/**", // 短链跳转(核心功能)
5959
"/actuator/**", // 监控端点
60-
"/error" // 错误页面
60+
"/error", // 错误页面
61+
"/{shortCode}" // 根路径短链重定向
6162
).permitAll()
62-
63+
6364
// 管理员接口
6465
.requestMatchers("/api/admin/**").hasRole("ADMIN")
6566

src/main/java/com/layor/tinyflow/service/ShortUrlService.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,10 @@ public class ShortUrlService {
6060
HashidsStrategy codeStrategy;
6161
@Autowired
6262
private AuthService authService; // 注入认证服务
63-
// 基础短链域名
64-
private static final String BASE_URL = "https://localhost:8080";
65-
66-
67-
68-
63+
64+
@Value("${app.domain:http://localhost:8080}")
65+
private String baseUrl;
66+
6967
public ShortUrlDTO createShortUrl(String longUrl, String customAlias) throws Exception {
7068
// 1. 校验长链接
7169
if (!isValidUrl(longUrl)) {
@@ -85,7 +83,7 @@ public ShortUrlDTO createShortUrl(String longUrl, String customAlias) throws Exc
8583
if (existingUrl.getUserId() != null && existingUrl.getUserId().equals(userId)) {
8684
return ShortUrlDTO.builder()
8785
.shortCode(existingUrl.getShortCode())
88-
.shortUrl(BASE_URL + "/" + existingUrl.getShortCode())
86+
.shortUrl(baseUrl + "/" + existingUrl.getShortCode())
8987
.longUrl(existingUrl.getLongUrl())
9088
.createdAt(existingUrl.getCreatedAt())
9189
.build();
@@ -118,7 +116,7 @@ public ShortUrlDTO createShortUrl(String longUrl, String customAlias) throws Exc
118116
// 4. 构造返回结果
119117
ShortUrlDTO dto = ShortUrlDTO.builder()
120118
.shortCode(shortCode)
121-
.shortUrl(BASE_URL + "/" + shortCode)
119+
.shortUrl(baseUrl + "/" + shortCode)
122120
.longUrl(longUrl)
123121
.createdAt(LocalDateTime.now())
124122
.build();

web/App.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ import QrcodeVue from 'qrcode.vue'
244244
import LanguageSwitcher from '/src/components/LanguageSwitcher.vue'
245245
import Favicon from '/src/components/Favicon.vue'
246246
247-
const API_BASE = 'http://localhost:8080' // 用于顶部“历史记录”跳转链接
247+
const API_BASE = (typeof window !== 'undefined' && window.location && window.location.origin)
248+
? window.location.origin
249+
: 'http://localhost:8080' // 用于顶部“历史记录”跳转链接
248250
const api = axios.create({ baseURL: '' }) // 使用 Vite dev 代理转发 /api 与 /shorten
249251
250252
export default {

web/node_modules/.vue-global-types/vue_3.5_0.d.ts

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)