Java中SpringSecurity密码错误5次锁定用户的实现方法
时间:2020-10-11 10:44:07|栏目:JAVA代码|点击: 次
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邮件通知工具类
栏 目:JAVA代码
下一篇:Java遍历集合的三种方式
本文标题:Java中SpringSecurity密码错误5次锁定用户的实现方法
本文地址:http://www.codeinn.net/misctech/9636.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




