captcha

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

验证码模块

本模块提供滑块、点选、旋转验证码的生成与校验能力,支持内存与 Redis 存储,实现由外部注入。

功能说明

  • 生成验证码:支持 click、slide、rotate
  • 校验验证码:按类型校验并自动清理已用记录
  • 存储实现:MemoryStore / RedisStore
  • captcha.yaml 配置文件会自动生成到 conf 目录

路由

  • POST /api/v1/captcha/generate
  • POST /api/v1/captcha/verify

配置示例

captcha.yaml:

enabled: true
ttl: 300
type: "click"
mode: "text"
padding: 5
store: "memory" # memory redis
cleanup-interval: 60

使用示例

生成验证码:

POST /api/v1/captcha/generate
{
  "type": "click"
}

校验验证码:

POST /api/v1/captcha/verify
{
  "type": "click",
  "captchaId": "xxxx",
  "points": [{"index":1,"x":10,"y":20}]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CaptchaServiceProvider

type CaptchaServiceProvider struct {
	// contains filtered or unexported fields
}

func (*CaptchaServiceProvider) Boot

Boot 启动时挂载验证码路由

func (*CaptchaServiceProvider) Conf

func (i *CaptchaServiceProvider) Conf() map[string]string

func (*CaptchaServiceProvider) Description

func (i *CaptchaServiceProvider) Description() string

func (*CaptchaServiceProvider) Register

func (i *CaptchaServiceProvider) Register(ctx context.Context) error

Register 注册验证码服务到容器

func (*CaptchaServiceProvider) SetApplication

func (i *CaptchaServiceProvider) SetApplication(app foundation.Application)

type ClickCaptchaResp

type ClickCaptchaResp struct {
	CaptchaId   string `json:"captchaId"`   // 验证码ID
	MasterImage string `json:"masterImage"` // 主图Base64
	ThumbImage  string `json:"thumbImage"`  // 缩略图Base64
}

type ClickPoint

type ClickPoint struct {
	Index int `json:"index"` // 点序号
	X     int `json:"x"`     // X坐标
	Y     int `json:"y"`     // Y坐标
}

type GenerateReq

type GenerateReq struct {
	Type string `json:"type"` // 验证码类型
}

type Options

type Options struct {
	Enabled         bool   `json:"enabled"`          // 是否启用
	TTL             int    `json:"ttl"`              // 过期时间(秒)
	Type            string `json:"type"`             // 默认验证码类型
	Mode            string `json:"mode"`             // 默认生成模式
	Padding         int    `json:"padding"`          // 校验容差
	Store           string `json:"store"`            // 存储驱动(memory|redis)
	CleanupInterval int    `json:"cleanup-interval"` // 清理间隔(秒)
}

type RotateCaptchaResp

type RotateCaptchaResp struct {
	CaptchaId   string `json:"captchaId"`   // 验证码ID
	MasterImage string `json:"masterImage"` // 主图Base64
	ThumbImage  string `json:"thumbImage"`  // 缩略图Base64
}

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(opt Options, store cache_captcha.CaptchaStore) (*Service, error)

NewService 创建验证码服务实例

func (*Service) Generate

func (s *Service) Generate(ctx context.Context, typ string) (interface{}, error)

Generate 生成指定类型的验证码

func (*Service) GenerateClick

func (s *Service) GenerateClick(ctx context.Context, mode string) (*ClickCaptchaResp, error)

GenerateClick 生成点选验证码

func (*Service) GenerateRotate

func (s *Service) GenerateRotate(ctx context.Context) (*RotateCaptchaResp, error)

GenerateRotate 生成旋转验证码

func (*Service) GenerateSlide

func (s *Service) GenerateSlide(ctx context.Context, mode string) (*SlideCaptchaResp, error)

GenerateSlide 生成滑块验证码

func (*Service) RegisterRoutes

func (s *Service) RegisterRoutes(engine *gin.Engine)

RegisterRoutes 注册验证码路由

func (*Service) Verify

func (s *Service) Verify(ctx context.Context, req *VerifyReq) (bool, error)

Verify 校验验证码

func (*Service) VerifyClick

func (s *Service) VerifyClick(ctx context.Context, req *VerifyClickReq) (bool, error)

VerifyClick 校验点选验证码

func (*Service) VerifyRotate

func (s *Service) VerifyRotate(ctx context.Context, req *VerifyRotateReq) (bool, error)

VerifyRotate 校验旋转验证码

func (*Service) VerifySlide

func (s *Service) VerifySlide(ctx context.Context, req *VerifySlideReq) (bool, error)

VerifySlide 校验滑块验证码

type SlideCaptchaResp

type SlideCaptchaResp struct {
	CaptchaId   string `json:"captchaId"`   // 验证码ID
	MasterImage string `json:"masterImage"` // 主图Base64
	TileImage   string `json:"tileImage"`   // 滑块图Base64
	TileX       int    `json:"tileX"`       // 滑块初始 X(左侧起点)
	TileY       int    `json:"tileY"`       // 滑块 Y / 缺口 Y
	TileWidth   int    `json:"tileWidth"`   // 滑块宽度
	TileHeight  int    `json:"tileHeight"`  // 滑块高度
}

type VerifyClickReq

type VerifyClickReq struct {
	CaptchaId string       `json:"captchaId"` // 验证码ID
	Points    []ClickPoint `json:"points"`    // 点选坐标
}

type VerifyReq

type VerifyReq struct {
	Type      string       `json:"type"`      // 验证码类型
	CaptchaId string       `json:"captchaId"` // 验证码ID
	Points    []ClickPoint `json:"points"`    // 点选坐标
	X         int          `json:"x"`         // 滑块X
	Y         int          `json:"y"`         // 滑块Y
	Angle     int          `json:"angle"`     // 旋转角度
}

type VerifyRotateReq

type VerifyRotateReq struct {
	CaptchaId string `json:"captchaId"` // 验证码ID
	Angle     int    `json:"angle"`     // 旋转角度
}

type VerifySlideReq

type VerifySlideReq struct {
	CaptchaId string `json:"captchaId"` // 验证码ID
	X         int    `json:"x"`         // 滑块X
	Y         int    `json:"y"`         // 滑块Y
}

Source Files

  • api.go
  • captcha_service_provider.go
  • handle.go
  • service.go

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL