1.生成驗證碼的工具類
-
package com.quanran.common.util;
-
import java.awt.BasicStroke;
-
import java.awt.Color;
-
import java.awt.Font;
-
import java.awt.Graphics2D;
-
import java.awt.image.BufferedImage;
-
import java.util.Random;
-
public class VerifyCodeUtils {
-
private int w = 70;
-
private int h = 35;
-
private String[] fontNames = { "宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312" };
-
private String codes = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
-
private String text;
-
private Random r = new Random();
-
private Color randomColor() {
-
int red = r.nextInt(150);
-
int green = r.nextInt(150);
-
int blue = r.nextInt(150);
-
return new Color(red, green, blue);
-
}
-
private Font randomFont() {
-
int index = r.nextInt(fontNames.length);
-
String fontName = fontNames[index];
-
int style = r.nextInt(4);
-
int size = r.nextInt(5) + 24;
-
return new Font(fontName, style, size);
-
}
-
private void drawLine(BufferedImage image) {
-
// 幹擾線的個數
-
int num = 3;
-
Graphics2D g2 = (Graphics2D) image.getGraphics();
-
for (int i = 0; i < num; i++) {
-
// (x1,y1)為幹擾線的起始點
-
int x1 = r.nextInt(w);
-
int y1 = r.nextInt(h);
-
// (x2,y2)為幹擾線的結束點
-
int x2 = r.nextInt(w);
-
int y2 = r.nextInt(h);
-
// 設定幹擾線的寬度
-
g2.setStroke(new BasicStroke(1.5F));
-
// 幹擾線的顔色
-
g2.setColor(Color.blue);
-
// 将目前這條幹擾線畫出來
-
g2.drawLine(x1, y1, x2, y2);
-
}
-
}
-
private char randomChar() {
-
int index = r.nextInt(codes.length());
-
return codes.charAt(index);
-
}
-
public BufferedImage createImage() {
-
// BufferedImage的構造(寬度,高度和圖檔類型)
-
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
-
Graphics2D g2 = (Graphics2D) image.getGraphics();
-
// 下面兩行代碼是為了解決圖檔背景色為黑色的問題,我設定成了白色
-
g2.setColor(Color.white);
-
g2.fillRect(0, 0, w, h);
-
// 可變字元串存儲圖檔裡的文本
-
StringBuilder sb = new StringBuilder();
-
// 向圖中畫四個字元
-
for (int i = 0; i < 4; i++) {
-
String s = randomChar() + "";
-
sb.append(s);
-
float x = i * 1.0F * w / 4;
-
g2.setFont(randomFont());
-
g2.setColor(randomColor());
-
g2.drawString(s, x, h - 5);
-
}
-
this.text = sb.toString();
-
drawLine(image);
-
// 傳回圖檔
-
return image;
-
}
-
public String getText() {
-
return text;
-
}
-
}
2.調用工具類
-
package com.quanran.invite.controller;
-
import io.swagger.annotations.Api;
-
import io.swagger.annotations.ApiImplicitParam;
-
import io.swagger.annotations.ApiImplicitParams;
-
import io.swagger.annotations.ApiOperation;
-
import io.swagger.annotations.ApiParam;
-
import java.awt.image.BufferedImage;
-
import java.io.OutputStream;
-
import javax.imageio.ImageIO;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestParam;
-
import org.springframework.web.bind.annotation.RestController;
-
import com.quanran.visitor.common.util.VerifyCodeUtils;
-
@Api(value="invite系統裡的邀請碼重新發送接口", description="invite系統裡的邀請碼重新發送接口")
-
@RestController
-
@RequestMapping("/invitationCode")
-
public class InvitationCodeController {
-
@ApiOperation("短信重發的驗證碼接口")
-
@GetMapping("getVerifyCode")
-
@ApiImplicitParams({
-
@ApiImplicitParam(name = "verifyCodeKey", value = "驗證碼的key", required = true, paramType = "query")
-
})
-
public void getVerifyCode(HttpServletRequest request, HttpServletResponse response,
-
@ApiParam(value="驗證碼的key", required = true) @RequestParam String verifyCodeKey) {
-
// 生成驗證碼的圖檔
-
VerifyCodeUtils code = new VerifyCodeUtils();
-
BufferedImage image = code.createImage();
-
// 設定響應頭通知浏覽器以圖檔的形式打開
-
response.setContentType("image/jpeg");
-
// 設定響應頭控制浏覽器不要緩存
-
response.setHeader("Pragma","no-cache");
-
response.setHeader("Cache-Control","no-cache");
-
response.setIntHeader("Expires",-1);
-
// 将圖檔變成流寫給浏覽器
-
OutputStream os=response.getOutputStream();
-
ImageIO.write(image, "jpg", os);
-
// 清空關閉流
-
os.flush();
-
os.close();
-
os=null;
-
response.flushBuffer();
-
}
-
}
備注:verifyCodeKey參數是前台傳過來的時間戳,開發中,我們會将此時間戳當做key,生成的驗證碼内容當做value,存到redis中