本地开发时,React 项目和 JeecgBoot 运行在不同端口:
http://localhost:3000http://localhost:3100不同端口 = 不同域名 = localStorage 不共享
通过 Nginx 将两个项目统一到一个端口。
macOS:
brew install nginx
Windows: 下载并安装:https://nginx.org/en/download.html
Linux:
sudo apt-get install nginx
使用项目根目录的 nginx.local.conf 文件:
# macOS/Linux
sudo cp nginx.local.conf /usr/local/etc/nginx/servers/local-dev.conf
# 或者直接编辑 nginx.conf
sudo nano /usr/local/etc/nginx/nginx.conf
# 1. 启动 React 项目(端口 3000)
npm run dev
# 2. 启动 JeecgBoot 前端(端口 3100)
cd /path/to/jeecg-boot-frontend
npm run serve
# 3. 启动 Nginx
sudo nginx
# 或重启
sudo nginx -s reload
现在所有服务都通过 http://localhost:8080 访问:
http://localhost:8080/spaces/http://localhost:8080/system/user✅ localStorage 自动共享!
sudo nginx -s stop
使用自定义域名 + Nginx。
sudo nano /etc/hosts
添加:
127.0.0.1 local-dev.com
server {
listen 80;
server_name local-dev.com;
location /spaces/ {
proxy_pass http://localhost:3000/spaces/;
}
location / {
proxy_pass http://localhost:3100/;
}
}
http://local-dev.com/spaces/http://local-dev.com/system/user仅用于快速测试,不推荐长期使用。
跳转时通过 URL 参数传递 token(已在 jeecgNavigation.ts 中实现):
// 开发环境自动添加 token 参数
navigateToJeecg('/system/user');
// 实际跳转:http://localhost:3100/system/user?_dev_token=xxx
需要在 JeecgBoot 中添加代码读取 URL 参数并写入 localStorage:
// JeecgBoot: src/main.js 或 App.vue
if (process.env.NODE_ENV === 'development') {
const urlParams = new URLSearchParams(window.location.search);
const devToken = urlParams.get('_dev_token');
if (devToken) {
try {
localStorage.setItem(
'内容中心管理系统__PRODUCTION__1.0.0__COMMON__LOCAL__KEY__',
devToken
);
console.log('[Dev] Token synced from URL parameter');
// 移除 URL 参数
window.history.replaceState({}, '', window.location.pathname);
} catch (e) {
console.error('[Dev] Failed to sync token:', e);
}
}
}
使用浏览器插件在不同端口间同步 localStorage。
缺点: 手动操作,不方便
| 方案 | 优点 | 缺点 | 推荐度 |
|---|---|---|---|
| Nginx 代理 | 完全模拟生产环境,自动共享 | 需要安装 Nginx | ⭐⭐⭐⭐⭐ |
| hosts + Nginx | 使用自定义域名,更真实 | 配置稍复杂 | ⭐⭐⭐⭐ |
| URL 参数 | 快速测试,无需额外配置 | 需要修改 JeecgBoot 代码 | ⭐⭐⭐ |
| 浏览器插件 | 无需代码修改 | 手动操作,不方便 | ⭐⭐ |
使用 Nginx 本地代理:
http://localhost:8080/spaces/ → React
http://localhost:8080/system/user → JeecgBoot
使用 自定义域名:
http://test.your-domain.com/spaces/ → React
http://test.your-domain.com/system/user → JeecgBoot
使用 同一域名:
https://your-domain.com/spaces/ → React
https://your-domain.com/system/user → JeecgBoot
# 1. 安装 Nginx
brew install nginx # macOS
# 或
sudo apt-get install nginx # Linux
# 2. 复制配置文件
sudo cp nginx.local.conf /usr/local/etc/nginx/servers/local-dev.conf
# 3. 启动 React(端口 3000)
npm run dev
# 4. 启动 JeecgBoot(端口 3100)
cd /path/to/jeecg-boot-frontend
npm run serve
# 5. 启动 Nginx
sudo nginx
# 6. 访问测试
open http://localhost:8080/spaces/
访问:http://localhost:8080/spaces/login
打开浏览器控制台:
localStorage.getItem('内容中心管理系统__PRODUCTION__1.0.0__COMMON__LOCAL__KEY__')
应该能看到 token 数据。
点击跳转按钮,或直接访问:
http://localhost:8080/system/user
如果 JeecgBoot 页面直接显示内容(没有跳转到登录页),说明 localStorage 共享成功!✅
# 检查配置文件语法
sudo nginx -t
# 查看错误日志
tail -f /usr/local/var/log/nginx/error.log
# 查看端口占用
lsof -i :8080
# 杀死进程
kill -9 <PID>
检查 Nginx 配置中的端口是否正确:
proxy_pass http://localhost:3000/; # React 端口
proxy_pass http://localhost:3100/; # JeecgBoot 端口
http://localhost:8080(不是 3000 或 3100)本地测试 localStorage 共享的关键:
只要满足这三点,localStorage 就会自动共享!