redisweb.rar
TestController
package k.controller;
import k.utils.AjaxResult;
import k.utils.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
private RedisService redisService;
@RequestMapping("/verify")
public String verifyMobile() {
return "test/verify";
}
@RequestMapping("/doVerify")
@ResponseBody
public AjaxResult doVerify(HttpServletRequest request) {
AjaxResult ajaxResult = AjaxResult.success();
String mobile = request.getParameter("mobile");
String yzm = request.getParameter("yzm");
String gqKey = mobile;
if (redisService.existsKey(gqKey)) {
String keyVal = redisService.getKeyVal(gqKey);
Integer old = Integer.parseInt(keyVal);
if (old >= 3) {
ajaxResult.setCode(0);
ajaxResult.setMsg("20s验证3次");
return ajaxResult;
}
redisService.setKeyVal(gqKey, (++old).toString());
} else {
redisService.setKeyVal(gqKey, "1");
redisService.expireKey(gqKey, 20, TimeUnit.SECONDS);
}
String yzm_key = "yzm" + mobile;
if (redisService.existsKey(yzm_key)) {
String keyVal = redisService.getKeyVal(yzm_key);
System.out.println("keyVal==" + keyVal + " yzm==" + yzm);
if (keyVal.equals(yzm)) {
ajaxResult.setMsg("验证成功");
} else {
ajaxResult.setMsg("验证失败");
}
} else {
redisService.setKeyVal(yzm_key, "1234");
redisService.expireKey(yzm_key, 20, TimeUnit.SECONDS);
}
return ajaxResult;
}
@RequestMapping("/doLogin")
@ResponseBody
public AjaxResult doLogin(HttpServletRequest request) {
AjaxResult ajaxResult = AjaxResult.success();
String mobile = request.getParameter("mobile");
String pwd = request.getParameter("pwd");
String xianzhiKey = "xz" + mobile;
if (redisService.existsKey(xianzhiKey)) {
ajaxResult.setCode(0);
ajaxResult.setMsg("登录错误次数太多,请稍后尝试");
return ajaxResult;
}
String timeKey = "time" + mobile;
if ("123456".equals(pwd)) {
ajaxResult.setCode(1);
ajaxResult.setMsg("登录成功");
redisService.setKeyVal(timeKey, "0");
redisService.expireKey(timeKey, 10, TimeUnit.SECONDS);
return ajaxResult;
} else {
ajaxResult.setCode(0);
ajaxResult.setMsg("登录失败");
if (redisService.existsKey(timeKey)) {
String keyVal = redisService.getKeyVal(timeKey);
Integer old = Integer.parseInt(keyVal);
if (old >= 5) {
ajaxResult.setMsg("登录错误5次");
redisService.setKeyVal(xianzhiKey, "1");
redisService.expireKey(xianzhiKey, 20, TimeUnit.SECONDS);
return ajaxResult;
}
redisService.setKeyVal(timeKey, (++old).toString());
} else {
redisService.setKeyVal(timeKey, "1");
redisService.expireKey(timeKey, 10, TimeUnit.SECONDS);
}
return ajaxResult;
}
}
}
RedisService
package k.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
/**
* 默认过期时长,单位:秒
*/
public static final long DEFAULT_EXPIRE = 60 * 60 * 24;
/**
* 不设置过期时长
*/
public static final long NOT_EXPIRE = -1;
public boolean existsKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 重名名key,如果newKey已经存在,则newKey的原值被覆盖
*
* @param oldKey
* @param newKey
*/
public void renameKey(String oldKey, String newKey) {
redisTemplate.rename(oldKey, newKey);
}
/**
* newKey不存在时才重命名
*
* @param oldKey
* @param newKey
* @return 修改成功返回true
*/
public boolean renameKeyNotExist(String oldKey, String newKey) {
return redisTemplate.renameIfAbsent(oldKey, newKey);
}
/**
* 删除key
*
* @param key
*/
public void deleteKey(String key) {
redisTemplate.delete(key);
}
/**
* 删除多个key
*
* @param keys
*/
public void deleteKey(String... keys) {
Set kSet = Stream.of(keys).map(k -> k).collect(Collectors.toSet());
redisTemplate.delete(kSet);
}
/**
* 删除Key的集合
*
* @param keys
*/
public void deleteKey(Collection keys) {
Set kSet = keys.stream().map(k -> k).collect(Collectors.toSet());
redisTemplate.delete(kSet);
}
/**
* 设置key的生命周期
*
* @param key
* @param time
* @param timeUnit
*/
public void expireKey(String key, long time, TimeUnit timeUnit) {
redisTemplate.expire(key, time, timeUnit);
}
/**
* 指定key在指定的日期过期
*
* @param key
* @param date
*/
public void expireKeyAt(String key, Date date) {
redisTemplate.expireAt(key, date);
}
/**
* 查询key的生命周期
*
* @param key
* @param timeUnit
* @return
*/
public long getKeyExpire(String key, TimeUnit timeUnit) {
return redisTemplate.getExpire(key, timeUnit);
}
/**
* 将key设置为永久有效
*
* @param key
*/
public void persistKey(String key) {
redisTemplate.persist(key);
}
public String getKeyVal(String key) {
return redisTemplate.opsForValue().get(key);
}
public void setKeyVal(String key, String val) {
redisTemplate.opsForValue().set(key, val);
}
}