feat(ai): 集成DashScope AI聊天功能并配置加密支持

- 添加DashScope AI配置项,包括API密钥和模型参数
- 实现ChatClient配置类用于构建AI聊天客户端
- 创建控制器接口支持用户通过HTTP请求与AI交互
- 引入Jasypt加密库用于敏感信息加密
- 添加Spring AI和Alibaba DashScope依赖管理- 升级项目依赖并引入WebFlux支持异步处理
This commit is contained in:
hanserwei
2025-10-22 11:58:27 +08:00
parent fff8f46dcf
commit 4207c85202
5 changed files with 103 additions and 2 deletions

37
pom.xml
View File

@@ -15,6 +15,7 @@
<description>snails-ai</description> <description>snails-ai</description>
<properties> <properties>
<java.version>21</java.version> <java.version>21</java.version>
<spring-ai.version>1.0.3</spring-ai.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
@@ -36,12 +37,46 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.mysql</groupId> <groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId> <artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<!-- spring-ai-alibaba-starter -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
<!--配置加密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies> </dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-bom</artifactId>
<version>1.0.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build> <build>
<plugins> <plugins>
@@ -72,4 +107,4 @@
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@@ -0,0 +1,19 @@
package com.hanserwei.snailsai.config;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ChatClientConfiguration {
@Resource
private DashScopeChatModel dashScopeChatModel;
@Bean
public ChatClient dashScopeChatClient() {
return ChatClient.builder(dashScopeChatModel).build();
}
}

View File

@@ -0,0 +1,24 @@
package com.hanserwei.snailsai.controller;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/dashscope")
@RestController
public class DashScopeController {
@Resource
private ChatClient dashScopeChatClient;
@GetMapping("/chat")
public String chat(@RequestParam("userPrompt") String userPrompt) {
return dashScopeChatClient.prompt()
.user(userPrompt)
.call()
.content();
}
}

View File

@@ -0,0 +1,11 @@
package com.hanserwei.snailsai.utils;
import org.jasypt.util.text.AES256TextEncryptor;
public class EncryptorUtil {
public static void main(String[] args) {
AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
textEncryptor.setPassword("yyyyyyyyyyyy");
System.out.println(textEncryptor.encrypt("xxxxxxxxxxxxxxxxxxxxxx"));
}
}

View File

@@ -23,8 +23,20 @@ spring:
minimum-idle: 20 # 保持 20 个空闲连接(与最大连接数一致) minimum-idle: 20 # 保持 20 个空闲连接(与最大连接数一致)
connection-timeout: 5000 # 获取连接超时 5 秒 connection-timeout: 5000 # 获取连接超时 5 秒
max-lifetime: 28800000 # 8 小时(确保在数据库连接超时前被回收) max-lifetime: 28800000 # 8 小时(确保在数据库连接超时前被回收)
ai:
dashscope:
api-key: ENC(cMgcKZkFllyE88DIbGwLKot9Vg02co+gsmY8L8o4/o3UjhcmqO4lJzFU35Sx0n+qFG8pDL0wBjoWrT8X6BuRw9vNlQhY1LgRWHaF9S1zzyM=)
chat:
options:
model: qwen-plus
temperature: 0.5
logging: logging:
level: level:
org.hibernate.SQL: debug org.hibernate.SQL: debug
# 隐藏掉 Hibernate 冗长的连接池 INFO 信息 # 隐藏掉 Hibernate 冗长的连接池 INFO 信息
org.hibernate.orm.connections.pooling: WARN org.hibernate.orm.connections.pooling: WARN
jasypt:
encryptor:
password: ${jasypt.encryptor.password}
algorithm: PBEWithHMACSHA512AndAES_256
iv-generator-classname: org.jasypt.iv.RandomIvGenerator