一般網站或平臺爲了安全性考慮,會增長驗證碼的功能,以防遭遇惡意機器註冊或軟件暴力對密碼刷字典破解,這裏採用的是google的kaptcha進行了簡單封裝;建立驗證碼圖片操做工具類;前端
pom.xml增長jar依賴java
<!-- 成生圖片驗證碼依賴 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
KaptchaUtils工具類git
import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.Properties; /** * @Description 建立驗證碼圖片操做工具類 * @Author JL * @Date 2019/08/05 * @Version V1.0 */ public class KaptchaUtils { /** * 建立驗證碼對象,並設置樣式 * @return */ private static DefaultKaptcha getDefaultKaptcha(){ DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); Config config = new Config(kaptchaCss()); defaultKaptcha.setConfig(config); return defaultKaptcha; } /** * 設置驗證碼圖片生成的樣式屬性 * @return */ private static Properties kaptchaCss(){ Properties properties = new Properties(); properties.setProperty("kaptcha.border", "yes"); properties.setProperty("kaptcha.border.color", "105,179,90"); properties.setProperty("kaptcha.textproducer.font.color", "blue"); properties.setProperty("kaptcha.image.width", "110"); properties.setProperty("kaptcha.image.height", "40"); properties.setProperty("kaptcha.textproducer.font.size", "30"); properties.setProperty("kaptcha.session.key", "code"); properties.setProperty("kaptcha.textproducer.char.length", "4"); properties.setProperty("kaptcha.textproducer.font.names", "宋體,楷體,微軟雅黑"); return properties; } /** * 建立驗證碼,返回驗證碼值與字節數組(字節數組用於向前端輸出) * @param defaultKaptcha * @return * @throws Exception */ private static KaptchaVo defaultKaptcha(DefaultKaptcha defaultKaptcha) throws Exception { KaptchaVo vo = new KaptchaVo(); String createText = defaultKaptcha.createText();//生產驗證碼字符串並保存到session中 ByteArrayOutputStream jpegOutputStream = null; try { jpegOutputStream = new ByteArrayOutputStream(); //使用生產的驗證碼字符串返回一個BufferedImage對象並轉爲byte寫入到byte數組中 BufferedImage challenge = defaultKaptcha.createImage(createText); ImageIO.write(challenge, "jpg", jpegOutputStream); vo.setCode(createText); vo.setImgs(jpegOutputStream.toByteArray()); } catch (IllegalArgumentException e) { throw e; }finally{ if (jpegOutputStream != null) { jpegOutputStream.flush(); jpegOutputStream.close(); } } return vo; } /** * 建立驗證碼,並生成圖片到指定位置,返回驗證碼值 * @param defaultKaptcha * @param imgPath * @return * @throws Exception */ private static String defaultKaptcha(DefaultKaptcha defaultKaptcha, String imgPath) throws Exception { String createText = defaultKaptcha.createText();//生產驗證碼字符串並保存到session中 BufferedOutputStream jpegOutputStream = null; try { jpegOutputStream = new BufferedOutputStream(new FileOutputStream(new File(imgPath))); //使用生產的驗證碼字符串返回一個BufferedImage對象並轉爲byte寫入到byte數組中 BufferedImage challenge = defaultKaptcha.createImage(createText); ImageIO.write(challenge, "jpg", jpegOutputStream); } catch (IllegalArgumentException e) { throw e; }finally{ if (jpegOutputStream != null) { jpegOutputStream.flush(); jpegOutputStream.close(); } } return createText; } /** * 建立驗證碼,返回驗證碼值與字節數組(字節數組用於向前端輸出) * @return * @throws Exception */ public static KaptchaVo createKaptcha() throws Exception { DefaultKaptcha defaultKaptcha = getDefaultKaptcha(); return defaultKaptcha(defaultKaptcha); } /** * 建立驗證碼,並生成圖片到指定位置,返回驗證碼值 * @param imgPath * @return * @throws Exception */ public static String createKaptcha(String imgPath) throws Exception { DefaultKaptcha defaultKaptcha = getDefaultKaptcha(); return defaultKaptcha(defaultKaptcha, imgPath); } public static class KaptchaVo { private String code;//驗證碼 private byte[] imgs;//字節數組 public String getCode() { return code; } public void setCode(String code) { this.code = code; } public byte[] getImgs() { return imgs; } public void setImgs(byte[] imgs) { this.imgs = imgs; } } public static void main(String[] args) throws Exception { //傳入圖片文件地址,返回驗證碼值 // String imgPath = "D:/temp/2.jpg"; // KaptchaUtils.createKaptcha(imgPath); //建立驗證碼,返回驗證碼值與字節數組,可將字節數組輸出到前端 KaptchaVo vo = KaptchaUtils.createKaptcha(); System.out.println(vo.code); System.out.println(vo.getImgs().length); } }
說明:github
作過項目的人都知道,不少寫過的可重複利用的代碼塊或有用的工具類沒有怎麼整理,當須要的時候,又得打開項目查找一翻,雖然功能開發不難,可是又得花時間成本去寫去測試,這樣的重複造輪子的事情太屢次了;所以不如把輪子保留,供你們一塊兒使用;數組
1.這個輪子能夠有:須要使用的時候確實還不存在這個組件。
2.我須要的時候輪子不在:每一種技術或工具產生都有它的項目背景,當代碼寫在項目裏的時候,我知道有這個東西,當換了一個項目或公司後,沒有備份也沒有記錄,這個時候你不在了,又得花時間手打一遍;
3.我不知道是否是造輪子:大多數狀況下初學者很難分清楚本身是否是在重複造輪子,事實上造輪子不是我目的。個人目的是完成工做任務,任務完成的速度越快越好,質量越高越好。而不是去判斷本身在不在造輪子。
4.不想重複花時間造輪子:有時候還會碰到一些並不困難可是很佔時間的東西,固然有現成的輪子是花時間最少的;
5.我就是想學習輪子:初學者的並非在重複造輪子,而是學習後以提升爲本身的知識與技能。安全
輪子有過測試,但不免有失誤,若有錯誤處,還敬請指出;session