通過kaptche 生成圖形驗證碼

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;

<dependency>
    <groupId>com.github.axet</groupId>
    <artifactId>kaptcha</artifactId>
    <version>0.0.9</version>
</dependency>

 

@Configuration
public class KaptchaConfig {
    @Bean
    public DefaultKaptcha producer() {
        Properties properties = new Properties();
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.textproducer.font.color", "black");
        properties.put("kaptcha.textproducer.char.space", "5");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

 

httpServletResponse.setHeader("Cache-Control", "no-store, no-cache");
httpServletResponse.setContentType("image/jpeg");
//獲取隨機驗證碼
String text = producer.createText();
//驗證碼保存到shiro
SecurityUtils.getSubject().getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, text);
//通過文字生成圖片
System.out.println(text);
BufferedImage image = producer.createImage(text);
//返回jpg
ImageIO.write(image, "jpg", httpServletResponse.getOutputStream());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章