Spring Security簡介
Spring Security是一個能夠為基于Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)功能,為應用系統提供聲明式的安全訪問控制功能,減少了為企業系統安全控制編寫大量重復代碼的工作。
下面看下實例代碼:
第一步:創建 AuthenticationSuccessEventListener.Java 用來處理登錄成功的事件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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 用來處理登錄失敗的事件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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中加入登錄狀態更新的代碼
1
2
3
4
5
6
7
8
9
10
|
/** * 更新用戶登錄次數 * * @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); } |
第四步:數據庫中添加登錄次數字段
1
2
3
4
5
|
<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