基於前后端分離或者非前后端分離都可以使用的java驗證碼實現方法
第一步:引入依賴
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
第二步:增加驗證碼生成的配置類,顏色,字體,干擾線等配置都在這里修改
參數參考地址:https://www.cnblogs.com/louis80/p/5230507.html
package com.zp.springbootdemo.util; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; /** * @Author zp * @Description 驗證碼生成的配置類 * @Date 21:24 2020/5/7 * @Param * @return **/ @Configuration public class KaptchaConfig { @Bean public DefaultKaptcha producer(){ DefaultKaptcha defaultKaptcha=new DefaultKaptcha(); Properties properties=new Properties(); properties.setProperty("kaptcha.border", "no"); properties.setProperty("kaptcha.border.color", "white"); properties.setProperty("kaptcha.textproducer.font.color", "255,192,55"); properties.setProperty("kaptcha.image.width", "125"); properties.setProperty("kaptcha.image.height", "45"); properties.setProperty("kaptcha.session.key", "code"); properties.setProperty("kaptcha.textproducer.font.size", "38"); properties.setProperty("kaptcha.noise.color","21,113,171"); properties.setProperty("kaptcha.background.clear.from","0,154,255"); properties.setProperty("kaptcha.background.clear.to","0,202,255"); properties.setProperty("kaptcha.textproducer.char.length", "4"); properties.setProperty("kaptcha.textproducer.font.names", "Arial"); Config config=new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
第三步:驗證碼的生成
通過DefaultKaptcha 類的createText()方法可生成文字驗證碼,文字驗證碼生成之后再生成圖片驗證碼。返回給前端時可返回base64編碼的圖片驗證碼字符串,前端進行處理。也可直接返回圖片。
1.方式一,獲取圖片驗證碼,返回一個base64編碼的圖片字符串
@Autowired private DefaultKaptcha producer; /** * @Author zp * @Description //獲取圖片驗證碼,返回一個base64編碼的圖片字符串 * @Date 22:48 2020/5/7 * @Param [] * @return java.lang.String **/ @GetMapping(value = "/default") public String generateVerificationCode() throws Exception { // 生成文字驗證碼,為了進行校驗,可將文字驗證碼存入redis中 String text = producer.createText(); // 生成圖片驗證碼 ByteArrayOutputStream outputStream = null; BufferedImage image = producer.createImage(text); outputStream = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", outputStream); BASE64Encoder encoder = new BASE64Encoder(); String imageStr=encoder.encode(outputStream.toByteArray()); //利用在線工具測試返回的base64編碼的圖片驗證碼字符串是否可以解析http://www.vgot.net/test/image2base64.php return imageStr; }
2.方式二 ,獲取圖片驗證碼,圖片驗證碼直接渲染到頁面上
/** * @Author zp * @Description //獲取圖片驗證碼,圖片驗證碼直接渲染到頁面上 * @Date 23:30 2020/5/7 * @Param [httpServletRequest, httpServletResponse] * @return void **/ @GetMapping("/getCode") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{ byte[] captchaChallengeAsJpeg = null; ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); try { //生成文字驗證碼,為了進行校驗,可將文字驗證碼存入redis中 String createText = producer.createText(); //使用生產的驗證碼字符串返回一個BufferedImage對象並轉為byte寫入到byte數組中 BufferedImage challenge = producer.createImage(createText); ImageIO.write(challenge, "jpg", jpegOutputStream); } catch (IllegalArgumentException e) { httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } //定義response輸出類型為image/jpeg類型,使用response輸出流輸出圖片的byte數組 captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); httpServletResponse.setHeader("Cache-Control", "no-store"); httpServletResponse.setHeader("Pragma", "no-cache"); httpServletResponse.setDateHeader("Expires", 0); httpServletResponse.setContentType("image/jpeg"); ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream(); responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush(); responseOutputStream.close(); }
第四步:驗證碼的校驗
驗證碼的校驗時,前端需要將生成的驗證碼傳到后端,后端進行比較。在生成驗證碼時,可將生成的文字驗證碼存入redis或session中,進行比較時需要從redis或session中獲取,然后進行比較。代碼省略。。。