谷歌验证码工具 Kaptcha 使用
生活随笔
收集整理的這篇文章主要介紹了
谷歌验证码工具 Kaptcha 使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Kaptcha 簡介
Kaptcha 是一個可高度配置的實用驗證碼生成工具,可自由配置的選項如:
驗證碼的字體
驗證碼字體的大小
驗證碼字體的字體顏色
驗證碼內容的范圍(數字,字母,中文漢字!)
驗證碼圖片的大小,邊框,邊框粗細,邊框顏色
驗證碼的干擾線
驗證碼的樣式(魚眼樣式、3D、普通模糊、...)
Kaptcha 詳細配置表
| kaptcha.border | 圖片邊框,合法值:yes , no | yes |
| kaptcha.border.color | 邊框顏色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black |
| kaptcha.image.width | 圖片寬 | 200 |
| kaptcha.image.height | 圖片高 | 50 |
| kaptcha.producer.impl | 圖片實現類 | com.google.code.kaptcha.impl.DefaultKaptcha |
| kaptcha.textproducer.impl | 文本實現類 | com.google.code.kaptcha.text.impl.DefaultTextCreator |
| kaptcha.textproducer.char.string | 文本集合,驗證碼值從此集合中獲取 | abcde2345678gfynmnpwx |
| kaptcha.textproducer.char.length | 驗證碼長度 | 5 |
| kaptcha.textproducer.font.names | 字體 | Arial, Courier |
| kaptcha.textproducer.font.size | 字體大小 | 40px. |
| kaptcha.textproducer.font.color | 字體顏色,合法值: r,g,b 或者 white,black,blue. | black |
| kaptcha.textproducer.char.space | 文字間隔 | 2 |
| kaptcha.noise.impl | 干擾實現類 | com.google.code.kaptcha.impl.DefaultNoise |
| kaptcha.noise.color | 干擾 顏色,合法值: r,g,b 或者 white,black,blue. | black |
| kaptcha.obscurificator.impl |
圖片樣式:<br />水紋 com.google.code.kaptcha.impl.WaterRipple <br /> 魚眼 com.google.code.kaptcha.impl.FishEyeGimpy <br /> 陰影 com.google.code.kaptcha.impl.ShadowGimpy |
com.google.code.kaptcha.impl.WaterRipple |
| kaptcha.background.impl | 背景實現類 | com.google.code.kaptcha.impl.DefaultBackground |
| kaptcha.background.clear.from | 背景顏色漸變,開始顏色 | light grey |
| kaptcha.background.clear.to | 背景顏色漸變, 結束顏色 | white |
| kaptcha.word.impl | 文字渲染器 | com.google.code.kaptcha.text.impl.DefaultWordRenderer |
| kaptcha.session.key | session key | KAPTCHA_SESSION_KEY |
| kaptcha.session.date | session date | KAPTCHA_SESSION_DATE |
用法
在pom.xml中導入
<dependency>
<groupId>com.google.code.kaptcha</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>
或者
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
代碼實現:
KaptchaConfig.java
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDDefaultKaptcha() {
DefaultKaptcha dk = new DefaultKaptcha();
Properties properties = new Properties();
// 圖片邊框
properties.setProperty("kaptcha.border", "yes");
// 邊框顏色
properties.setProperty("kaptcha.border.color", "105,179,90");
// 字體顏色
properties.setProperty("kaptcha.textproducer.font.color", "red");
// 圖片寬
properties.setProperty("kaptcha.image.width", "110");
// 圖片高
properties.setProperty("kaptcha.image.height", "40");
// 字體大小
properties.setProperty("kaptcha.textproducer.font.size", "30");
// session key
properties.setProperty("kaptcha.session.key", "code");
// 驗證碼長度
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字體
properties.setProperty("kaptcha.textproducer.font.names", "宋體,楷體,微軟雅黑");
Config config = new Config(properties);
dk.setConfig(config);
return dk;
}
}
ValidateCodeController.java
@Controller
public class ValidateCodeController {
@Autowired
private KaptchaConfig kaptchaConfig;
/**
* 創建驗證碼
*
* @throws Exception
*/
@GetMapping("/validata/code/{client_id}")
public void createCode(@PathVariable String client_id, HttpServletResponse response) throws Exception {
DefaultKaptcha kaptcha = kaptchaConfig.getDDefaultKaptcha();
Assert.notNull(client_id, "機器碼不能為空");
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
//生成文字驗證碼
String text = kaptcha.createText();
//生成圖片驗證碼
BufferedImage image = kaptcha.createImage(text);
//validateCodeService.saveImageCode(client_id, text);//驗證碼保存方式根據個人想法實現
try (
ServletOutputStream out = response.getOutputStream()
) {
ImageIO.write(image, "JPEG", out);
}
}
}
MC❤濤
總結
以上是生活随笔為你收集整理的谷歌验证码工具 Kaptcha 使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 赛尔号超级英雄怎样升级快 赛尔号超级英雄
- 下一篇: 地下城431什么意思