Class CaptchaView

java.lang.Object
tech.riemann.nif.web.captcha.CaptchaView
All Implemented Interfaces:
org.springframework.web.servlet.View

public class CaptchaView extends Object implements 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 Details

  • Constructor Details

    • CaptchaView

      public CaptchaView(CodeCache cache)
      构造验证码视图

      使用默认配置:4位验证码,默认生成器

      Parameters:
      cache - 缓存实现
    • CaptchaView

      public CaptchaView(CaptchaGener captchaGener, int length)
      构造验证码视图

      使用自定义验证码生成器和长度

      Parameters:
      captchaGener - 验证码生成器
      length - 验证码长度
    • CaptchaView

      public CaptchaView(CaptchaGener captchaGener)
      构造验证码视图

      使用自定义验证码生成器,默认4位长度

      Parameters:
      captchaGener - 验证码生成器
    • CaptchaView

      public CaptchaView(CodeCache cache, boolean httpOnly, boolean secure)
      构造函数,可配置Cookie安全属性

      使用默认配置:4位验证码,默认生成器

      Parameters:
      cache - 缓存实现
      httpOnly - 是否启用HttpOnly标志
      secure - 是否启用Secure标志(需要HTTPS)
    • CaptchaView

      public CaptchaView(int length)
      构造验证码视图

      使用默认验证码生成器

      Parameters:
      length - 验证码长度
    • CaptchaView

      public CaptchaView(ImageVerification imageVerification)
      构造验证码视图

      使用自定义ImageVerification实例

      Parameters:
      imageVerification - 图片验证码实例
  • Method Details

    • setCache

      public void setCache(CodeCache cache)
      设置缓存实现
      Parameters:
      cache - 缓存实现
    • setHttpOnly

      public void setHttpOnly(boolean httpOnly)
      设置是否启用HttpOnly标志
      Parameters:
      httpOnly - 是否启用HttpOnly
    • setSecure

      public void setSecure(boolean secure)
      设置是否启用Secure标志
      Parameters:
      secure - 是否启用Secure(需要HTTPS环境)
    • setCookiePath

      public void setCookiePath(String cookiePath)
      设置Cookie的作用域路径
      Parameters:
      cookiePath - Cookie路径
    • getContentType

      public @NonNull String getContentType()
      获取内容类型
      Specified by:
      getContentType in interface org.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:
      render in interface org.springframework.web.servlet.View
      Parameters:
      model - 模型数据,未使用
      request - HTTP请求对象
      response - HTTP响应对象
      Throws:
      Exception - 当处理过程中发生异常时抛出
    • setImageVerification

      public void setImageVerification(ImageVerification imageVerification)
      设置图片验证码生成器实例

      供 starter 装配按配置构造(长度/干扰线等)后注入

      Parameters:
      imageVerification - 图片验证码实例