bufferedimage生成的图片模糊_Kaptcha图片验证码工具
閱讀文本大概需要3分鐘。
驗(yàn)證碼的作用
圖片驗(yàn)證碼自從誕生以來從未被拋棄,依然發(fā)出屬于它所應(yīng)有的光。驗(yàn)證碼經(jīng)常驗(yàn)證如下一些場景。
1、用戶登錄,防止機(jī)器人登錄
2、論壇留言,防止惡意灌水
3、短信驗(yàn)證碼發(fā)送,防止盜刷短信
Kaptcha 簡介
Kaptcha 是一個(gè)可高度配置的實(shí)用驗(yàn)證碼生成工具,可自由配置的選項(xiàng)如:
驗(yàn)證碼的字體
驗(yàn)證碼字體的大小
驗(yàn)證碼字體的字體顏色
驗(yàn)證碼內(nèi)容的范圍(數(shù)字,字母,中文漢字!)
驗(yàn)證碼圖片的大小,邊框,邊框粗細(xì),邊框顏色
驗(yàn)證碼的干擾線
驗(yàn)證碼的樣式(魚眼樣式、3D、普通模糊)
Kaptcha詳細(xì)配置表
配置項(xiàng):kaptcha.border描述:圖片邊框,合法值:yes?,?no
默認(rèn)值:yes
配置項(xiàng):kaptcha.border.color
描述:邊框顏色,合法值:?r,g,b?(and?optional?alpha)?或者?white,black,blue.
默認(rèn)值:black
配置項(xiàng):kaptcha.image.width
描述:圖片寬
默認(rèn)值:200
配置項(xiàng):kaptcha.image.height
描述:圖片高
默認(rèn)值:50
配置項(xiàng):kaptcha.producer.impl
描述:圖片實(shí)現(xiàn)類
默認(rèn)值:com.google.code.kaptcha.impl.DefaultKaptcha
配置項(xiàng):kaptcha.textproducer.impl
描述:文本實(shí)現(xiàn)類
默認(rèn)值:com.google.code.kaptcha.text.impl.DefaultTextCreator
配置項(xiàng):kaptcha.textproducer.char.string
描述:文本集合,驗(yàn)證碼值從此集合中獲取
默認(rèn)值:abcde2345678gfynmnpwx
配置項(xiàng):kaptcha.textproducer.char.length
描述:驗(yàn)證碼長度
默認(rèn)值:5
配置項(xiàng):kaptcha.textproducer.font.names
描述:字體
默認(rèn)值:Arial,?Courier
配置項(xiàng):kaptcha.textproducer.font.size
描述:字體大小
默認(rèn)值:40px.
配置項(xiàng):kaptcha.textproducer.font.color
描述:字體顏色,合法值:?r,g,b??或者?white,black,blue.
默認(rèn)值:black
配置項(xiàng):kaptcha.textproducer.char.space
描述:文字間隔
默認(rèn)值:2
配置項(xiàng):kaptcha.noise.impl
描述:干擾實(shí)現(xiàn)類
默認(rèn)值:com.google.code.kaptcha.impl.DefaultNoise
配置項(xiàng):kaptcha.noise.color
描述:干擾?顏色,合法值:?r,g,b?或者?white,black,blue.
默認(rèn)值:black
配置項(xiàng):kaptcha.obscurificator.impl
描述:圖片樣式,
???水紋?com.google.code.kaptcha.impl.WaterRipple?
???魚眼?com.google.code.kaptcha.impl.FishEyeGimpy?
???陰影?com.google.code.kaptcha.impl.ShadowGimpy
默認(rèn)值:com.google.code.kaptcha.impl.WaterRipple
配置項(xiàng):kaptcha.background.impl
描述:背景實(shí)現(xiàn)類
默認(rèn)值:com.google.code.kaptcha.impl.DefaultBackground
配置項(xiàng):kaptcha.background.clear.from
描述:背景顏色漸變,開始顏色
默認(rèn)值:light?grey
配置項(xiàng):kaptcha.background.clear.to
描述:背景顏色漸變,?結(jié)束顏色
默認(rèn)值:white
配置項(xiàng):kaptcha.word.impl
描述:文字渲染器
默認(rèn)值:com.google.code.kaptcha.text.impl.DefaultWordRenderer
配置項(xiàng):kaptcha.session.key
描述:session?key
默認(rèn)值:KAPTCHA_SESSION_KEY
配置項(xiàng):kaptcha.session.date
描述:session?date
默認(rèn)值:KAPTCHA_SESSION_DATE
SpringBoot整合 Kaptcha
1、pom.xml文件中引入
<dependency>
????<groupId>com.oopsguy.kaptchagroupId>
????<artifactId>kaptcha-spring-boot-autoconfigureartifactId>
????<version>1.0.0-beta-2version>
dependency>
2、配置DefaultKaptcha
package?com.piano;import?java.util.Properties;
import?org.springframework.context.annotation.Bean;
import?org.springframework.context.annotation.Configuration;
import?com.google.code.kaptcha.impl.DefaultKaptcha;
import?com.google.code.kaptcha.util.Config;
@Configuration
public?class?ConfigBean?{
????@Bean
????public?DefaultKaptcha?getDefaultKaptcha(){
????????DefaultKaptcha?dk?=?new?DefaultKaptcha();
????????Properties?properties?=?new?Properties();
????????properties.put("kaptcha.border",?"yes");
????????properties.put("kaptcha.border.color","105,179,90");
????????properties.put("kaptcha.textproducer.font.color","blue");
????????properties.put("kaptcha.image.width","125");
????????properties.put("kaptcha.image.height","45");
????????properties.put("kaptcha.textproducer.font.size","45");
????????properties.put("kaptcha.session.key","code");
????????properties.put("kaptcha.textproducer.char.length","4");
????????properties.put("kaptcha.textproducer.font.names","宋體,楷體,微軟雅黑");
????????Config?config?=?new?Config(properties?);
????????dk.setConfig(config);
????????return?dk;
????}
}
3、編寫controller
package?com.piano.student.controller;import?java.awt.image.BufferedImage;
import?java.io.IOException;
import?javax.imageio.ImageIO;
import?javax.servlet.ServletOutputStream;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
import?org.springframework.beans.factory.annotation.Autowired;
import?org.springframework.stereotype.Controller;
import?org.springframework.web.bind.annotation.RequestMapping;
import?org.springframework.web.bind.annotation.RequestMethod;
import?org.springframework.web.servlet.ModelAndView;
import?com.google.code.kaptcha.Constants;
import?com.google.code.kaptcha.impl.DefaultKaptcha;
@Controller
public?class?KaptchaController?{
????@Autowired
????private?DefaultKaptcha?captchaProducer;
????@RequestMapping(value?=?"verification",?method?=?RequestMethod.GET)
????public?ModelAndView?verification(HttpServletRequest?request,?HttpServletResponse?response)?throws?IOException?{
????????response.setDateHeader("Expires",?0);
????????//?Set?standard?HTTP/1.1?no-cache?headers.
????????response.setHeader("Cache-Control",?"no-store,?no-cache,?must-revalidate");
????????//?Set?IE?extended?HTTP/1.1?no-cache?headers?(use?addHeader).
????????response.addHeader("Cache-Control",?"post-check=0,?pre-check=0");
????????//?Set?standard?HTTP/1.0?no-cache?header.
????????response.setHeader("Pragma",?"no-cache");
????????//?return?a?jpeg
????????response.setContentType("image/jpeg");
????????//?create?the?text?for?the?image
????????String?capText?=?captchaProducer.createText();
????????//?store?the?text?in?the?session
????????request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,?capText);
????????//?create?the?image?with?the?text
????????BufferedImage?bi?=?captchaProducer.createImage(capText);
????????ServletOutputStream?out?=?response.getOutputStream();
????????//?write?the?data?out
????????ImageIO.write(bi,?"jpg",?out);
????????try?{
????????????out.flush();
????????}?finally?{
????????????out.close();
????????}
????????return?null;
????}
}
4、訪問http://127.0.0.1:8083/verification
☆
往期精彩
☆
01?漫談發(fā)版哪些事,好課程推薦
02?Linux的常用最危險(xiǎn)的命令
03?精講Spring Boot—入門+進(jìn)階+實(shí)例
04?優(yōu)秀的Java程序員必須了解的GC哪些
05?互聯(lián)網(wǎng)支付系統(tǒng)整體架構(gòu)詳解
關(guān)注我
每天進(jìn)步一點(diǎn)點(diǎn)
很干!在看嗎??總結(jié)
以上是生活随笔為你收集整理的bufferedimage生成的图片模糊_Kaptcha图片验证码工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何把自己的经历写成小说_古天乐的经历教
- 下一篇: 微信小程序点击按钮弹出弹窗_微信小程序实