文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

SpringBoot整合Redis实现登录失败锁定功能(实例详解)

代码魔法师

代码魔法师

2024-04-02 17:21

关注

这篇文章将为大家详细讲解有关SpringBoot整合Redis实现登录失败锁定功能(实例详解),小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

SpringBoot 整合 Redis 实现登录失败锁定功能

简介

在用户登录过程中,防止暴力破解行为是至关重要的。使用 Redis 可以轻松实现登录失败锁定功能,在一定时间内限制用户多次尝试登录失败。

实现步骤

1. 依赖管理

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. 配置 Redis 属性

spring.redis.host=localhost
spring.redis.port=6379

3. 创建辅助类

public class LoginAttemptService {

    private static final String LOCK_KEY = "login-attempt:";

    private RedisTemplate<String, Integer> redisTemplate;

    public LoginAttemptService(RedisTemplate<String, Integer> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void incrementLoginAttempt(String username) {
        String key = LOCK_KEY + username;
        redisTemplate.opsForValue().increment(key, 1);
        redisTemplate.expire(key, 60 * 5, TimeUnit.SECONDS);
    }

    public int getLoginAttempt(String username) {
        String key = LOCK_KEY + username;
        Integer attempts = redisTemplate.opsForValue().get(key);
        return attempts == null ? 0 : attempts;
    }

    public void unlock(String username) {
        String key = LOCK_KEY + username;
        redisTemplate.delete(key);
    }
}

4. 集成到 Spring Security

AuthenticationFailureHandler 中实现登录失败锁定逻辑:

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    String username = request.getParameter("username");
    int attempts = loginAttemptService.getLoginAttempt(username);
    if (attempts >= MAX_ATTEMPTS) {
        loginAttemptService.unlock(username);
        response.setStatus(HttpStatus.FORBIDDEN.value());
        return;
    }
    loginAttemptService.incrementLoginAttempt(username);
}

5. 注入服务

@Configuration
public class RedisConfig {

    @Bean
    public LoginAttemptService loginAttemptService(RedisTemplate<String, Integer> redisTemplate) {
        return new LoginAttemptService(redisTemplate);
    }
}

6. 自定义错误消息

AuthenticationEntryPoint 中实现自定义错误消息:

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
    String username = request.getParameter("username");
    int attempts = loginAttemptService.getLoginAttempt(username);
    if (attempts >= MAX_ATTEMPTS) {
        response.setStatus(HttpStatus.FORBIDDEN.value());
        response.getWriter().write("Your account has been locked due to multiple failed login attempts.");
    }
}

7. 测试

使用测试用例验证功能:

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = LoginController.class, excludeAutoConfiguration = {SecurityAutoConfiguration.class})
class LoginControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private LoginAttemptService loginAttemptService;

    @Test
    void shouldLockAccountAfterMaxAttempts() throws Exception {
        for (int i = 0; i < MAX_ATTEMPTS; i++) {
            mockMvc.perform(post("/login")
                            .param("username", "admin")
                            .param("password", "wrong-password"))
                    .andExpect(status().isBadRequest());
        }
        mockMvc.perform(post("/login")
                        .param("username", "admin")
                        .param("password", "wrong-password"))
                .andExpect(status().isForbidden());
        assertEquals(MAX_ATTEMPTS, loginAttemptService.getLoginAttempt("admin"));
    }

    @Test
    void shouldUnlockAccountAfterTimePeriod() throws Exception {
        for (int i = 0; i < MAX_ATTEMPTS; i++) {
            mockMvc.perform(post("/login")
                            .param("username", "admin")
                            .param("password", "wrong-password"))
                    .andExpect(status().isBadRequest());
        }
        Thread.sleep((MAX_ATTEMPTS_TIME + 1) * 1000);
        mockMvc.perform(post("/login")
                        .param("username", "admin")
                        .param("password", "wrong-password"))
                .andExpect(status().isBadRequest());
        assertEquals(1, loginAttemptService.getLoginAttempt("admin"));
    }
}

以上就是SpringBoot整合Redis实现登录失败锁定功能(实例详解)的详细内容,更多请关注编程学习网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     436人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     201人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     163人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     242人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     68人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯