Java中SpringSecurity密码错误5次锁定用户的实现方法

作者:xinkou9725 时间:2021-07-10 10:38:17 

Spring Security简介

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

下面看下实例代码:

第一步:创建 AuthenticationSuccessEventListener.Java  用来处理登录成功的事件。


package com.dcits.yft.auth;
import com.dcits.yft.system.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* 登陆成功监听
*
* @author Shaoj 3/2/2017.
*/
@Component
public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {
 @Autowired
 private UserDao userDao;
 @Override
 public void onApplicationEvent(AuthenticationSuccessEvent authenticationSuccessEvent) {
   YftUserDetails yftUserDetails = (YftUserDetails) authenticationSuccessEvent.getAuthentication().getPrincipal();
   String account = yftUserDetails.getUsername();
   Map<String, Object> user = userDao.queryUserByAccount(account);
   userDao.updateStatusByAccount(account, user.get("ENABLE").toString(), 0);
 }
}

第二步:新建AuthenticationFailureListener.java 用来处理登录失败的事件。


package com.dcits.yft.auth;
import com.dcits.yft.system.dao.ParamsDao;
import com.dcits.yft.system.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* 登陆失败监听
*
* @author Shaoj 3/2/2017.
*/
@Component
public class AuthenticationFailureListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
 @Autowired
 private UserDao userDao;
 @Autowired
 private ParamsDao paramsDao;
 @Override
 public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent authenticationFailureBadCredentialsEvent) {
   String account = authenticationFailureBadCredentialsEvent.getAuthentication().getPrincipal().toString();
   Map<String, Object> user = userDao.queryUserByAccount(account);
   if (user != null) {
     // 用户失败次数
     int fails = Integer.parseInt(user.get("FAILS").toString());
     fails++;
     // 系统配置失败次数
     int FAILS_COUNT = Integer.parseInt(paramsDao.queryParamsValue("FAILS_COUNT"));
     // 超出失败次数,停用账户
     if (fails >= FAILS_COUNT) {
       userDao.updateStatusByAccount(account, "false", fails);
       // 失败次数++
     } else {
       userDao.updateStatusByAccount(account, user.get("ENABLE").toString(), fails);
     }
   }
 }
}

第三步:在UserDao.java中加入登录状态更新的代码


/**
* 更新用户登录次数
*
* @param account 账户
* @param login_counts 登录次数
* @return
*/
public void updateLoginCounts(String account) {
 daoUtil.update("update t_yft_user set login_counts = login_counts + 1 where account = ?", account);
}

第四步:数据库中添加登录次数字段


<span style="font-family: Arial, Helvetica, sans-serif;">alter table T_YFT_USER add (FAILS number(11) default 0 );</span>
<span style="font-family: Arial, Helvetica, sans-serif;">comment on column T_YFT_USER.FAILS is '失败尝试次数';</span>
[sql] view plain copy
INSERT INTO t_yft_params (ID,CODE,NAME,VALUE,UNIT,REMARK,CRT_DATE)
VALUES (66,'FAILS_COUNT','登陆尝试次数','5','','',to_date('2017-03-02','yyyy-mm-dd'));

以上所述是小编给大家介绍的Java中SpringSecurity密码错误5次锁定用户的实现方法网站的支持!

来源:http://blog.csdn.net/xinkou9725/article/details/61195791

标签:spring,security,java
0
投稿

猜你喜欢

  • java实现单词搜索迷宫游戏

    2023-11-10 22:44:32
  • java字符串的重要使用方法以及实例

    2023-11-13 23:11:51
  • Java链表(Linked List)基本原理与实现方法入门示例

    2021-10-12 05:49:14
  • Flutter加载图片的多样玩法汇总

    2023-08-24 09:48:22
  • C#中32位浮点数Float(Real)一步步按位Bit进行分析

    2023-07-19 16:01:20
  • 支持SpEL表达式的自定义日志注解@SysLog介绍

    2023-08-27 09:38:42
  • Android Flutter使用本地数据库编写备忘录应用

    2023-09-15 17:24:09
  • 带你重新认识MyBatis的foreach

    2023-11-21 08:44:54
  • 详解利用spring-security解决CSRF问题

    2023-07-31 14:31:19
  • Java回调函数与观察者模式实例代码

    2023-11-16 17:30:11
  • 浅谈Maven的安装及修改为阿里云下载依赖

    2023-08-05 08:30:37
  • maven工程中jar包瘦身的五种方法

    2023-11-04 05:01:54
  • MybatisPlus如何自定义TypeHandler映射JSON类型为List

    2023-08-08 14:05:38
  • java调用外部程序的方法及代码演示

    2023-11-13 22:42:55
  • 通过实例解析Socket套接字通信原理

    2023-11-02 20:17:35
  • jpa EntityManager 复杂查询实例

    2023-08-31 01:03:01
  • Java调用第三方http接口的常用方式总结

    2023-11-06 22:47:24
  • vscode+platformIO开发stm32f4的实现

    2023-11-02 16:43:37
  • Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现

    2023-10-31 14:48:05
  • Spring Cloud如何使用Feign构造多参数的请求

    2023-11-03 00:18:31
  • asp之家 软件编程 m.aspxhome.com