Google Kaptcha 生成图形验证码

官方的pom文档

<dependency>  
    <groupId>com.google.code.kaptcha</groupId>  
    <artifactId>kaptcha</artifactId>  
    <version>2.3.2</version>  
</dependency>

阿里的maven仓库pom

<dependency>  
    <groupId>com.github.penggle</groupId>  
    <artifactId>kaptcha</artifactId>  
    <version>2.3.2</version>  
</dependency>

控制验证码的图片的生成的规则的配置信息都放到了com.google.code.kaptcha.util.Config类中

import java.awt.Color;
import java.awt.Font;
import java.util.Properties;
 
import com.google.code.kaptcha.BackgroundProducer;
import com.google.code.kaptcha.GimpyEngine;
import com.google.code.kaptcha.NoiseProducer;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultBackground;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.impl.DefaultNoise;
import com.google.code.kaptcha.impl.WaterRipple;
import com.google.code.kaptcha.text.TextProducer;
import com.google.code.kaptcha.text.WordRenderer;
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
import com.google.code.kaptcha.text.impl.DefaultWordRenderer;
import com.google.code.kaptcha.util.ConfigHelper;
 
public class Config
{
  private Properties properties;
  private ConfigHelper helper;
  
  public Config(Properties properties)
  {
    this.properties = properties;
    this.helper = new ConfigHelper();
  }
  
  /**
   * 设置图片是否有边框
   * @return
   */
  public boolean isBorderDrawn()
  {
    String paramName = "kaptcha.border";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getBoolean(paramName, paramValue, true);
  }
  
  /**
   * 边框颜色   合法值: r,g,b (and optional alpha) 或者 white,black,blue.
   * @return
   */
  public Color getBorderColor()
  {
    String paramName = "kaptcha.border.color";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getColor(paramName, paramValue, Color.BLACK);
  }
  
  /**
   * 边框厚度  合法值:>0
   * @return
   */
  public int getBorderThickness()
  {
    String paramName = "kaptcha.border.thickness";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getPositiveInt(paramName, paramValue, 1);
  }
  
  /**
   * 文本集合,验证码值从此集合中获取
   * @return
   */
  public char[] getTextProducerCharString()
  {
    String paramName = "kaptcha.textproducer.char.string";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getChars(paramName, paramValue, "abcde2345678gfynmnpwx".toCharArray());
  }
  
  /**
   * 验证码长度
   * @return
   */
  public int getTextProducerCharLength()
  {
    String paramName = "kaptcha.textproducer.char.length";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getPositiveInt(paramName, paramValue, 5);
  }
  
  /**
   * 字体类型
   * @param fontSize 见Font中的定义
   * @return
   */
  public Font[] getTextProducerFonts(int fontSize)
  {
    String paramName = "kaptcha.textproducer.font.names";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getFonts(paramName, paramValue, fontSize, new Font[] { new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) });
  }
  
  /**
   * 字体大小
   * @return
   */
  public int getTextProducerFontSize()
  {
    String paramName = "kaptcha.textproducer.font.size";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getPositiveInt(paramName, paramValue, 40);
  }
  
  /**
   * 字体颜色  rgb颜色或者Color中的值
   * @return
   */
  public Color getTextProducerFontColor()
  {
    String paramName = "kaptcha.textproducer.font.color";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getColor(paramName, paramValue, Color.BLACK);
  }
  
  /**
   * 干扰线的颜色
   * @return
   */
  public Color getNoiseColor()
  {
    String paramName = "kaptcha.noise.color";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getColor(paramName, paramValue, Color.BLACK);
  }
    
  /**
   * 背景颜色渐变色开始色  rgb或者Color中定义的
   * @return
   */
  public Color getBackgroundColorFrom()
  {
    String paramName = "kaptcha.background.clear.from";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getColor(paramName, paramValue, Color.LIGHT_GRAY);
  }
  
  /**
   * 背景颜色渐变色结束色   rgb或者Color中定义的
   * @return
   */
  public Color getBackgroundColorTo()
  {
    String paramName = "kaptcha.background.clear.to";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getColor(paramName, paramValue, Color.WHITE);
  }
  
  /**
   * 图片的宽度
   * @return
   */
  public int getWidth()
  {
    String paramName = "kaptcha.image.width";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getPositiveInt(paramName, paramValue, 200);
  }
  
  /**
   * 图片的高度
   * @return
   */
  public int getHeight()
  {
    String paramName = "kaptcha.image.height";
    String paramValue = this.properties.getProperty(paramName);
    return this.helper.getPositiveInt(paramName, paramValue, 50);
  }
  
  /**
   * 图片的session key
   * @return
   */
  public String getSessionKey()
  {
    return this.properties.getProperty("kaptcha.session.key", "KAPTCHA_SESSION_KEY");
  }
  
  public Properties getProperties()
  {
    return this.properties;
  }
  
  /**
   * 生成默认的图片生产者实现
   * @return
   */
  public Producer getProducerImpl()
  {
    String paramName = "kaptcha.producer.impl";
    String paramValue = this.properties.getProperty(paramName);
    Producer producer = (Producer)this.helper.getClassInstance(paramName, paramValue, new DefaultKaptcha(), this);
    return producer;
  }
  
  /**
   * 生成默认的验证码文字生产者实现
   * @return
   */
  public TextProducer getTextProducerImpl()
  {
    String paramName = "kaptcha.textproducer.impl";
    String paramValue = this.properties.getProperty(paramName);
    TextProducer textProducer = (TextProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultTextCreator(), this);
    
    return textProducer;
  }
  
  /**
   * 文字干扰实现类,默认DefaultNoise,还可以选择com.google.code.kaptcha.impl.NoNoise没有干扰线的实现类
   * @return
   */
  public NoiseProducer getNoiseImpl()
  {
      String paramName = "kaptcha.noise.impl";
      String paramValue = this.properties.getProperty(paramName);
      NoiseProducer noiseProducer = (NoiseProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultNoise(), this);
      
      return noiseProducer;
  }
  
  /**
   * 图片样式的实现类,默认WaterRipple(水纹),还有下面2种可选
   * 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy    阴影com.google.code.kaptcha.impl.ShadowGimpy
   * 
   * @return
   */
  public GimpyEngine getObscurificatorImpl()
  {
    String paramName = "kaptcha.obscurificator.impl";
    String paramValue = this.properties.getProperty(paramName);
    GimpyEngine gimpyEngine = (GimpyEngine)this.helper.getClassInstance(paramName, paramValue, new WaterRipple(), this);
    return gimpyEngine;
  }
  
  /**
   * 文字渲染实现类,默认DefaultWordRenderer,也只有这一个默认的实现类
   * @return
   */
  public WordRenderer getWordRendererImpl()
  {
    String paramName = "kaptcha.word.impl";
    String paramValue = this.properties.getProperty(paramName);
    WordRenderer wordRenderer = (WordRenderer)this.helper.getClassInstance(paramName, paramValue, new DefaultWordRenderer(), this);
    
    return wordRenderer;
  }
  
  /**
   * 背景图片实现类,默认DefaultBackground,也只有这一个默认实现类
   * @return
   */
  public BackgroundProducer getBackgroundImpl()
  {
    String paramName = "kaptcha.background.impl";
    String paramValue = this.properties.getProperty(paramName);
    BackgroundProducer backgroundProducer = (BackgroundProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultBackground(), this);
    
    return backgroundProducer;
  }
}

spring bean的配置

<!-- google kaptcha的相关配置-->
    <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">  
        <property name="config">  
            <bean class="com.google.code.kaptcha.util.Config">  
                <constructor-arg>  
                    <props> 
                        <!-- 是否有边框 可选yes 或者 no --> 
                        <prop key="kaptcha.border">yes</prop>  
                        <!-- 边框颜色 -->
                        <prop key="kaptcha.border.color">105,179,90</prop>  
                        <!-- 验证码文本字符颜色 -->
                        <prop key="kaptcha.textproducer.font.color">blue</prop>  
                        <!-- 验证码文本字符大小 -->
                        <prop key="kaptcha.textproducer.font.size">45</prop>  
                        <!-- 验证码图片的宽度 默认200 -->
                        <prop key="kaptcha.image.width">125</prop>  
                        <!-- 验证码图片的高度 默认50 -->
                        <prop key="kaptcha.image.height">45</prop>  
                        <!-- 验证码文本字符长度  默认为5 -->
                        <prop key="kaptcha.textproducer.char.length">4</prop>  
                        <!-- 验证码文本字体样式  默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)  -->
                        <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>  
                    </props>  
                </constructor-arg>  
            </bean>  
        </property>  
    </bean>

springboot使用配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
 
@Configuration
public class KaptchaConfig {
 
    @Bean
    public Producer KaptchaProducer() {
        Properties kaptchaProperties = new Properties();
        kaptchaProperties.put("kaptcha.border", "no");
        kaptchaProperties.put("kaptcha.textproducer.char.length","4");
        kaptchaProperties.put("kaptcha.image.height","50");
        kaptchaProperties.put("kaptcha.image.width","150");
        kaptchaProperties.put("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy");
        kaptchaProperties.put("kaptcha.textproducer.font.color","black");
        kaptchaProperties.put("kaptcha.textproducer.font.size","40");
        kaptchaProperties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
        //kaptchaProperties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.DefaultNoise");
        kaptchaProperties.put("kaptcha.textproducer.char.string","acdefhkmnprtwxy2345678");
 
        Config config = new Config(kaptchaProperties);
        return config.getProducerImpl();
    }
}
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
 
@Controller
@Slf4j
public class KaptchaController {
 
    private final Producer captchaProducer;
 
    @Autowired
    public KaptchaController(Producer captchaProducer) {
        this.captchaProducer = captchaProducer;
    }
 
    @RequestMapping("/image/code")
    public ModelAndView kaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        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 = captchaProducer.createText();
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
 
        String code = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        log.info("输出验证码:[{}]", code);
 
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        out.flush();
        out.close();
        return null;
    }
}

 

posted @ 2020-05-10 00:26  King-DA  阅读(1120)  评论(0编辑  收藏  举报