Springboot?+redis+谷歌開源Kaptcha實現圖片驗證碼功能
更新時間:2022年01月29日 11:59:46 作者:look-word
這篇文章主要介紹了Springboot?+redis+?歌開源Kaptcha實現圖片驗證碼功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
背景
- 注冊-登錄-修改密碼一般需要發(fā)送驗證碼,但是容易被攻擊惡意調?
- 什么是短信-郵箱轟炸機
- 手機短信轟炸機是批、循環(huán)給手機無限發(fā)送各種網站的注冊驗證碼短信的方法。
- 公司帶來的損失
- 短信1條5分錢,如果被大盜刷大家自己計算 郵箱通知不用錢,但被大盜刷,帶寬、連接等都被占用,導致無法正常使用
- 如何避免自己的網站成為”肉雞“或者被刷呢
- 增加圖形驗證碼(開發(fā)人員)
- 單IP請求次數限制(開發(fā)人員)
- 限制號碼發(fā)送(一般短信提供商會做)
- 攻防永遠是有的,只過加大了攻擊者的成本,ROI劃不過來?然就放棄了
Kaptcha 框架介紹
谷歌開源的一個可高度配置的實用驗證碼生成工具
- 驗證碼的字體/大小/顏色
- 驗證碼內容的范圍(數字,字母,中文漢字!)
- 驗證碼圖?的大小,邊框,邊框粗細,邊框顏色
- 驗證碼的?擾線 驗證碼的樣式(魚眼樣式、3D、普通 模糊)
添加依賴
<!--kaptcha依賴包--> <dependency> <groupId>com.baomidou</groupId> <artifactId>kaptcha-spring-bootstarter</artifactId> <version>1.0.0</version> </dependency>
配置類
/**
* 圖像驗證碼的配置文件
* @author : look-word
* @date : 2022-01-28 17:10
**/
@Configuration
public class CaptchaConfig {
/**
* 驗證碼配置
* Kaptcha配置類名
*
* @return
*/
@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;
}
}實戰(zhàn)
我的配置類
獲取訪問ip和生成MD5的工具類
public class CommonUtil {
/**
* 獲取ip
* @param request
* @return
*/
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;
}接口開發(fā)
@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();
}
}
/**
* 生成redis驗證碼模塊的key
* @param request
* @return
*/
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 圖片驗證碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Elasticsearch term 查詢之精確值搜索功能實現
term查詢是Elasticsearch中用于精確值搜索的一種基本方式,通過了解 term 查詢的工作原理和使用方法,你可以更好地利用 Elasticsearch 進行結構化數據的搜索和分析,本文將詳細介紹 term 查詢的工作原理、使用場景以及如何在 Elasticsearch 中應用它,感興趣的朋友一起看看吧2024-06-06
Spring之底層架構核心概念Environment及用法詳解
這篇文章主要介紹了Spring之底層架構核心概念-Environment,本文結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12

