java生产图片验证码
1.第一步先引入架包
<!--生成验证码-->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
2.创建Bean并根据自己的需要进行配置
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha producer(){
Properties p =new Properties();
p.put("kaptcha.border","no");//是否有边框
p.put("kaptcha.textproducer.font.color","black");//字体颜色
p.put("kaptcha.image.width","135");//图片宽度
p.put("kaptcha.textproducer.char.string","ABCDEFHKIJZPYZPRSTWX123456789");//使用哪些字符生成验证码
p.put("kaptcha.image.height","50");//图片高度
p.put("kaptcha.textproducer.font.size","43");//字体大小
p.put("kaptcha.noise.color","blue");//字体颜色
p.put("kaptcha.textproducer.char.length","5");//字符的个数
p.put("kaptcha.textproducer.font.names","Arial");//字体
Config config =new Config(p);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
Kaptcha 详细配置表,可根据需要配置。
Constant | 描述 | 默认值 |
---|---|---|
kaptcha.border | 图片边框,合法值:yes , no | yes |
kaptcha.border.color | 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue | black |
kaptcha.image.width | 图片宽 | 200 |
kaptcha.image.height | 图片高 | 50 |
kaptcha.producer.impl | 图片实现类 | com.google.code.kaptcha.impl.DefaultKaptcha |
kaptcha.textproducer.impl | 文本实现类 | com.google.code.kaptcha.text.impl.DefaultTextCreator |
kaptcha.textproducer.char.string | 文本集合,验证码值从此集合中获取 | abcde2345678gfynmnpwx |
kaptcha.textproducer.char.length | 验证码长度 | 5 |
kaptcha.textproducer.font.names | 字体 | Arial, Courier |
kaptcha.textproducer.font.size | 字体大小 | 40px |
kaptcha.textproducer.font.color | 字体颜色,合法值: r,g,b 或者 white,black,blue | black |
kaptcha.textproducer.char.space | 文字间隔 | 2 |
kaptcha.noise.impl | 干扰实现类 | com.google.code.kaptcha.impl.DefaultNoise |
kaptcha.noise.color | 干扰 颜色,合法值: r,g,b 或者 white,black,blue | black |
kaptcha.obscurificator.impl | 图片样式: 水纹 com.google.code.kaptcha.impl.WaterRipple 鱼眼 com.google.code.kaptcha.impl.FishEyeGimpy 阴影 com.google.code.kaptcha.impl.ShadowGimpy | com.google.code.kaptcha.impl.WaterRipple |
kaptcha.background.impl | 背景实现类 | com.google.code.kaptcha.impl.DefaultBackground |
kaptcha.background.clear.from | 背景颜色渐变,开始颜色 | light grey |
kaptcha.background.clear.to | 背景颜色渐变, 结束颜色 | white |
kaptcha.word.impl | 文字渲染器 | com.google.code.kaptcha.text.impl.DefaultWordRenderer |
kaptcha.session.key | session key | KAPTCHA_SESSION_KEY |
kaptcha.session.date | session date | KAPTCHA_SESSION_DATE |
3.Controller 层的关键代码,在浏览器直接请求就可以看到了。
@Autowired
private Producer producer;
@ApiOperation(value = "生成验证码")
@GetMapping("getCode")
public void getCode(HttpServletResponse response) throws IOException {
//生产验证码
String text = producer.createText();
//生产图片验证码
BufferedImage img = producer.createImage(text);
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(img,"jpg",outputStream);
System.out.println("---------------"+text);
}