當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot中使用Google 的Kaptcha工具实现验证码校验
生活随笔
收集整理的這篇文章主要介紹了
Springboot中使用Google 的Kaptcha工具实现验证码校验
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一般官網用戶登錄的時候需要輸入一個臨時驗證碼,防止網站被腳本或者工具惡意刷頁面,這個驗證碼可以通過谷歌開源的Kaptcha工具來實現,步驟如下:
1、maven集成:
2、添加屬性配置類,這里可以通過設置背景顏色,字體大小,以及驗證碼的數字范圍:
@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里面寫個接口給前端調用,直接生成照片返回去給前端展示即可:
@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");//生成文字驗證碼String text = producer.createText();//生成圖片驗證碼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調用接口,既可以看到結果:
總結
以上是生活随笔為你收集整理的Springboot中使用Google 的Kaptcha工具实现验证码校验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache的shiro获取当前Sess
- 下一篇: base64编码 springboot_