Kaptcha image verification code made by Java, integrated in the ssm framework

The Kaptcha image verification code made by Java is integrated in the ssm framework. Here it is called by the interface. The configuration of the image verification code uses the set method. This is the Controller code.

//验证码
@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 file

        <!--验证码-->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325397639&siteId=291194637