需要使用kaptcha插件。
一、实现流程
(1)在pom.xml引入如下依赖配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> <scope>system</scope> <systemPath>E:\谢宏滔\方法记忆\常用包\kaptcha-2.3.2.jar</systemPath> </dependency> <dependency> <groupId>com.jhlabs</groupId> <artifactId>filters</artifactId> <version>2.0.235</version> <scope>system</scope> <systemPath>E:\谢宏滔\方法记忆\常用包\filters-2.0.235.jar</systemPath> </dependency> |
需要如下两个包,因为我在网上的maven仓库加载这两个包都失败了(不知道是我的问题还是仓库的问题),所以我就下载到本地进行加载了。
这里可能会有一个问题就是本地文件不能发布到tomcat服务器中,导致:java.lang.NoClassDefFoundError 错误,这个时候需要手动将这两个jar包加入tomcat的lib中,才能在tomcat中运行。
(2)在spring配置文件spring.xml中加入配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <bean id="captcha" class="com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="config"> <bean class="com.google.code.kaptcha.util.Config"> <constructor-arg> <props> <prop key="kaptcha.border">no</prop> <prop key="kaptcha.border.color">105,179,90</prop> <prop key="kaptcha.textproducer.font.color">red</prop> <prop key="kaptcha.image.width">250</prop> <prop key="kaptcha.textproducer.font.size">90</prop> <prop key="kaptcha.image.height">90</prop> <prop key="kaptcha.session.key">code</prop> <prop key="kaptcha.textproducer.char.length">4</prop> <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑 </prop> </props> </constructor-arg> </bean> </property> </bean> </beans> |
就是把kaptcha的相关类加载到spring的ioc中,用来调用。
(3)把相关逻辑写入controller中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
@ResponseBody @RequestMapping(value = "testkaptcha") public void testkaptcha(HttpServletRequest request,HttpServletResponse response) throws IOException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/spring.xml"); Producer producer = (Producer) applicationContext.getBean("captcha"); HttpSession session = request.getSession(); 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"); String capText = producer.createText(); session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); String code = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY); System.out.println(code); BufferedImage bi = producer.createImage(capText); ServletOutputStream out = null; try { out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { out.close(); } } |
其实和之前的单纯实现spring mvc验证码差不多。
(4)实现效果
不是很好看,但是将就吧…想要实现其他效果可以在spring.xml中相关配置那里加。
而服务端则获取session的值:den2,可以用于接下来的验证操作。
二、总结
说实话,直接写验证码的实现类可能比用这个包效果还好些…
实现出来效果其实是差不多的。而且手写可以随心所欲加线条加噪点。
这个kaptcha包还需要去研究下怎么去操作图形,其实并没有想象中方便。