Java中SpringSecurity密碼錯誤5次鎖定用戶的實現(xiàn)方法
Spring Security簡介
Spring Security是一個能夠為基于Spring的企業(yè)應用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)功能,為應用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復代碼的工作。
下面看下實例代碼:
第一步:創(chuàng)建 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;
/**
* 登陸成功監(jiān)聽
*
* @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;
/**
* 登陸失敗監(jiān)聽
*
* @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) {
// 用戶失敗次數(shù)
int fails = Integer.parseInt(user.get("FAILS").toString());
fails++;
// 系統(tǒng)配置失敗次數(shù)
int FAILS_COUNT = Integer.parseInt(paramsDao.queryParamsValue("FAILS_COUNT"));
// 超出失敗次數(shù),停用賬戶
if (fails >= FAILS_COUNT) {
userDao.updateStatusByAccount(account, "false", fails);
// 失敗次數(shù)++
} else {
userDao.updateStatusByAccount(account, user.get("ENABLE").toString(), fails);
}
}
}
}
第三步:在UserDao.java中加入登錄狀態(tài)更新的代碼
/**
* 更新用戶登錄次數(shù)
*
* @param account 賬戶
* @param login_counts 登錄次數(shù)
* @return
*/
public void updateLoginCounts(String account) {
daoUtil.update("update t_yft_user set login_counts = login_counts + 1 where account = ?", account);
}
第四步:數(shù)據(jù)庫中添加登錄次數(shù)字段
<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 '失敗嘗試次數(shù)';</span>
[sql] view plain copy
INSERT INTO t_yft_params (ID,CODE,NAME,VALUE,UNIT,REMARK,CRT_DATE)
VALUES (66,'FAILS_COUNT','登陸嘗試次數(shù)','5','','',to_date('2017-03-02','yyyy-mm-dd'));
以上所述是小編給大家介紹的Java中SpringSecurity密碼錯誤5次鎖定用戶的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
javaweb servlet中使用請求轉發(fā)亂碼的實現(xiàn)
下面小編就為大家?guī)硪黄猨avaweb servlet中使用請求轉發(fā)亂碼的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
java使用CompletableFuture分批處理任務實現(xiàn)
本文主要介紹了java使用CompletableFuture分批處理任務實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07
POI導出之Excel實現(xiàn)單元格的背景色填充問題
這篇文章主要介紹了POI導出之Excel實現(xiàn)單元格的背景色填充問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
IDEA 2020.1 for Mac 下載安裝配置及出現(xiàn)的問題小結
這篇文章主要介紹了IDEA 2020.1 for Mac 下載安裝配置及出現(xiàn)的問題小結,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

