一、導(dǎo)入糊涂 huttol依賴
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.1</version>
</dependency>
二、編寫驗證碼控制器(controller)
ioc容器當(dāng)中獲取redis注入對象
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 返回驗證碼
* 思路: 把驗證碼存入redis
* 登陸時候 在進行獲取出來 進行相應(yīng)的判斷
* 接收的形式 key --- value
*
* @param response 返回出去的流
* @param codeKey 接收驗證碼的key # 這里的key結(jié)合前端隨機數(shù)生成4位數(shù)的key 自行百度即可
* @throws IOException
*/
@GetMapping("captcha")
public void capthca (String codeKey,HttpServletResponse res){
// 定義驗證碼基本樣式 寬度--高度--驗證碼數(shù)量--干擾線粗細(xì)
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(100, 38, 4, 2);
// 獲取驗證碼
string code = captcha.getCode();
// 進行存入到redis當(dāng)中
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
opsForValue.set(codeKey,code);
// 設(shè)置 緩存時間 60秒
opsForValue.getOperations().expire(codeKey, 60, TimeUnit.SECONDS);
// 返回流 到頁面
captcha.write(response.getOutputStream());
}
三、進行測試 如果 測試當(dāng)中 404啥的 配置一下攔截即可
codeKey為存儲的key
http://localhost:8080/login/captcha?codeKey=adasdw

image.png
呼累死了 純手打 還是舒舒服服的
91G~EXLK``{VQC4XP(`W[`E.gif

91G~EXLK``{VQC4XP(`W[`E.gif
你的壓力來源于無法自律,只是假裝努力,現(xiàn)狀跟不上內(nèi)心欲望,所以你焦慮又恐慌?!獥畈灰?/p>