在现在网站中很多还是使用比较传统的图片作为校验用户数据的标准,比如发送短信都要输入图片校验码,或者登录也需要使用图片校验码,防止暴力破解。
图片生成接口
@Controller
@RequestMapping("/image")
public class ImageController {
private static final String IMG_EXTENDS = "JPEG";
@Permition(oriented = Permit.All)
@RequestMapping(value = "/verification", method = RequestMethod.GET)
public void verificationCreator(@RequestParam String uid,HttpServletResponse response) throws IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setContentType("image/jpeg");
response.setDateHeader("Expires", 0);
String identifying = MathUtils.randomNumberStr(4);
ServletOutputStream output = null;
try {
output = response.getOutputStream();
BufferedImage image = new VerificationImageGenerator(new Random()).bufferedImage(identifying);
ImageIO.write(image, IMG_EXTENDS, output);
} finally {
IOUtils.closeQuietly(output);
}
}
//图片生成器
private static final class VerificationImageGenerator {
private static final int WIDTH = 70;
private static final int HEIGHT = 35;
private static final Color IMG_BACK_1 = new Color(165, 42, 42);
private static final Color IMG_BACK_2 = new Color(238, 18, 137);
private static final Color IMG_BACK_3 = new Color(100, 149, 237);
private static final Color IMG_BACK_4 = new Color(238, 238, 0);
private static final int FONT_SIZE = 25;
private final Random random;
public VerificationImageGenerator(Random random) {
this.random = random;
}
public BufferedImage bufferedImage(String identifying) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setFont(new Font("Times New Roman", Font.BOLD, FONT_SIZE));
for(int i = 0; i < identifying.length(); i++){
if(i+1==1){
g.setColor(IMG_BACK_1);
}else if(i+1==2){
g.setColor(IMG_BACK_2);
}else if(i+1==3){
g.setColor(IMG_BACK_3);
}else if(i+1==4){
g.setColor(IMG_BACK_4);
}
g.drawString(String.valueOf(identifying.charAt(i)), 13 * i + 6, 25);
}
//g.drawOval(0, 12, 60, 11);
for (int i = 0; i < 100; i++) {
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
g.drawLine(x, y, x, y);
}
g.dispose();
return image;
}
}
}
辅助工具类
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class MathUtils {
private static final int DEFAULT_SCALE = 5;
public static String randomNumberStr(int length) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < length; i++) {
list.add(new Random().nextInt(10));
}
Collections.shuffle(list);
StringBuilder sb = new StringBuilder();
list.forEach(sb::append);
return sb.toString();
}
public static String random(int len){
String[] str = {"1234567890","ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"};
Random r = new Random();
StringBuilder result= new StringBuilder();
for (int i =0; i < len; i++ ){
for(String s:str){
if(result.toString().length()==len){
break;
}
int n = r.nextInt(s.length());
result.append(s.substring(n, n +1));
}
}
return result.toString();
}
public static String getOrderNo(){
String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
String seconds = new SimpleDateFormat("HHmmss").format(new Date());
return date+"00010"+getTwo()+"00"+seconds+getTwo();
}
public static BigDecimal nvl(BigDecimal num) {
return num == null ? BigDecimal.ZERO : num;
}
public static Long nvl(Long num) {
return num == null ? 0L : num;
}
public static Integer nvl(Integer num) {
return num == null ? 0 : num;
}
public static BigDecimal limitScale(BigDecimal decimal) {
return limitScale(decimal, DEFAULT_SCALE);
}
public static BigDecimal limitScale(BigDecimal decimal, int scale) {
decimal = nvl(decimal);
return decimal.scale() < scale ? decimal : decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
}
public static Integer ceil(Long total, Integer size){
if(nvl(total) <= 0 || nvl(size) <= 0){
return 0;
}
return (int)(Math.ceil(total.doubleValue() / size.doubleValue()));
}
public static String getTwo(){
Random rad=new Random();
String result = rad.nextInt(100) +"";
if(result.length()==1){
result = "0" + result;
}
return result;
}
}