Python 官方文档:入门教程 => 点击学习
背景 注册-登录-修改密码一般需要发送验证码,但是容易被攻击恶意调⽤什么是短信-邮箱轰炸机手机短信轰炸机是批、循环给手机无限发送各种网站的注册验证码短信的方法。公司带来的损失短信1条
添加依赖
<!--kaptcha依赖包-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>kaptcha-spring-bootstarter</artifactId>
<version>1.0.0</version>
</dependency>
配置类
@Configuration
public class CaptchaConfig {
@Bean
@Qualifier("captchaProducer")
public DefaultKaptcha kaptcha() {
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
//验证码个数
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
//字体间隔
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE,"8");
//⼲扰线颜⾊
//⼲扰实现类
properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.Google.code.kaptcha.impl.NoNoise");
//图⽚样式
properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL,
"com.google.code.kaptcha.impl.WaterRipple");
//⽂字来源
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789");
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
我的配置类
获取访问ip和生成MD5的工具类
public class CommonUtil {
public static String
getIpAddr(httpservletRequest request) {
String ipAddress = null;
try {
ipAddress = request.getHeader("xforwarded-for");
if (ipAddress == null ||
ipAddress.length() == 0 ||
"unknown".equalsIgnoreCase(ipAddress)) {
ipAddress =
request.getHeader("Proxy-Client-IP");
}
request.getHeader("WL-Proxy-Client-IP");
request.getRemoteAddr();
if
(ipAddress.equals("127.0.0.1")) {
// 根据⽹卡取本机配置的IP
InetAddress inet = null;
try {
inet =
InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress =
inet.getHostAddress();
}
// 对于通过多个代理的情况,第⼀个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null &&
ipAddress.length() > 15) {
// "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0)
{
ipAddress.substring(0, ipAddress.indexOf(","));
} catch (Exception e) {
ipAddress="";
}
return ipAddress;
}
public static String MD5(String data) {
java.security.MessageDigest md =
MessageDigest.getInstance("MD5");
byte[] array =
md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new
StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) |
0x100).substring(1, 3));
return sb.toString().toUpperCase();
} catch (Exception exception) {
return null;
}
接口开发
@RestController
@RequestMapping("/api/v1/captcha")
public class CaptchaController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private Producer producer;
@RequestMapping("get_captcha")
public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
String captchaText = producer.createText();
String key = getCaptchaKey(request);
// 十分钟过期
stringRedisTemplate.opsForValue().set(key,captchaText,10, TimeUnit.MINUTES);
BufferedImage image = producer.createImage(captchaText);
ServletOutputStream outputStream=null;
try {
outputStream= response.getOutputStream();
ImageIO.write(image,"jpg",outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getCaptchaKey(HttpServletRequest request){
String ipAddr = CommonUtil.getIpAddr(request);
// 请求头
String userAgent=request.getHeader("user-Agent");
String key="user_service:captcha:"+CommonUtil.MD5(ipAddr+userAgent);
return key;
}
配置文件
server:
port: 8080
spring:
redis:
host: redis锁在的ip
passWord: redis的密码
port: 端口号
lettuce:
pool:
# 连接池最⼤连接数(使⽤负值表示没有限制)
max-idle: 10
# 连接池中的最⼤空闲连接
max-active: 10
# 连接池中的最⼩空闲连接
min-idle: 0
# 连接池最⼤阻塞等待时间(使⽤负值表示没有限制)
max-wait: -1ms
结果
到此这篇关于SpringBoot +redis+⾕歌开源Kaptcha实现图片验证码功能的文章就介绍到这了,更多相关Springboot 图片验证码内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Springboot +redis+谷歌开源Kaptcha实现图片验证码功能
本文链接: https://www.lsjlt.com/news/138032.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0