當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot中使用Google 的Kaptcha工具实现验证码校验
生活随笔
收集整理的這篇文章主要介紹了
Springboot中使用Google 的Kaptcha工具实现验证码校验
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一般官網(wǎng)用戶登錄的時(shí)候需要輸入一個(gè)臨時(shí)驗(yàn)證碼,防止網(wǎng)站被腳本或者工具惡意刷頁面,這個(gè)驗(yàn)證碼可以通過谷歌開源的Kaptcha工具來實(shí)現(xiàn),步驟如下:
1、maven集成:
2、添加屬性配置類,這里可以通過設(shè)置背景顏色,字體大小,以及驗(yàn)證碼的數(shù)字范圍:
@Configuration public class KaptchaConfig {@Beanpublic Config config(){Properties properties = new Properties();properties.setProperty("kaptcha.border" , "no");properties.setProperty("kaptcha.textproducer.font.color" , "black");properties.setProperty("kaptcha.textproducer.char.space" , "4");properties.setProperty("kaptcha.textproducer.char.length" , "4");properties.setProperty("kaptcha.textproducer.char.string" , "123456789");Config config = new Config(properties);return config;}@Beanpublic Producer producer(Config config){DefaultKaptcha producer = new DefaultKaptcha();producer.setConfig(config);return producer;} }3、在controller里面寫個(gè)接口給前端調(diào)用,直接生成照片返回去給前端展示即可:
@Autowiredprivate Producer producer;@GetMapping("/captcha.jpg")public void captcha(HttpServletResponse response) throws ServletException, IOException {response.setHeader("Cache-Control", "no-store, no-cache");response.setContentType("image/jpeg");//生成文字驗(yàn)證碼String text = producer.createText();//生成圖片驗(yàn)證碼BufferedImage image = producer.createImage(text);//保存到shiro session // ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);ServletOutputStream out = response.getOutputStream();ImageIO.write(image, "jpg", out);}4、使用postman調(diào)用接口,既可以看到結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的Springboot中使用Google 的Kaptcha工具实现验证码校验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache的shiro获取当前Sess
- 下一篇: base64编码 springboot_