?kaptcha 介紹?
? ? ? ? ?kaptcha是一個(gè)優(yōu)秀的驗(yàn)證碼框架?,在不多的項(xiàng)目中使用了很多年很穩(wěn)定,功能強(qiáng)大配置項(xiàng)很豐富,可以根據(jù)不同的需求配置不同的效果。
?以下最常用的配置? 字母+數(shù)字 驗(yàn)證碼實(shí)現(xiàn)的效果圖:?
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-849086.html
一? 導(dǎo)入kaptcha包:
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
<scope>compile</scope>
</dependency>
二? 在springboot使用,配置驗(yàn)證碼bean放在框架容器管理
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Bean
public DefaultKaptcha getKaptchaBean() {
// 創(chuàng)建 DefaultKaptcha 實(shí)例
DefaultKaptcha bean = new DefaultKaptcha();
// 創(chuàng)建 Properties 實(shí)例,用于配置 Kaptcha 屬性
Properties properties = new Properties();
// 設(shè)置驗(yàn)證碼邊框?yàn)闊o(wú)邊框
properties.setProperty("kaptcha.border", "no");
// 設(shè)置驗(yàn)證碼圖片的寬度為 95 像素
properties.setProperty("kaptcha.image.width", "95");
// 設(shè)置驗(yàn)證碼圖片的高度為 45 像素
properties.setProperty("kaptcha.image.height", "45");
// 設(shè)置驗(yàn)證碼背景顏色為淺灰色
properties.setProperty("kaptcha.background.clear.from", "248,248,248");
// 設(shè)置驗(yàn)證碼字符范圍為數(shù)字和小寫字母(排除容易混淆的字符)
properties.setProperty("kaptcha.textproducer.char.string", "23456789abcdefhijkmnpqrstuvwxyz");
// 設(shè)置驗(yàn)證碼文字顏色為藍(lán)色
properties.setProperty("kaptcha.textproducer.font.color", "0,0,255");
// 設(shè)置驗(yàn)證碼干擾實(shí)現(xiàn)類為水波紋效果
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
// 設(shè)置驗(yàn)證碼文字字體大小為 35 像素
properties.setProperty("kaptcha.textproducer.font.size", "35");
// 設(shè)置驗(yàn)證碼字符長(zhǎng)度為 4
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 設(shè)置驗(yàn)證碼字符間隔為 2 像素
properties.setProperty("kaptcha.textproducer.char.space", "2");
// 設(shè)置驗(yàn)證碼噪點(diǎn)生成實(shí)現(xiàn)類為無(wú)噪點(diǎn)
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
// 使用 Config 類將 Properties 配置應(yīng)用到 Kaptcha 實(shí)例中
com.google.code.kaptcha.util.Config config = new com.google.code.kaptcha.util.Config(properties);
bean.setConfig(config);
// 返回配置后的 Kaptcha 實(shí)例
return bean;
}
}
如果不是springboot項(xiàng)目,xml配置項(xiàng):文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-849086.html
<!-- 圖片邊框 -->
<prop key="kaptcha.border">no</prop>
<!-- 圖片寬度 -->
<prop key="kaptcha.image.width">95</prop>
<!-- 圖片高度 -->
<prop key="kaptcha.image.height">45</prop>
<!-- 驗(yàn)證碼背景顏色漸變,開(kāi)始顏色 -->
<prop key="kaptcha.background.clear.from">248,248,248</prop>
<!-- 驗(yàn)證碼背景顏色漸變,結(jié)束顏色 -->
<prop key="kaptcha.background.clear.to">248,248,248</prop>
<!-- 驗(yàn)證碼的字符 -->
<prop key="kaptcha.textproducer.char.string">23456789abcdefhijkmnpqrstuvwxyz</prop>
<!-- 驗(yàn)證碼字體顏色 -->
<prop key="kaptcha.textproducer.font.color">0,0,255</prop>
<!-- 驗(yàn)證碼的效果,水紋 -->
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop>
<!-- 驗(yàn)證碼字體大小 -->
<prop key="kaptcha.textproducer.font.size">35</prop>
<!-- 驗(yàn)證碼字?jǐn)?shù) -->
<prop key="kaptcha.textproducer.char.length">4</prop>
<!-- 驗(yàn)證碼文字間距 -->
<prop key="kaptcha.textproducer.char.space">2</prop>
<!-- 驗(yàn)證碼字體 -->
<prop key="kaptcha.textproducer.font.names">new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
</prop>
<!-- 不加噪聲 -->
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
?三 使用驗(yàn)證碼
import com.google.code.kaptcha.Producer;
import com.jxwifi.kyc.common.tools.cache.CaptcheCacheServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
/**
* 圖形驗(yàn)證碼控制器
*/
@RequestMapping("/public/")
@RestController
public class CaptchaController {
@Autowired
private CaptcheCacheServiceImpl cacheService;
@Autowired
private Producer captchaProducer;
/**
* 獲取圖形驗(yàn)證碼
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "/getCaptcha", method = RequestMethod.GET)
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
String uuid = UUID.randomUUID().toString();
String captcha = captchaProducer.createText();
cacheService.put("captchaKey_"+uuid, captcha);
// 將驗(yàn)證碼放置到Cookie中
Cookie cookie = new Cookie("captcha-key", uuid);
cookie.setPath("/");
cookie.setMaxAge(60*1000);
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin",
response.addCookie(cookie);
BufferedImage challenge = captchaProducer.createImage(captcha);
ImageIO.write(challenge, "jpg", jpegOutputStream);
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = null;
responseOutputStream = response.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
到了這里,關(guān)于一個(gè)很好用且開(kāi)源的java驗(yàn)證碼框架kaptcha的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!