package com.anji.captcha.service.impl;import com.anji.captcha.model.common.RepCodeEnum;import com.anji.captcha.model.common.ResponseModel;import com.anji.captcha.model.vo.CaptchaVO;import com.anji.captcha.service.CaptchaService;import com.anji.captcha.util.StringUtils;import java.util.Iterator;import java.util.Properties;public class DefaultCaptchaServiceImpl extends AbstractCaptchaService { public DefaultCaptchaServiceImpl() { } public String captchaType() { return "default"; } public void init(Properties config) { Iterator var2 = CaptchaServiceFactory.instances.keySet().iterator(); while(var2.hasNext()) { String s = (String)var2.next(); if (!this.captchaType().equals(s)) { this.getService(s).init(config); } } } public void destroy(Properties config) { Iterator var2 = CaptchaServiceFactory.instances.keySet().iterator(); while(var2.hasNext()) { String s = (String)var2.next(); if (!this.captchaType().equals(s)) { this.getService(s).destroy(config); } } } private CaptchaService getService(String captchaType) { return (CaptchaService)CaptchaServiceFactory.instances.get(captchaType); } public ResponseModel get(CaptchaVO captchaVO) { if (captchaVO == null) { return RepCodeEnum.NULL_ERROR.parseError(new Object[]{"captchaVO"}); } else { return StringUtils.isEmpty(captchaVO.getCaptchaType()) ? RepCodeEnum.NULL_ERROR.parseError(new Object[]{"类型"}) : this.getService(captchaVO.getCaptchaType()).get(captchaVO); } } public ResponseModel check(CaptchaVO captchaVO) { if (captchaVO == null) { return RepCodeEnum.NULL_ERROR.parseError(new Object[]{"captchaVO"}); } else if (StringUtils.isEmpty(captchaVO.getCaptchaType())) { return RepCodeEnum.NULL_ERROR.parseError(new Object[]{"类型"}); } else { return StringUtils.isEmpty(captchaVO.getToken()) ? RepCodeEnum.NULL_ERROR.parseError(new Object[]{"token"}) : this.getService(captchaVO.getCaptchaType()).check(captchaVO); } } public ResponseModel verification(CaptchaVO captchaVO) { if (captchaVO == null) { return RepCodeEnum.NULL_ERROR.parseError(new Object[]{"captchaVO"}); } else if (StringUtils.isEmpty(captchaVO.getCaptchaVerification())) { return RepCodeEnum.NULL_ERROR.parseError(new Object[]{"二次校验参数"}); } else { try { String codeKey = String.format(REDIS_SECOND_CAPTCHA_KEY, captchaVO.getCaptchaVerification()); if (!CaptchaServiceFactory.getCache(cacheType).exists(codeKey)) { return ResponseModel.errorMsg(RepCodeEnum.API_CAPTCHA_INVALID); } CaptchaServiceFactory.getCache(cacheType).delete(codeKey); } catch (Exception var3) { this.logger.error("验证码坐标解析失落败", var3); return ResponseModel.errorMsg(var3.getMessage()); } return ResponseModel.success(); } }}
获取验证码
package com.anji.captcha.controller;import com.anji.captcha.model.common.ResponseModel;import com.anji.captcha.model.vo.CaptchaVO;import com.anji.captcha.service.CaptchaService;import com.anji.captcha.util.StringUtils;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;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;@RestController@RequestMapping({"/captcha"})public class CaptchaController { @Autowired private CaptchaService captchaService; public CaptchaController() { } @PostMapping({"/get"}) public ResponseModel get(@RequestBody CaptchaVO data, HttpServletRequest request) { assert request.getRemoteHost() != null; data.setBrowserInfo(getRemoteId(request)); return this.captchaService.get(data); } @PostMapping({"/check"}) public ResponseModel check(@RequestBody CaptchaVO data, HttpServletRequest request) { data.setBrowserInfo(getRemoteId(request)); return this.captchaService.check(data); } public ResponseModel verify(@RequestBody CaptchaVO data, HttpServletRequest request) { return this.captchaService.verification(data); } public static final String getRemoteId(HttpServletRequest request) { String xfwd = request.getHeader("X-Forwarded-For"); String ip = getRemoteIpFromXfwd(xfwd); String ua = request.getHeader("user-agent"); return StringUtils.isNotBlank(ip) ? ip + ua : request.getRemoteAddr() + ua; } private static String getRemoteIpFromXfwd(String xfwd) { if (StringUtils.isNotBlank(xfwd)) { String[] ipList = xfwd.split(","); return StringUtils.trim(ipList[0]); } else { return null; } }}
验证验证码
package com.et.captcha.controller;import com.anji.captcha.model.common.ResponseModel;import com.anji.captcha.model.vo.CaptchaVO;import com.anji.captcha.service.CaptchaService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/ 后端二次校验接口示例 /@RestController@RequestMapping("/auth")public class LoginController { @Autowired private CaptchaService captchaService; @PostMapping("/login") public ResponseModel get(@RequestParam("captchaVerification") String captchaVerification) { CaptchaVO captchaVO = new CaptchaVO(); captchaVO.setCaptchaVerification(captchaVerification); ResponseModel response = captchaService.verification(captchaVO); if(response.isSuccess() == false){ //验证码校验失落败,返复书息见告前端 //repCode 0000 无非常,代表成功 //repCode 9999 做事器内部非常 //repCode 0011 参数不能为空 //repCode 6110 验证码已失落效,请重新获取 //repCode 6111 验证失落败 //repCode 6112 获取验证码失落败,请联系管理员 } return response; }}
前端工程代码太多就不贴出来了,详细可以查看web-ui文件家里面的内容,以上只是一些关键代码,所有代码请拜会下面代码仓库
代码仓库https://github.com/Harries/springboot-demo3.测试第一步:启动后端,启动service/springboot的StartApplication。
第二步:启动前端,终端进入文件夹view/vue,

yarn installyarn run dev
浏览器登录 http://localhost:8081/#/
4.引用https://gitee.com/anji-plus/captchahttp://www.liuhaihua.cn/archives/710384.html