//驗證碼 @RequestMapping("verifycode.do") public void testcode(HttpServletRequest request,HttpServletResponse response) throws IOException { //請求設置 response.setDateHeader("Expires",0); response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); response.addHeader("Cache-Control", "post-check=0, pre-check=0"); response.setHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); //kaptcha對象 /* * <!-- 是否有邊框 --><prop key="kaptcha.border">yes</prop> <!-- 設置邊框顏色 --><prop key="kaptcha.border.color">105,179,90</prop> <!-- 獲取中文 --><prop key="kaptcha.textproducer.impl">org.cric.util.ChineseText</prop> <!-- 設置字體顏色 --><prop key="kaptcha.textproducer.font.color">black</prop> <!-- 設置驗證碼寬度 --><prop key="kaptcha.image.width">50</prop> <!-- 設置驗證碼高度 --><prop key="kaptcha.image.height">40</prop> <!-- 設置字體大小 --><prop key="kaptcha.textproducer.font.size">30</prop> <!-- 設置字體個數 --><prop key="kaptcha.textproducer.char.length">4</prop> <!-- 驗證碼文本字符間距 --> <prop key="kaptcha.textproducer.char.space">3</prop> <!-- 設置字體樣式 --> <prop key="kaptcha.textproducer.font.names">宋體,楷體,微軟雅黑</prop> <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop> <prop key="kaptcha.textproducer.char.string">0123456789abcdefghijklmnopqrstuvwxyz</prop> * */ DefaultKaptcha producer=new DefaultKaptcha(); //設置圖片驗證碼相關參數 Properties properties=new Properties(); properties.setProperty("kaptcha.border", "no"); properties.setProperty("kaptcha.border.color", "105,179,90"); properties.setProperty("kaptcha.textproducer.font.color", "black"); properties.setProperty("kaptcha.image.width", "100"); properties.setProperty("kaptcha.image.height", "40"); properties.setProperty("kaptcha.session.key", "code"); properties.setProperty("kaptcha.textproducer.char.length", "4"); properties.setProperty("kaptcha.textproducer.font.size", "30"); properties.setProperty("kaptcha.textproducer.char.space", "5"); properties.setProperty("kaptcha.textproducer.font.names", "宋體,楷體,微軟雅黑"); properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise"); properties.setProperty("kaptcha.textproducer.char.string","0123456789abcdefghijklmnopqrstuvwxyz"); Config config=new Config(properties); producer.setConfig(config); String capText = producer.createText(); request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); BufferedImage bi = producer.createImage(capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); try { out.flush(); } finally { out.close(); } }
pom文件git
<!--驗證碼--> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>