网站建设方案书 百度文库,教育云平台网站建设,做门头上那个网站申报,全国企业信息查询网点击下载《SpringBootRedis如何实现用户输入错误密码后限制登录#xff08;含源码#xff09;》
1. 引言
在当今的网络环境中#xff0c;保障用户账户的安全性是非常重要的。为了防止暴力破解和恶意攻击#xff0c;我们需要在用户尝试登录失败一定次数后限制其登录。这不…点击下载《SpringBootRedis如何实现用户输入错误密码后限制登录含源码》
1. 引言
在当今的网络环境中保障用户账户的安全性是非常重要的。为了防止暴力破解和恶意攻击我们需要在用户尝试登录失败一定次数后限制其登录。这不仅可以保护用户的账户安全还可以减轻服务器的压力。在本文中我们将使用Spring Boot和Redis来实现这个功能。 2. 系统设计
首先我们需要一个系统来跟踪用户的登录尝试。由于我们的系统需要处理大量的用户请求所以我们需要一个快速、可靠的存储系统来存储这些信息。Redis是一个内存中的数据结构存储它可以提供高速的读写操作非常适合我们的需求。
我们的系统将使用Redis的哈希数据类型来存储每个用户的登录尝试信息。哈希中的每个字段表示一个尝试的计数器而字段的值则表示尝试的次数。当用户尝试登录时我们会增加相应的计数器。如果计数器的值超过了设定的限制我们将阻止用户在设定的时间内再次尝试登录。
3. 实现细节
3.1 添加依赖
首先我们需要在Spring Boot项目中添加Redis的依赖。在pom.xml文件中添加以下依赖
dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-redis/artifactId
/dependency3.2 配置连接信息
然后我们需要在application.properties文件中配置Redis的连接信息
spring.redis.hostyour_redis_host
spring.redis.portyour_redis_port3.3 连接Redis
接下来我们需要创建一个RedisTemplate来连接Redis
Bean
public RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) { RedisTemplateString, Object template new RedisTemplate(); template.setConnectionFactory(factory); return template;
}3.4 创建RedisService
然后我们需要创建一个RedisService来处理与Redis的交互
Service
public class RedisService { Autowired private StringRedisTemplate redisTemplate; public void incrementKey(String key) { redisTemplate.opsForValue().increment(key, 1); } public long getKey(String key) { return redisTemplate.opsForValue().get(key); }
}3.5 创建UserController
现在我们可以在UserController中实现登录功能
RestController
public class UserController { Autowired private UserService userService; Autowired private RedisService redisService; Autowired private AuthenticationManager authenticationManager; PostMapping(/login) public ResponseEntity? login(RequestBody User user) { // 检查用户名和密码是否正确 if (userService.checkUser(user.getUsername(), user.getPassword())) { // 检查用户是否被阻止登录 long attempts redisService.getKey(login_attempts_ user.getUsername()); if (attempts 3) { // 用户尝试登录次数超过3次阻止用户登录一定时间例如30分钟 redisService.incrementKey(login_attempts_ user.getUsername()); // 增加尝试次数计数器在一定时间后过期 return ResponseEntity.status(HttpStatus.FORBIDDEN).body(You have exceeded the maximum login attempts. Please wait for 30 minutes and try again.); } else { // 用户没有被阻止登录允许他们登录并重置尝试次数计数器在一定时间后过期 redisService.set(login_attempts_ user.getUsername(), 0, 30, TimeUnit.MINUTES); // 设置计数器在30分钟后过期例如30分钟 userService.loginUser(user); // 允许用户登录并重置尝试次数计数器在一定时间后过期 return ResponseEntity.status(HttpStatus.OK).body(Login successful.); } } else { // 如果用户名和密码不正确增加尝试次数计数器并返回错误信息给用户。如果用户已经被阻止登录则返回错误信息给用户。否则允许用户再次尝试登录。重复这个过程直到用户成功登录或被阻止登录。 redisService.incrementKey(login_attempts_ user.getUsername()); // 增加尝试次数计数器 return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Invalid username or password.); } }
}4. 测试与验证
为了验证我们的系统是否正常工作我们需要编写一些测试用例。我们可以使用JUnit和MockMvc进行测试。以下是一个简单的测试用例用于测试登录功能
RunWith(SpringRunner.class)
SpringBootTest(webEnvironment SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerTest { Autowired private WebApplicationContext context; Autowired private UserService userService; Autowired private RedisService redisService; private MockMvc mockMvc; Before public void setup() { mockMvc MockMvcBuilders.webAppContextSetup(context).build(); } Test public void testLoginSuccess() throws Exception { mockMvc.perform(post(/login) .contentType(MediaType.APPLICATION_JSON) .content({\username\:\test\,\password\:\test\})) .andExpect(status().isOk()); long attempts redisService.getKey(login_attempts_ test); assertEquals(0, attempts); // 确保尝试次数计数器被重置为0 } Test public void testLoginFailureAndBlock() throws Exception { mockMvc.perform(post(/login) .contentType(MediaType.APPLICATION_JSON) .content({\username\:\test\,\password\:\test\})) .andExpect(status().isOk()); mockMvc.perform(post(/login) .contentType(MediaType.APPLICATION_JSON) .content({\username\:\test\,\password\:\test\})) .andExpect(status().isForbidden()); // 用户被阻止登录返回403错误 long attempts redisService.getKey(login_attempts_ test); assertEquals(3, attempts); // 确保尝试次数计数器被设置为3表示用户已被阻止登录 }
}5. 总结
总结一下SpringBoot Redis实现用户连续输入多次密码错误后限制登录的原理和作用如下 原理 当用户尝试登录时系统会检查Redis中是否存在该用户的登录尝试记录。 如果记录存在则判断用户连续输入密码错误的次数并根据设定的阈值决定是否限制用户登录。 如果达到阈值则在Redis中设置一个标志位表示该用户已被限制登录。 在限制期间内系统将拒绝该用户的登录请求。 限制时间过后系统将自动解除对该用户的限制。 作用 增强系统安全性通过限制用户连续输入密码错误的次数可以防止暴力破解攻击保护用户账户安全。 提高用户体验对于因连续输入密码错误而被限制登录的用户系统会给出相应的提示信息引导用户重置密码或找回账号避免用户因多次尝试而产生挫败感。 防止恶意行为对于恶意尝试登录的用户系统可以实施限制登录的策略有效地遏制了其不良行为。 优化服务器性能通过Redis实现用户登录限制可以减轻服务器的压力提高系统的性能和稳定性。
点击下载《SpringBootRedis如何实现用户输入错误密码后限制登录含源码》