@RequestLimit(count = 60, time = 60) @RequestMapping(value = "/captcha", method = {RequestMethod.GET}) public ResponseEntity captcha(HttpServletRequest request, HttpServletResponse response) throws Exception { log.info("### 刷新驗證碼"); SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5); String verCode = specCaptcha.text().toLowerCase(); String key = UUID.randomUUID().toString(); // 存入redis並設置過時時間爲30分鐘 redisUtil.set(key, verCode, 30L, TimeUnit.MINUTES); // 將key和base64返回給前端 CaptchaVO captchaVO = new CaptchaVO(); captchaVO.setCaptchaImage(specCaptcha.toBase64()); captchaVO.setCaptchaKey(key); log.info("### 刷新驗證碼:{}, {}", key, verCode); return ResponseEntity.ok().body(captchaVO); }
package com.**.**.common.utils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; import java.io.Serializable; import java.util.Set; import java.util.concurrent.TimeUnit; @Slf4j @Component public class RedisUtil { @Autowired private RedisTemplate redisTemplate; public void remove(String... keys) { for (String key : keys) { remove(key); } } public void removePattern(String pattern) { Set<Serializable> keys = this.redisTemplate.keys(pattern); if (keys.size() > 0) { this.redisTemplate.delete(keys); } } public void remove(String key) { if (exists(key)) { this.redisTemplate.delete(key); } } public boolean exists(String key) { return this.redisTemplate.hasKey(key).booleanValue(); } public <T> T get(String key) { T result = null; ValueOperations<Serializable, T> operations = this.redisTemplate.opsForValue(); result = operations.get(key); return result; } public <T> boolean set(String key, T value) { boolean result = false; ValueOperations<Serializable, T> operations = this.redisTemplate.opsForValue(); operations.set(key, value); result = true; return result; } /** * 只適用於key對應的值再也不更新的問題,若是遇到包含生命週期的值須要更新的情景就不適用了,由於set 方法會丟失該key的生存時間,變成永久有效的 * @param key * @param value * @param expireTime 單位秒 * @param <T> * @return */ public <T> boolean set(String key, T value, Long expireTime) { boolean result = false; ValueOperations<Serializable, T> operations = this.redisTemplate.opsForValue(); operations.set(key, value); this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; return result; } /** * * 只適用於key對應的值再也不更新的問題,若是遇到包含生命週期的值須要更新的情景就不適用了,由於set 方法會丟失該key的生存時間,變成永久有效的 * @param key * @param value * @param expireTime * @param unit * @param <T> * @return */ public <T> boolean set(String key, T value, Long expireTime, final TimeUnit unit) { boolean result = false; ValueOperations<Serializable, T> operations = this.redisTemplate.opsForValue(); operations.set(key, value); this.redisTemplate.expire(key, expireTime, unit); result = true; return result; } public <T> T getHash(String hashName, String key) { T result = null; HashOperations<Serializable, Serializable, T> operations = this.redisTemplate.opsForHash(); result = operations.get(hashName, key); return result; } public <T> void setHash(String hashName, String key, T value) { redisTemplate.opsForHash().put(hashName, key, value); } /** * 只適用於key對應的值再也不更新的問題,若是遇到包含生命週期的值須要更新的情景就不適用了,由於set 方法會丟失該key的生存時間,變成永久有效的 * @param key * @param expireTime */ public Boolean expire(String key, Long expireTime) { return this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); } /** * 設置分佈式鎖 * @param key * @param value * @return */ public synchronized Boolean setNX(final String key, final String value) throws Exception{ Object obj = null; try { obj = redisTemplate.execute((RedisCallback<Object>) connection -> { StringRedisSerializer serializer = new StringRedisSerializer(); Boolean success = connection.setNX(serializer.serialize(key), serializer.serialize(value)); connection.close(); return success; }); } catch (Exception e) { log.error("setNX com.strawhat.redis error, key : {} - {}", key,e); throw e; } return obj != null ? (Boolean) obj : false; } /** * 設置分佈式鎖,超時間單位秒 * @param key * @param value * @return */ public synchronized Boolean setNX(final String key, final String value,long timeOut) throws Exception { boolean b = this.setNX(key,value); redisTemplate.expire(key,timeOut,TimeUnit.SECONDS); return b; } /** * 刪除鎖 * @param key * @return */ public void unlock(final String key) { redisTemplate.delete(key); } }