後端生成圖形驗證碼,並進行base64編碼生成字符串傳至前端,由前端解碼顯示圖形驗證碼,代碼如下:
後端-獲取圖形驗證碼base64編碼字符串接口:
/**
* @param response
* @return map
* @throws Exception 獲取圖形驗證碼接口
*/
@RequestMapping("/getVerifyCode")
@ResponseBody
public Map getVerifyCode(HttpServletResponse response) throws Exception {
Map map = new HashMap();
//第一個參數是生成的驗證碼,第二個參數是生成的圖片
Object[] objs = VerifyUtil.createImage();
//將圖片輸出給瀏覽器
BufferedImage image = (BufferedImage) objs[1];
String value = objs[0].toString().toLowerCase();
String key = UUID.randomUUID().toString();
redisUtils.set(key, value, 2, MINUTES);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 使用ServletOutputStream流可直接輸出圖片
// ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(image, "png", outputStream);
Base64 base64 = new Base64();
String pic = new String(base64.encode(outputStream.toByteArray()));
map.put("codeKey", key);
map.put("codePic", pic);
return map;
}
後端-生成圖形驗證碼工具類
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* @param
* @desc 圖形驗證碼生成
*
*/
public class VerifyUtil {
// 驗證碼字符集
private static final char[] chars = {
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
// 字符數量
private static final int SIZE = 4;
// 干擾線數量
private static final int LINES = 4;
// 寬度
private static final int WIDTH = 83;
// 高度
private static final int HEIGHT = 40;
// 字體大小
private static final int FONT_SIZE = 30;
private static int[] w = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
/**
* 生成隨機驗證碼及圖片
* Object[0]:驗證碼字符串;
* Object[1]:驗證碼圖片。
*/
public static Object[] createImage() {
StringBuffer sb = new StringBuffer();
// 1.創建空白圖片
BufferedImage image = new BufferedImage(
WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 2.獲取圖片畫筆
Graphics graphic = image.getGraphics();
// 3.設置畫筆顏色
graphic.setColor(Color.LIGHT_GRAY);
// 4.繪製矩形背景
graphic.fillRect(0, 0, WIDTH, HEIGHT);
// 5.畫隨機字符
Random ran = new Random();
for (int i = 0; i <SIZE; i++) {
// 取隨機字符索引
int n = ran.nextInt(chars.length);
// 設置隨機顏色
graphic.setColor(getRandomColor());
// 設置字體大小
graphic.setFont(new Font(
null, Font.BOLD + Font.ITALIC, FONT_SIZE));
// 畫字符
graphic.drawString(
chars[n] + "", i * WIDTH / SIZE, HEIGHT*2/3);
// 記錄字符
sb.append(chars[n]);
}
// 6.畫干擾線
for (int i = 0; i < LINES; i++) {
// 設置隨機顏色
graphic.setColor(getRandomColor());
// 隨機畫線
graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
}
// 7.返回驗證碼和圖片
return new Object[]{sb.toString(), image};
}
/**
* 隨機取色
*/
public static Color getRandomColor() {
Random ran = new Random();
Color color = new Color(ran.nextInt(256),
ran.nextInt(256), ran.nextInt(256));
return color;
}
}
前端-訪問獲取圖形驗證碼base64編碼字符串接口,並解碼顯示圖形驗證碼
<script src="/wjxcx/jquery.min.js"></script>
<img id="randomImage" title="點擊更換" id="imgcode" οnclick="javascript:refreshimg();" src="" />
<script type="text/javascript">
$(function(){
refreshimg();
})
function refreshimg() {
$.ajax({
type : "post",
dataType : "json",
url : "/wjxcx/common/getKeyCode",
data : {},
success : function(data){
$('#randomImage').attr('src', 'data:image/png;base64,'+data.codePic);
},
error : function(){
}
});
}
</script>
借鑑文章:
https://blog.csdn.net/fengbonu/article/details/78813094
https://blog.csdn.net/u010407050/article/details/52046820
可使用以下網站測試:
在線加密解密(採用Crypto-JS實現):
http://tool.oschina.net/encrypt?type=4