🔄Update: 删除加密逻辑

This commit is contained in:
2025-10-06 13:50:08 +08:00
parent 34d5bc6038
commit c7365e1338
8 changed files with 60 additions and 45 deletions
+6
View File
@@ -40,6 +40,12 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -0,0 +1,20 @@
package cn.hezhaohui.backend.controller;
import cn.hezhaohui.backend.entity.ChatRequest;
import cn.hezhaohui.backend.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin(origins = "*")
@RestController
@RequestMapping
public class ChatController {
@Autowired
private ChatService chatService;
@PostMapping("/api/chat")
public String chat(@RequestBody ChatRequest request) {
return chatService.chat(request);
}
}
@@ -0,0 +1,11 @@
package cn.hezhaohui.backend.entity;
import lombok.Data;
@Data
public class ChatRequest {
private String platformId;
private String apiKey;
private String endpoint;
private String message;
}
@@ -0,0 +1,13 @@
package cn.hezhaohui.backend.service;
import cn.hezhaohui.backend.entity.ChatRequest;
import org.springframework.stereotype.Service;
@Service
public class ChatService {
public String chat(ChatRequest request) {
System.out.println(request);
return " ";
}
}
-1
View File
@@ -12,7 +12,6 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"crypto-js": "^4.2.0",
"vue": "^3.5.22" "vue": "^3.5.22"
}, },
"devDependencies": { "devDependencies": {
+9 -3
View File
@@ -50,7 +50,6 @@
<script setup> <script setup>
import { ref, computed, onMounted } from 'vue' import { ref, computed, onMounted } from 'vue'
import { decrypt } from '../utils/encryption'
// 响应式数据 // 响应式数据
const userInput = ref('') const userInput = ref('')
@@ -143,7 +142,13 @@ const sendMessage = async () => {
const callAIPlatform = async (platformId, apiKey, endpoint, message) => { const callAIPlatform = async (platformId, apiKey, endpoint, message) => {
try { try {
// 发送请求到后端API // 发送请求到后端API
const response = await fetch('/api/chat', { // console.log(JSON.stringify({
// platformId,
// apiKey,
// endpoint,
// message
// }))
const response = await fetch('http://localhost:8080/api/chat', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@@ -154,7 +159,8 @@ const callAIPlatform = async (platformId, apiKey, endpoint, message) => {
endpoint, endpoint,
message message
}) })
}) }
)
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`) throw new Error(`HTTP error! status: ${response.status}`)
+1 -5
View File
@@ -61,7 +61,6 @@
<script setup> <script setup>
import { ref, computed, onMounted } from 'vue' import { ref, computed, onMounted } from 'vue'
import { encrypt } from '../utils/encryption'
// 厂商配置预设 // 厂商配置预设
const platforms = [ const platforms = [
@@ -151,13 +150,10 @@ const saveConfig = () => {
return return
} }
// 加密密钥
const encryptedKey = encrypt(apiKey.value)
// 构造配置对象 // 构造配置对象
const config = { const config = {
id: selectedPlatform.value, id: selectedPlatform.value,
apiKey: encryptedKey, apiKey: apiKey.value,
endpoint: endpoint.value endpoint: endpoint.value
} }
-36
View File
@@ -1,36 +0,0 @@
// AES-256 加密/解密工具
// 注意:这是一个简化版本,实际生产环境应使用更安全的加密库
import CryptoJS from 'crypto-js'
// 密钥(在实际应用中应使用更安全的方式管理)
const SECRET_KEY = 'ai-api-tool-secret-key-2025'
/**
* 加密函数
* @param {string} text - 要加密的文本
* @returns {string} - 加密后的文本
*/
export function encrypt(text) {
try {
return CryptoJS.AES.encrypt(text, SECRET_KEY).toString()
} catch (error) {
console.error('加密失败:', error)
throw new Error('加密失败')
}
}
/**
* 解密函数
* @param {string} encryptedText - 要解密的文本
* @returns {string} - 解密后的文本
*/
export function decrypt(encryptedText) {
try {
const bytes = CryptoJS.AES.decrypt(encryptedText, SECRET_KEY)
return bytes.toString(CryptoJS.enc.Utf8)
} catch (error) {
console.error('解密失败:', error)
throw new Error('解密失败')
}
}