zjdtm/secure-prompt-spring-boot-starter
GitHub: zjdtm/secure-prompt-spring-boot-starter
一个轻量级 Spring Boot Starter,通过注解方式防护 LLM 应用的提示词注入攻击。
Stars: 0 | Forks: 0
# SecurePrompt
[](https://github.com/zjdtm/secure-prompt-spring-boot-starter/actions/workflows/ci.yml)
[](https://www.oracle.com/java/)
[](https://spring.io/projects/spring-boot)
[](LICENSE)
[](https://github.com/zjdtm/secure-prompt-spring-boot-starter/releases)
## 概述
在将 ChatGPT、Claude 等 LLM API 引入 Spring Boot 服务时,若直接将用户输入传递给 LLM,会面临 **Prompt Injection** 攻击的风险。
SecurePrompt 仅需一个 `@SecurePrompt` 注解即可自动验证所有 LLM 输入。
```
@PostMapping("/chat")
@SecurePrompt // ← 이 한 줄로 Prompt Injection 방어 완료
public String chat(@RequestBody ChatRequest request) {
return llmService.call(request.getPrompt());
}
```
## 主要功能
- **即刻生效** — 仅需添加 `@EnableSecurePrompt` 注解即可完成,无需额外代码
- **33 种内置模式** — 基于 OWASP LLM Top 10 的攻击模式可立即使用
- **Risk Score** — 自动计算检测到的模式危险度,分数范围为 0–100
- **无需数据库** — 默认为 In-Memory 运行,后续可连接数据库
- **灵活配置** — 通过 `application.yml` 调整阈值、拦截模式、自定义模式
- **高性能** — 使用 Caffeine 缓存复用编译后的 Regex,平均耗时 < 1ms
## 检测攻击类型
| 类别 | 模式数量 | 严重程度 | 示例 |
|---------|---------|--------|------|
| `INSTRUCTION_OVERRIDE` | 10 个 | CRITICAL/HIGH | `ignore previous instructions`, `jailbreak`, `bypass safety` |
| `ROLE_CONFUSION` | 8 个 | CRITICAL/HIGH | `[SYSTEM]`, `you are now a`, `you have no restrictions` |
| `CONTEXT_MANIPULATION` | 8 个 | HIGH | `---END USER---`, `reveal your system prompt` |
| `ENCODING_BYPASS` | 4 个 | MEDIUM/LOW | Base64 padding, Hex encoding, Unicode escape |
| `DELIMITER_INJECTION` | 5 个 | HIGH/MEDIUM | `