import com.example.common.CaptureConfig;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@CrossOrigin
@RestController
@RequestMapping
public class CaptureController {
@RequestMapping("/capture")
public void captcha(String key,HttpServletRequest request, HttpServletResponse response)throws Exception{
// 指定验证码的长宽以及字符的个数
SpecCaptcha captcha = new SpecCaptcha(135,33,5);
captcha.setCharType(Captcha.TYPE_NUM_AND_UPPER);
// 首先把验证码在后台保存一份,但是不能保存在session,可以存在redis,也可以存在后台的某个Map里
CaptureConfig.CAPTURE_MAP.put(key,captcha.text().toLowerCase());
// 验证码输出
CaptchaUtil.out(captcha,request,response);
}
}
// 算术类型
ArithmeticCaptcha captcha = new ArithmeticCaptcha(135,33);
// 几位数运算,默认两位
captcha.setLen(2);
// 获取运算公式:3+2=?
captcha.getArithmeticString();
// 获取运算结果: 5
captcha.text();
CaptureConfig.CAPTURE_MAP.put(key,captcha.text().toLowerCase());
CaptchaUtil.out(captcha,request,response);
3.登录流程
@AutoLog("登录系统")
@PostMapping("/login")
public Result login(@RequestBody Admin admin, @RequestParam String key, HttpServletRequest request){
//判断验证码是否正确
if (!admin.getVerCode().equals(CaptureConfig.CAPTURE_MAP.get(key))){
// 如果不相等,验证码清空
CaptchaUtil.clear(request);
return Result.error("验证码不正确");
}
Admin loginUser =userService.login(admin);
return Result.success(loginUser);
}