最近有個需求,需要返回驗證碼,以前我一直以爲有驗證碼是在前端做的,後面才發現,前端存儲的只是一個圖片,和一個 session 數據,用戶根據圖片輸入內容,前端取出 session 的內容進行校驗,這就是最常見的驗證碼邏輯。
這裏直接創建一個簡單的 springboot 工程。
只要有 spring web 模塊即可
主要工程結構
com.example.verificationcode.VerificationcodeApplication
package com.example.verificationcode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class VerificationcodeApplication {
public static void main(String[] args) {
SpringApplication.run(VerificationcodeApplication.class, args);
}
}
com.example.verificationcode.VerificationCodeController
package com.example.verificationcode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
@Controller
public class VerificationCodeController {
/**
* 二維碼接口
* @param request
* @param response
*/
/* 獲取驗證碼圖片*/
@RequestMapping("/getVerifyCode")
public void getVerificationCode(HttpServletResponse response,HttpServletRequest request) {
try {
int width=200;
int height=69;
BufferedImage verifyImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//生成對應寬高的初始圖片
String randomText = VerifyCode.drawRandomText(width,height,verifyImg);
//單獨的一個類方法,出於代碼複用考慮,進行了封裝。
//功能是生成驗證碼字符並加上噪點,干擾線,返回值爲驗證碼字符
request.getSession().setAttribute("verifyCode", randomText);
response.setContentType("image/png");//必須設置響應內容類型爲圖片,否則前臺不識別
OutputStream os = response.getOutputStream(); //獲取文件輸出流
ImageIO.write(verifyImg,"png",os);//輸出圖片流
os.flush();
os.close();//關閉流
} catch (IOException e) {
e.printStackTrace();
}
}
}
com.example.verificationcode.VerifyCode
package com.example.verificationcode;
/*對圖片進行處理的類和方法*/
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class VerifyCode {
public static String drawRandomText(int width, int height, BufferedImage verifyImg) {
Graphics2D graphics = (Graphics2D)verifyImg.getGraphics();
graphics.setColor(Color.WHITE);//設置畫筆顏色-驗證碼背景色
graphics.fillRect(0, 0, width, height);//填充背景
graphics.setFont(new Font("微軟雅黑", Font.BOLD, 40));
//數字和字母的組合
String baseNumLetter= "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
StringBuffer sBuffer = new StringBuffer();
int x = 10; //旋轉原點的 x 座標
String ch = "";
Random random = new Random();
for(int i = 0;i < 4;i++){
graphics.setColor(getRandomColor());
//設置字體旋轉角度
int degree = random.nextInt() % 45; //角度小於30度
int dot = random.nextInt(baseNumLetter.length());
ch = baseNumLetter.charAt(dot) + "";
sBuffer.append(ch);
//正向旋轉
graphics.rotate(degree * Math.PI / 180, x, 45);
graphics.drawString(ch, x, 45);
//反向旋轉,不反向旋轉的話字符會跑出畫布
graphics.rotate(-degree * Math.PI / 180, x, 45);
x += 48;
}
//畫干擾線
for (int i = 0; i <6; i++) {
// 設置隨機顏色
graphics.setColor(getRandomColor());
// 隨機畫線
graphics.drawLine(random.nextInt(width), random.nextInt(height),
random.nextInt(width), random.nextInt(height));
}
//添加噪點
for(int i=0;i<30;i++){
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
graphics.setColor(getRandomColor());
graphics.fillRect(x1, y1, 2,2);
}
return sBuffer.toString();
}
/**
* 隨機取色
*/
private static Color getRandomColor() {
Random ran = new Random();
Color color = new Color(ran.nextInt(256),
ran.nextInt(256), ran.nextInt(256));
return color;
}
}
測試結果
如果註釋掉
將會出現該情況
源碼地址:https://github.com/weijieqiu/iStudy