#Springboot集成kaptcha图形验证码增加系统安全性
最近服务器遭到了莫名其妙的攻击,打破了周末的宁静,不仅华为云直接停掉了服务器,短信商也提醒我们要做好短信验证码的防护,因此提出了增加图形验证码校验的建议。这些天也是为了这个事忙碌着。 目前基本实现完成了,下面总结一下技术经验。
引入依赖
在需要使用图形验证码的项目对应的pom.xml中添加依赖配置
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
配置类
KapachaConfig.java
在配置类中,对验证码的大小、样式风格等进行初始化设置。
package com.yeegee.mall.kaptcha;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class KapachaConfig {
@Bean
public Producer kaptchaProducer() {
Properties properties = new Properties();
// 设置验证码的尺寸
properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "140");
properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "40");
// 设置验证码文本的字体大小
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "28");
// 设置验证码的字符个数
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "5");
// 设置验证码的边框
properties.setProperty(Constants.KAPTCHA_BORDER, "no");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL,"com.google.code.kaptcha.impl.ShadowGimpy");
properties.setProperty(Constants.KAPTCHA_NOISE_IMPL,"com.google.code.kaptcha.impl.NoNoise");
Config config = new Config(properties);
DefaultKaptcha kaptcha = new DefaultKaptcha();
kaptcha.setConfig(config);
return kaptcha;
}
}
视图(View)响应
KaptchaController.java
在Controller层生成验证码图片,并通过response返回给前端显示
package com.yeegee.mall.controller;
import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
@RestController
@RequestMapping("/kaptcha")
public class KaptchaController {
@Autowired
private Producer kaptchaProducer;
@RequestMapping("/genCode")
public void genImageCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = kaptchaProducer.createText();
BufferedImage bi = kaptchaProducer.createImage(capText);
ImageIO.write(bi, "jpg", response.getOutputStream());
}
}
前端展示
前端使用的是UNIAPP技术,脚本语言用的是JAVASCRIPT
标记语言的写法
<view>
<image :src="imgCodeUrl" @click="doRefresh" style="width: 100%;height: 140rpx;" ></image>
</view>
其中调用了一个js方法,这个方法用于向后端请求并更新验证码图片
doRefresh(){
let config.baseUrl = "http://api.yeegee.com";
this.imgCodeUrl=config.baseUrl+"/kaptcha/genCode?telephone="+this.phone+"&t="+(new Date()).getTime();
}
结束语
以前人生很顺利,每天上着班,下班回家玩玩游戏,看看电视,一天嘻嘻哈哈就过去了,确实很开心,也并没什么不好。 只不过在真正检验自己技术的时候,或者是在面试的时候,才发现很多东西自己只是略知一二,甚至只是了解一点皮毛。
我也不想鼓励加班,包括面对突发状况,我也是胆战心惊。 但又能怎么办呢,既然接下来了这份工作,咱们就好好干着。 办法总比困难多。 技术不是解决问题的唯一出路。 所以接触一些以前不愿意做的事情,可能对你的技术能力、思维能力,综合处理事务的能力都有所帮助。
欢迎大家多多关注和支持! 我们一直会保持不定期更新,绝对不会放弃,让我们都遇到更好的自己!