springboot驗證碼生成服務(數字+字母)
阿新 • • 發佈:2019-01-26
springboot生成驗證碼服務(數字+字母)
1.專案準備
springboot redis
說明:個人開發原因,我將生成的驗證碼存放在redis中,所以使用到redis
2.服務開發:
1). 驗證碼生成工具(5位隨機數字+字母(可修改)):
```
private static char mapTable[] = {
'1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
public static Map<String, Object> getImageCode(int width, int height, OutputStream os) {
Map<String,Object> returnMap = new HashMap<String, Object>();
// JedisConfig jedisConfig = new JedisConfig();
if (width <= 0) width = 80;
if (height <= 0) height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 168; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
String strEnsure = "";
for (int i = 0; i < 5; ++i) {
strEnsure += mapTable[(int) (mapTable.length * Math.random())];
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
//直接生成
String str = strEnsure.substring(i, i + 1);
g.drawString(str, 13 * i + 6, 16);
}
g.dispose();
returnMap.put("image",image);
returnMap.put("strEnsure",strEnsure);
return returnMap;
}
static Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255) fc = 255;
if (bc > 255) bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
```
2).編寫控制類(獲取驗證碼):
```
@RequestMapping(value = "/getImage")
public String imagecode(HttpServletRequest request, HttpServletResponse response) throws Exception {
String codeKey = request.getParameter("redisKey");
OutputStream os = response.getOutputStream();
//這裡修改傳入的數值,可以修改產生的驗證碼的大小
Map<String,Object> map = ImageCode.getImageCode(80, 20, os);
String strEnsure = (String)map.get("strEnsure");
jedisConfig.set(codeKey,strEnsure);
jedisConfig.setExpire(codeKey,10);
try {
ImageIO.write((BufferedImage) map.get("image"), "JPEG", os);
} catch (IOException e) {
return "";
}
return null;
}
```
3).呼叫方法
http://127.0.0.1:8088/getImage
response: springboot生成驗證碼服務(數字+字母)
1.專案準備
springboot redis
說明:個人開發原因,我將生成的驗證碼存放在redis中,所以使用到redis
2.服務開發:
1). 驗證碼生成工具(5位隨機數字+字母(可修改)):
```
private static char mapTable[] = {
'1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
public static Map<String, Object> getImageCode(int width, int height, OutputStream os) {
Map<String,Object> returnMap = new HashMap<String, Object>();
// JedisConfig jedisConfig = new JedisConfig();
if (width <= 0) width = 80;
if (height <= 0) height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 168; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
String strEnsure = "";
for (int i = 0; i < 5; ++i) {
strEnsure += mapTable[(int) (mapTable.length * Math.random())];
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
//直接生成
String str = strEnsure.substring(i, i + 1);
g.drawString(str, 13 * i + 6, 16);
}
g.dispose();
returnMap.put("image",image);
returnMap.put("strEnsure",strEnsure);
return returnMap;
}
static Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255) fc = 255;
if (bc > 255) bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
```
2).編寫控制類(獲取驗證碼):
```
@RequestMapping(value = "/getImage")
public String imagecode(HttpServletRequest request, HttpServletResponse response) throws Exception {
String codeKey = request.getParameter("redisKey");
OutputStream os = response.getOutputStream();
//這裡修改傳入的數值,可以修改產生的驗證碼的大小
Map<String,Object> map = ImageCode.getImageCode(80, 20, os);
String strEnsure = (String)map.get("strEnsure");
jedisConfig.set(codeKey,strEnsure);
jedisConfig.setExpire(codeKey,10);
try {
ImageIO.write((BufferedImage) map.get("image"), "JPEG", os);
} catch (IOException e) {
return "";
}
return null;
}
```
3).呼叫方法,生成效果
http://127.0.0.1:8088/getImage
response:
1.專案準備
springboot redis
說明:個人開發原因,我將生成的驗證碼存放在redis中,所以使用到redis
2.服務開發:
1). 驗證碼生成工具(5位隨機數字+字母(可修改)):
```
private static char mapTable[] = {
'1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
public static Map<String, Object> getImageCode(int width, int height, OutputStream os) {
Map<String,Object> returnMap = new HashMap<String, Object>();
// JedisConfig jedisConfig = new JedisConfig();
if (width <= 0) width = 80;
if (height <= 0) height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 168; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
String strEnsure = "";
for (int i = 0; i < 5; ++i) {
strEnsure += mapTable[(int) (mapTable.length * Math.random())];
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
//直接生成
String str = strEnsure.substring(i, i + 1);
g.drawString(str, 13 * i + 6, 16);
}
g.dispose();
returnMap.put("image",image);
returnMap.put("strEnsure",strEnsure);
return returnMap;
}
static Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255) fc = 255;
if (bc > 255) bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
```
2).編寫控制類(獲取驗證碼):
```
@RequestMapping(value = "/getImage")
public String imagecode(HttpServletRequest request, HttpServletResponse response) throws Exception {
String codeKey = request.getParameter("redisKey");
OutputStream os = response.getOutputStream();
//這裡修改傳入的數值,可以修改產生的驗證碼的大小
Map<String,Object> map = ImageCode.getImageCode(80, 20, os);
String strEnsure = (String)map.get("strEnsure");
jedisConfig.set(codeKey,strEnsure);
jedisConfig.setExpire(codeKey,10);
try {
ImageIO.write((BufferedImage) map.get("image"), "JPEG", os);
} catch (IOException e) {
return "";
}
return null;
}
```
3).呼叫方法
http://127.0.0.1:8088/getImage
response: springboot生成驗證碼服務(數字+字母)
1.專案準備
springboot redis
說明:個人開發原因,我將生成的驗證碼存放在redis中,所以使用到redis
2.服務開發:
1). 驗證碼生成工具(5位隨機數字+字母(可修改)):
```
private static char mapTable[] = {
'1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
public static Map<String, Object> getImageCode(int width, int height, OutputStream os) {
Map<String,Object> returnMap = new HashMap<String, Object>();
// JedisConfig jedisConfig = new JedisConfig();
if (width <= 0) width = 80;
if (height <= 0) height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 168; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
String strEnsure = "";
for (int i = 0; i < 5; ++i) {
strEnsure += mapTable[(int) (mapTable.length * Math.random())];
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
//直接生成
String str = strEnsure.substring(i, i + 1);
g.drawString(str, 13 * i + 6, 16);
}
g.dispose();
returnMap.put("image",image);
returnMap.put("strEnsure",strEnsure);
return returnMap;
}
static Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255) fc = 255;
if (bc > 255) bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
```
2).編寫控制類(獲取驗證碼):
```
@RequestMapping(value = "/getImage")
public String imagecode(HttpServletRequest request, HttpServletResponse response) throws Exception {
String codeKey = request.getParameter("redisKey");
OutputStream os = response.getOutputStream();
//這裡修改傳入的數值,可以修改產生的驗證碼的大小
Map<String,Object> map = ImageCode.getImageCode(80, 20, os);
String strEnsure = (String)map.get("strEnsure");
jedisConfig.set(codeKey,strEnsure);
jedisConfig.setExpire(codeKey,10);
try {
ImageIO.write((BufferedImage) map.get("image"), "JPEG", os);
} catch (IOException e) {
return "";
}
return null;
}
```
3).呼叫方法,生成效果
http://127.0.0.1:8088/getImage
response: