场景---图形验证码的使用
1、接口
public interface ValidateCodeGenerator {
ImageCode createImageCode(HttpServletRequest request);
}
2、默认实现
@Data
public class ImageCodeGenerator implements ValidateCodeGenerator {
@Autowired
private SecurityProperties securityProperties;
@Override
public ImageCode createImageCode(HttpServletRequest request) {
//从配置读取
int width = ServletRequestUtils.getIntParameter(request, "width", securityProperties.getCode().getImage().getWidth());
int height = ServletRequestUtils.getIntParameter(request, "height", securityProperties.getCode().getImage().getHeight());
//step1 创建一个图片,创建一个内存映像对象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//得到画笔
Graphics g = image.getGraphics();
Random r = new Random();
//设置画笔的颜色
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
//设置整个画板的背景颜色
g.fillRect(0, 0, width, height);
//生成一个随机数
//String number = r.nextInt(99999) + "";
String number = getNumber(securityProperties.getCode().getImage().getLength());
g.setColor(new Color(0, 0, 0));
g.drawString(number, 5, 15);
//加一些干扰线
for (int i = 0; i < 4; i++) {
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.drawLine(r.nextInt(60), r.nextInt(20), r.nextInt(60), r.nextInt(20));
}
return new ImageCode(image, number, securityProperties.getCode().getImage().getExpireIn());
}
//随机从"A~Z","0~9"当中取出5个字符
private String getNumber(int length) {
Random r = new Random();
char[] chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0',
'1', '2', '3', '4', '5', '6', '7', '8', '9'};
String number = "";
for (int i = 0; i < length; i++) {
number += chars[r.nextInt(chars.length)];
}
return number;
}
}
3、配置
@Configuration
public class ValidateCodeBeanConfig {
@Autowired
private SecurityProperties securityProperties;
@Bean
@ConditionalOnMissingBean(name = "imageCodeGenerator") //在容器中去找是否有imageCOdeGenerator。如果没有就用方法内配置的bean
public ValidateCodeGenerator imageCodeGenerator(){
ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
codeGenerator.setSecurityProperties(securityProperties);
return codeGenerator;
}
}
4、使用
@RestController
public class ValidateCodeController {
public static final String SESSION_KEY = "SESSION_IMAGE_CODE";
private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();
@Autowired
private ValidateCodeGenerator imageCodeGenerator;
@GetMapping("/code/image")
public void createCode(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ImageCode imageCode = imageCodeGenerator.createImageCode(request);
sessionStrategy.setAttribute(new ServletWebRequest(request), SESSION_KEY, imageCode);
ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream());
}
}
5、增量
/**
* 配置一个bean去覆盖默认的bean(ValidateCodeBeanConfig-》imageCodeGenerator)
*/
@Component("imageCodeGenerator")
public class DemoImageCodeGenerator implements ValidateCodeGenerator {
@Override
public ImageCode createImageCode(HttpServletRequest request) {
System.out.println("更高级的图形验证码生成代码");
return null;
}
}
网友评论