Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法
Spring Security簡介
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應(yīng)用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉(zhuǎn)Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復(fù)代碼的工作。
下面看下實(shí)例代碼:
第一步:創(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密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Mybatis-Plus使用saveOrUpdate及問題解決方法
本文主要介紹了Mybatis-Plus使用saveOrUpdate及問題解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Java實(shí)現(xiàn)帶頭結(jié)點(diǎn)的單鏈表
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)帶頭結(jié)點(diǎn)的單鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Java棧和基礎(chǔ)隊(duì)列的實(shí)現(xiàn)詳解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)中的棧與隊(duì)列,在Java的時(shí)候,對于棧與隊(duì)列的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時(shí)候能夠有扎實(shí)的基礎(chǔ)能力。本文小編就來詳細(xì)說說Java中的棧與隊(duì)列,需要的朋友可以參考一下2022-02-02使用SpringMVC返回json字符串的實(shí)例講解
下面小編就為大家分享一篇使用SpringMVC返回json字符串的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Java實(shí)現(xiàn)企業(yè)微信回調(diào)配置的詳細(xì)步驟與測試
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)企業(yè)微信回調(diào)配置的詳細(xì)步驟與測試,企業(yè)微信回調(diào)是指企業(yè)微信通過HTTP?POST請求將業(yè)務(wù)數(shù)據(jù)回調(diào)到指定的URL上,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-09-09JDK21新特性Record?Patterns記錄模式詳解(最新推薦)
這篇文章主要介紹了JDK21新特性Record?Patterns記錄模式詳解,本JEP建立在Pattern?Matching?for?instanceof(JEP?394)的基礎(chǔ)上,該功能已在JDK?16中發(fā)布,它與Pattern?Matching?for?switch(JEP?441)共同演進(jìn),需要的朋友可以參考下2023-09-09