官方的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; } }