现象结果示例截图
swagger示例效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/7ad4cb489e624d50b81018fb514e6737.png)
控制台示例效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/38312afb229a432fb29279c6cabb7f4d.png)
后端代码示例
package com.java.javamethod.service.impl;
import com.java.javamethod.dao.UserMapper;
import com.java.javamethod.domain.Result;
import com.java.javamethod.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@Service("loginService")
@Slf4j
public class LoginServiceImpl implements LoginService {
@Resource
private UserMapper userMapper;
@Resource
private RedisTemplate<String, Integer> redisTemplate;
private static final String FAIL_COUNTER = "user_login_fail_counter";
private static final String FAIL_LOCK = "user_login_fail_lock";
public void setCheckFailCounter(String username) {
String key = String.join(":", FAIL_COUNTER, username);
Integer count = redisTemplate.opsForValue().get(key);
redisTemplate.opsForValue().increment(key);
if (count == null) {
redisTemplate.expire(key, 10, TimeUnit.MINUTES);
}
if (count!=null&&count.intValue() > 5) {
lock(username);
}
}
public void deleteLoginFailCounter(String username) {
redisTemplate.delete(String.join(":", FAIL_COUNTER, username));
}
public void lock(String username) {
String key = String.join(":", FAIL_LOCK, username);
redisTemplate.opsForValue().set(key, 1, 30, TimeUnit.MINUTES);
}
public boolean isLock(String username) {
return redisTemplate.hasKey(String.join(":", FAIL_LOCK, username));
}
public long unlockTime(String username) {
String key = String.join(":", FAIL_LOCK, username);
return redisTemplate.opsForValue().getOperations().getExpire(key, TimeUnit.MINUTES);
}
public Result loginCheck(String username, String password) {
User user = userMapper.queryByName(username);
Integer passwordStr = redisTemplate.opsForValue().get(username);
if (password.equals(passwordStr)) {
return Result.success();
} else {
if (user != null) {
if (user.getPassword().equals(password)) {
redisTemplate.opsForValue().set(username, Integer.valueOf(password));
return Result.success().setErrmsg("用户名密码正确登录成功");
} else {
log.error("===========用户名密码对应错误{}==========", "密码错误");
return Result.failed();
}
} else {
log.error("========用户名密码对应错误{}=========", "用户名错误");
return Result.failed();
}
}
}
@Override
public Result login(String username, String password) {
boolean lock = isLock(username);
if (lock) {
long outtime = unlockTime(username);
return new Result().setSuccess(false).setErrmsg("账号已经被锁定,请于"+outtime+"分钟后解锁");
}
Result result = loginCheck(username, password);
if (!result.isSuccess()) {
setCheckFailCounter(username);
return new Result();
}
deleteLoginFailCounter(username);
return new Result();
}
}