feat: iframe 鉴权后端接口,前端使用 url 拼接token,将 token 放入 localstorage 进行鉴权
1个文件已修改
1个文件已添加
34 ■■■■■ 已修改文件
aiflowy-modules/aiflowy-module-system/src/main/java/tech/aiflowy/system/controller/SysGenerateTokenController.java 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
aiflowy-ui-react/src/pages/ExternalBot.tsx 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
aiflowy-modules/aiflowy-module-system/src/main/java/tech/aiflowy/system/controller/SysGenerateTokenController.java
New file
@@ -0,0 +1,27 @@
package tech.aiflowy.system.controller;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.aiflowy.common.ai.util.UUIDGenerator;
@RestController
@RequestMapping("/api/v1/token")
public class SysGenerateTokenController {
    // 手动生成 Token 并绑定账号
    @GetMapping("/generateToken")
    public SaResult generateToken() {
        long loginId = StpUtil.getLoginIdAsLong();             // 假设这是你要绑定的账号ID
        String customToken = UUIDGenerator.generateUUID();; // 自定义的 Token 字符串
        SaLoginModel saLoginModel = new SaLoginModel();
        saLoginModel.setToken(customToken);
        saLoginModel.setTimeout(-1);
        // 将 loginId 与 customToken 关联,并设置有效期(单位:秒)
        StpUtil.createLoginSession(loginId, saLoginModel); // 24小时有效期
        System.out.println("生成了token: " + customToken);
        return SaResult.ok("Token 已生成").setData(customToken);
    }
}
aiflowy-ui-react/src/pages/ExternalBot.tsx
@@ -92,6 +92,11 @@
});
export const ExternalBot: React.FC = () => {
    const urlParams = new URLSearchParams(location.search);
    const token = urlParams.get('authKey');
    if (token) {
        localStorage.setItem('authKey', token);
    }
    const [newTitle, setNewTitle] = useState<string>('');
    // ==================== Style ====================
@@ -343,4 +348,4 @@
    path: "/ai/externalBot/:id",
    element: ExternalBot,
    frontEnable: true,
};
};