Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit login attempts in Spring Security?

Tags:

Is there some configuration or available module in Spring Security to limit login attempts (ideally, I'd like to have an increasing wait time between subsequent failed attempts)? If not, which part of the API should be used for this?

like image 691
Michael Borgwardt Avatar asked Mar 18 '11 11:03

Michael Borgwardt


People also ask

How do I limit the number of login attempts in Spring Security?

Solution. Review the existing Spring Security's authentication class, the “locked” feature is already implemented. To enable the limit login attempts, you need to set the UserDetails. isAccountNonLocked to false.

Can Spring Security log failed logon events?

A user can login failed 3 times maximum. His account will be locked on the last failed attempt. The user account is locked during 24 hours. That means after this duration the user account will be unlocked (upon the next login attempt).

How do I restrict URL in Spring Security?

Securing the URLs The most common methods are: authenticated(): This is the URL you want to protect, and requires the user to login. permitAll(): This is used for URL's with no security applied for example css, javascript. hasRole(String role): Restrict to single role.


2 Answers

From Spring 4.2 upwards annotation based event listeners are available:

@Component public class AuthenticationEventListener {      @EventListener     public void authenticationFailed(AuthenticationFailureBadCredentialsEvent event) {          String username = (String) event.getAuthentication().getPrincipal();          // update the failed login count for the user         // ...     }  } 
like image 97
Markus Pscheidt Avatar answered Sep 20 '22 14:09

Markus Pscheidt


Implement an AuthenticationFailureHandler that updates a count/time in the DB. I wouldn't count on using the session because the attacker is not going to be sending cookies anyway.

like image 31
sourcedelica Avatar answered Sep 21 '22 14:09

sourcedelica