Class CaptchaView
java.lang.Object
tech.riemann.nif.web.captcha.CaptchaView
- All Implemented Interfaces:
org.springframework.web.servlet.View
验证码视图类
实现Spring的View接口,用于生成和渲染验证码图片
主要功能: 1. 生成验证码图片并写入HTTP响应流 2. 通过Cookie传递验证码的键值 3. 将验证码存储到缓存中供后续验证 4. 支持配置Cookie安全属性(HttpOnly、Secure)
使用流程: 1. 前端请求验证码接口 2. CaptchaView生成验证码图片并返回 3. 同时生成captcha_key Cookie 4. 验证码存储到缓存中,key为captcha_key的值 5. 用户提交表单时,携带captcha_key和验证码值 6. 后端通过CodeCache验证验证码是否正确
使用示例:
// 在Controller中返回CaptchaView
@GetMapping("/captcha")
public View captcha() {
return new CaptchaView(codeCache);
}
- Author:
- Kerbores(kerbores@riemann.tech)
-
Field Summary
FieldsFields inherited from interface org.springframework.web.servlet.View
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE -
Constructor Summary
ConstructorsConstructorDescriptionCaptchaView(int length) 构造验证码视图CaptchaView(CaptchaGener captchaGener) 构造验证码视图CaptchaView(CaptchaGener captchaGener, int length) 构造验证码视图CaptchaView(CodeCache cache) 构造验证码视图CaptchaView(CodeCache cache, boolean httpOnly, boolean secure) 构造函数,可配置Cookie安全属性CaptchaView(ImageVerification imageVerification) 构造验证码视图 -
Method Summary
Modifier and TypeMethodDescription@NonNull String获取内容类型voidrender(@Nullable Map<String, ?> model, @NonNull jakarta.servlet.http.HttpServletRequest request, @NonNull jakarta.servlet.http.HttpServletResponse response) 渲染验证码图片void设置缓存实现voidsetCookiePath(String cookiePath) 设置Cookie的作用域路径voidsetHttpOnly(boolean httpOnly) 设置是否启用HttpOnly标志voidsetImageVerification(ImageVerification imageVerification) 设置图片验证码生成器实例voidsetSecure(boolean secure) 设置是否启用Secure标志
-
Field Details
-
CAPTCHA_SESSION_KEY
-
-
Constructor Details
-
CaptchaView
-
CaptchaView
构造验证码视图使用自定义验证码生成器和长度
- Parameters:
captchaGener- 验证码生成器length- 验证码长度
-
CaptchaView
构造验证码视图使用自定义验证码生成器,默认4位长度
- Parameters:
captchaGener- 验证码生成器
-
CaptchaView
构造函数,可配置Cookie安全属性使用默认配置:4位验证码,默认生成器
- Parameters:
cache- 缓存实现httpOnly- 是否启用HttpOnly标志secure- 是否启用Secure标志(需要HTTPS)
-
CaptchaView
public CaptchaView(int length) 构造验证码视图使用默认验证码生成器
- Parameters:
length- 验证码长度
-
CaptchaView
构造验证码视图使用自定义ImageVerification实例
- Parameters:
imageVerification- 图片验证码实例
-
-
Method Details
-
setCache
-
setHttpOnly
public void setHttpOnly(boolean httpOnly) 设置是否启用HttpOnly标志- Parameters:
httpOnly- 是否启用HttpOnly
-
setSecure
public void setSecure(boolean secure) 设置是否启用Secure标志- Parameters:
secure- 是否启用Secure(需要HTTPS环境)
-
setCookiePath
-
getContentType
获取内容类型- Specified by:
getContentTypein interfaceorg.springframework.web.servlet.View- Returns:
- 内容类型字符串
-
render
public void render(@Nullable Map<String, ?> model, @NonNull jakarta.servlet.http.HttpServletRequest request, @NonNull jakarta.servlet.http.HttpServletResponse response) throws Exception渲染验证码图片实现View接口的方法,生成验证码图片并写入HTTP响应流
渲染流程: 1. 设置响应头,防止缓存 2. 生成验证码图片并写入输出流 3. 生成唯一键值并通过Cookie传递 4. 将验证码存储到缓存中
- Specified by:
renderin interfaceorg.springframework.web.servlet.View- Parameters:
model- 模型数据,未使用request- HTTP请求对象response- HTTP响应对象- Throws:
Exception- 当处理过程中发生异常时抛出
-
setImageVerification
设置图片验证码生成器实例供 starter 装配按配置构造(长度/干扰线等)后注入
- Parameters:
imageVerification- 图片验证码实例
-