在pom文件中添加依赖:

<dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

 

在web.xml中配置一个servlet:

<!--利用servlet生成验证码-->
    <servlet>
        <servlet-name>Kaptcha</servlet-name>
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
        <!-- 是否有边框 -->
        <init-param>
            <param-name>kaptcha.border</param-name>
            <param-value>no</param-value>
        </init-param>
        <!-- 字体颜色 -->
        <init-param>
            <param-name>kaptcha.textproducer.font.color</param-name>
            <param-value>red</param-value>
        </init-param>
        <!-- 图片宽度 -->
        <init-param>
            <param-name>kaptcha.image.width</param-name>
            <param-value>135</param-value>
        </init-param>
        <!-- 使用哪些字符生成验证码 -->
        <init-param>
            <param-name>kaptcha.textproducer.char.string</param-name>
            <param-value>ACDEFHKPRSTWX345679</param-value>
        </init-param>
        <!-- 图片高度 -->
        <init-param>
            <param-name>kaptcha.image.height</param-name>
            <param-value>50</param-value>
        </init-param>
        <!-- 字体大小 -->
        <init-param>
            <param-name>kaptcha.textproducer.font.size</param-name>
            <param-value>43</param-value>
        </init-param>
        <!-- 干扰线的颜色 -->
        <init-param>
            <param-name>kaptcha.noise.color</param-name>
            <param-value>black</param-value>
        </init-param>
        <!-- 字符个数 -->
        <init-param>
            <param-name>kaptcha.textproducer.char.length</param-name>
            <param-value>4</param-value>
        </init-param>
        <!-- 字体 -->
        <init-param>
            <param-name>kaptcha.textproducer.font.names</param-name>
            <param-value>Arial</param-value>
        </init-param>
        <!-- Session的key -->
        <init-param>
            <param-name>kaptcha.session.key</param-name>
            <param-value>captcha</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Kaptcha</servlet-name>
        <url-pattern>/captcha</url-pattern>
    </servlet-mapping>

 

建立Controller:

 1 package com.yas.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 import javax.servlet.http.HttpSession;
 7 
 8 @Controller
 9 @RequestMapping("/captcha")
10 public class CaptchaController {
11 
12     @RequestMapping("/test1")
13     public String test1(String captcha, HttpSession session) {
14         String realCaptcha = (String) session.getAttribute("captcha");
15         if (realCaptcha.equals(captcha)) {
16             return "hello";
17         }
18         return "error1";
19     }
20 }

 

建立测试页面captcha.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script>
        function refresh(){
            var img = document.getElementById("cap");
            img.src = "${pageContext.request.contextPath}/captcha?"+new Date().getTime();

        }
    </script>
</head>
<body>
<form action="${pageContext.request.contextPath}/captcha/test1">
    <img id="cap" src="${pageContext.request.contextPath}/captcha" onclick="refresh()">
    <input type="text" name="captcha">
    <br>
    <input type="submit" value="提交">
</form>
</body>
</html>