您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java怎么實(shí)現(xiàn)登錄驗(yàn)證碼保存到redis”,在日常操作中,相信很多人在Java怎么實(shí)現(xiàn)登錄驗(yàn)證碼保存到redis問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java怎么實(shí)現(xiàn)登錄驗(yàn)證碼保存到redis”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
package com.yishang.yspay.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.awt.*;
import java.util.Random;
/**
* 驗(yàn)證碼生成類
*/
public class RandomValidateCode {
public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY";//放到session中的key
//private String randString = "0123456789";//隨機(jī)產(chǎn)生只有數(shù)字的字符串 private String
//private String randString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//隨機(jī)產(chǎn)生只有字母的字符串
private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//隨機(jī)產(chǎn)生數(shù)字與字母組合的字符串
private int width = 95;// 圖片寬
private int height = 25;// 圖片高
private int lineSize = 10;// 干擾線數(shù)量
private int stringNum = 4;// 隨機(jī)產(chǎn)生字符數(shù)量
/**
* redis工具
*/
@Autowired
private RedisTemplate<String, String> redisTemplate;
private Random random = new Random();
/*
* 獲得字體
*/
private Font getFont() {
return new Font("Fixedsys", Font.CENTER_BASELINE, 24);
}
/*
* 獲得顏色
*/
public Color getRandColor(int fc, int bc) {
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc - 16);
int g = fc + random.nextInt(bc - fc - 14);
int b = fc + random.nextInt(bc - fc - 18);
return new Color(r, g, b);
}
/*
* 繪制字符串
*/
public String drowString(Graphics g, String randomString, int i) {
g.setFont(getFont());
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random
.nextInt(121)));
String rand = String.valueOf(getRandomString(random.nextInt(randString
.length())));
randomString += rand;
g.translate(random.nextInt(3), random.nextInt(3));
g.drawString(rand, 13 * i, 16);
return randomString;
}
/*
* 繪制干擾線
*/
public void drowLine(Graphics g) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.drawLine(x, y, x + xl, y + yl);
}
/*
* 獲取隨機(jī)的字符
*/
public String getRandomString(int num) {
return String.valueOf(randString.charAt(num));
}
}
@RequestMapping(value = "/getVerify")
public void getVerify(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("image/jpeg");//設(shè)置相應(yīng)類型,告訴瀏覽器輸出的內(nèi)容為圖片
response.setHeader("Pragma", "No-cache");//設(shè)置響應(yīng)頭信息,告訴瀏覽器不要緩存此內(nèi)容
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
RandomValidateCode randomValidateCode = new RandomValidateCode();
// BufferedImage類是具有緩沖區(qū)的Image類,Image類是用于描述圖像信息的類
BufferedImage image = new BufferedImage(95, 25, BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();// 產(chǎn)生Image對(duì)象的Graphics對(duì)象,改對(duì)象可以在圖像上進(jìn)行各種繪制操作
g.fillRect(0, 0, 95, 25);
g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
g.setColor(randomValidateCode.getRandColor(110, 133));
// 繪制干擾線
for (int i = 0; i <= 40; i++) {
randomValidateCode.drowLine(g);
}
// 繪制隨機(jī)字符
String randomString = "";
for (int i = 1; i <= 4; i++) {
randomString = randomValidateCode.drowString(g, randomString, i);
}
// 生產(chǎn)驗(yàn)證碼字符串并保存到 Redis 中,ip-rightCode,有效期為 1 小時(shí)
String ip = request.getRemoteAddr();
logger.info("ip:" + ip + ",rightCode = " + randomString);
redisTemplate.opsForValue().set(ip, randomString, 20, TimeUnit.SECONDS);
g.dispose();
try {
// 將內(nèi)存中的圖片通過(guò)流動(dòng)形式輸出到客戶端
ImageIO.write(image, "JPEG", response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
到此,關(guān)于“Java怎么實(shí)現(xiàn)登錄驗(yàn)證碼保存到redis”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。