Created
October 31, 2011 10:41
-
-
Save max747/1327260 to your computer and use it in GitHub Desktop.
Account lock implementation in Spring Security
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | |
... | |
<bean class="your.application.package.LoginFailureEventListenter"> | |
<property name="loginAttemptsThreshold" value="5" /> | |
</bean> | |
... | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginFailureEventListenter implements | |
ApplicationListener<AuthenticationFailureBadCredentialsEvent> { | |
// アカウントロックをかける連続認証失敗回数の閾値 | |
private int loginAttemptsThreshold; | |
public void setLoginAttemptsThreshold(int threshold) { | |
this.loginAttemptsThreshold = threshold; | |
} | |
@Override | |
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) { | |
if (event.getException().getClass().equals(UsernameNotFoundException.class)) { | |
// 存在しないユーザ名の場合は無視 | |
return; | |
} | |
String userId = event.getAuthentication().getName(); | |
recordLoginAttempts(userId); | |
int failedLoginAttempts = countFailedLoginAttempts(userId); | |
if (failedLoginAttempts == loginAttemptsThreshold) { | |
lockoutUser(userId); | |
} | |
} | |
// ログイン失敗の情報を DB に記録... | |
private void recordLoginAttempts(String userId) { | |
// ... | |
} | |
// 何回連続でログインに失敗したかの情報を返す | |
private int countFailedLoginAttempts(String userId) { | |
// ... | |
} | |
// アカウントをロックアウトする | |
private void lockoutUser(String userId) { | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment