<>说明:(生成Gif、Png类型的图形验证码)

最近两次做项目,一些功能性的代码重复的写因此非常的麻烦。因此记录一下,以防下次。

<>1、功能性代码:图形绘制、验证码生成。

<>1、导入别人写好的工具父类、以及需要支持的工具类。(最主要的)

工具类已经被我导出成Jar的形式:工具类地址
<https://download.csdn.net/download/weixin_41660948/10919088>

* 父类 Captcha、两个子类 PngCaptcha、GifCaptcha;

<>2、编写一个公共的Controller请求、用于复用获取图形验证码。
/** * @param time * 回调多次请求的时候根据系统当前时间来进行不同的请求,防止同一时间重复进行多次请求。 *
注意:将值存入session中,以便获取验证码的值,通过覆盖进行值更新。 * 1.只要登录不成功即需要每次进行请求新的验证码。 *
2.需要设置响应头,告诉浏览器,并且不进行缓存。 * 3.session为true的时候当request不存在session,则创建一个;一般用于存、取数据。
* 4.session默认为true,session为false一般用于查询数据的时候,没有session不会创建新的; */ @RequestMapping(
value= "/getGifCode.html", method = RequestMethod.GET) public void getGifCode(
@RequestParam(required=false) String time,HttpServletResponse response,
HttpServletRequest res) throws IOException { HttpSession session = res.
getSession(true); // 设置响应信息 response.setHeader("Pragma", "No-cache"); response.
setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0);
response.setContentType("image/gif"); /* * 1.创建gif格式动画验证码 宽,高,位数。 *
2.向客户端响应输出gif动态验证信息。 * 3.此处可以通过实例化PngCaptcha()获取png图形验证码。 *
4.captcha两个方法,out()输出图片、text()获取文本。 */ Captcha captcha = new GifCaptcha(120, 40,
5); captcha.out(response.getOutputStream()); // 将验证码随机产生的随机验证码信息存入Session之中。
session.setAttribute(GlobalConstant.captcha, captcha.text()); System.out.println
("生成的验证码:"+captcha.text()); }}
<>3、前端获取方式
<div style="margin-left: 10px;"> <img src="getGifCode.html" > <!--
点击产生图片,编写JS参数为 time=Date.getTime() --> </div>
<>2、短信验证码的实现

依赖的类、依赖的jar包:
<!-- 发送短信依赖包 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <
artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>
com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.7</version>
</dependency>
额外的依赖类(导入官方下载的Demo即可):


实现接口: 使用第三方云之讯的SDK,官网地址 <https://www.ucpaas.com/>;

配置文件:
#测试使用 is_test=true rest_server=open.ucpaas.com
实现短信验证码的工具类: 具体看官方api
/** * @ClassName: SendSMSTool * @Description: 主要调用 云之讯 接口发送短信工具类 * @author
Unruly * @date 2018年12月28日 */ public class SendSMSTool { static AbsRestClient
InstantiationRestAPI() { return new JsonReqClient(); } public static String
SendMessage(String randNum, String mobile){ if(StringUtils.isBlank(mobile)) {
return "手机号:Is a blank"; } String sid = "****************************"; String
token= "****************************"; String appid =
"****************************"; String templateid ="415648"; // 发送的短信验证码模板
String result=InstantiationRestAPI().sendSms(sid, token, appid, templateid,
randNum, mobile, appid); JSONObject json = JSONObject.parseObject(result);
return json.getString("msg"); } }
控制层处理请求方式:
/** * 发送短信的方法 * @param num 手机号 * @param res * @return 发送的状态 * @throws
Exception */ @ResponseBody @RequestMapping(value = "/sendMessage") public Object
getSMS(@RequestParam(name = "phone", required = true) String phone) throws
Exception{ HashMap<String, Object> map = new HashMap<>(16); HttpSession session
= ServletTool.getSession(); map.put(GlobalConstant.code, 1001); map.put(
GlobalConstant.msg, "发送信息失败:请输入合法的手机号!"); if (!StringUtils.isBlank(phone) &&
GlobalRegTool.phone_reg(phone)) { /* 随机的生成的手机验证码 */ String phoneVercode = String
.valueOf(RandomUtils.nextInt(10000, 100000)); String sendMessage = SendSMSTool.
SendMessage(phoneVercode, phone); map.put(GlobalConstant.code,0); map.put(
GlobalConstant.msg, sendMessage); session.setAttribute(GlobalConstant.
session_phone, phone); // 将验证信息存入Session session.setAttribute(GlobalConstant.
phone_code, phoneVercode); } return map; }

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信