package com.trafficaudit.security.captcha; import com.trafficaudit.common.Result; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Collections; import java.util.List; import java.util.Map; @RestController @RequestMapping("/api/auth") public class CaptchaController { @Resource private CaptchaService captchaService; @GetMapping("/captcha") public Result> captcha() { return Result.ok(captchaService.generate()); } @PostMapping("/captcha/verify") public Result> verify(@RequestBody Map params) { String captchaId = (String) params.get("captchaId"); List clickIds = strList(params.get("captchaClickIds")); CaptchaService.VerifyResult r = captchaService.verify(captchaId, clickIds); switch (r) { case OK: return Result.ok(Collections.singletonMap("ok", true)); case WRONG: return Result.error(4001, "点击顺序错误,请重新点击"); case RETRY_LIMIT: return Result.error(4002, "错误次数过多,已更换题目"); default: return Result.error(4003, "验证码不存在或已过期,请刷新"); } } @SuppressWarnings("unchecked") private List strList(Object o) { if (o instanceof List) { return (List) o; } return Collections.emptyList(); } }