27 comments on “Spring Security : limit login attempts example

  1. how to download src code becas this link is not working
    spring-security-limit-login-annotation.zip

    Reply
  2. wouldn’t this allow a denial of service attack by locking all the accounts?

    Reply
  3. why any incorrect username or password cannot insert into the user_attempts table? Please help me

    Reply
  4. Excellent tutorial, but i’m having some issue with password encoder (using BCrypt), i map my WebSecurityConfigurerAdapter implementor using:
    auth.userDetailsService(customUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    but when i use:
    @Autowired
    public void configureGlobal
    the spring send me null pointer because don’t find a valid encript id.
    =/

    Reply
  5. This fails to build.

    [INFO] ————————————————————————
    [INFO] BUILD FAILURE
    [INFO] ————————————————————————
    [INFO] Total time: 5.988 s
    [INFO] Finished at: 2017-11-26T23:45:57-08:00
    [INFO] Final Memory: 21M/69M
    [INFO] ————————————————————————
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project spring-security-limit-login-annotation: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

    Reply
  6. Could you write this guide using AuthenticationFailureHandler?

    Reply
  7. how to encrypt username and save to database and decrypt when login is performed using rsa.

    Reply
  8. I have successfully run your project using oracle db.In addition to this ,I want to fetch roles from db dynamically without hardcode using intercept-url in security.xml.How i can achieve this.

    Reply
  9. every works fine mk can you expalin me how this project flow…. or anyone

    Reply
  10. Thank you so much dude! You are the best

    Reply
  11. I’m using hibernate and there seems to be a problem with my transaction management because the update for the field accountNonLocked is not updated. I checked the sql query on the console and it is executed. What could be the problem

    Reply
    1. yes you are right in my case the same program i download and run firstly but first time i have not changed database password then i changed even though user_attempts tables not update do you know the reason

      Reply
  12. correct column of the table field lastModified
    ————————————————————————

    CREATE TABLE user_attempts (
    id int(11) NOT NULL AUTO_INCREMENT,
    username varchar(45) NOT NULL,
    attempts varchar(45) NOT NULL,
    lastModified datetime,
    PRIMARY KEY (id)
    );

    Reply
  13. Thanks for mkyong… above program running. thanks a lot..

    Reply
  14. Why there isn’t any kind of relationship n users and user_attempts table?

    Reply
  15. Is it more easier if we user
    AuthenticationFailureHandler?

    Reply
  16. Can we unlock the user through code or by firing the query on database by changing ‘accountNonLocked’ to 1 ?

    Reply
  17. Hi Mkyong, Awesome Tutorial!! I am trying to add “Password Hashing functionality” in this code by using BCryptPasswordEncoder .. Where do we actually add “encoder” reference? Could you please help in this.. I was following your password hashing tutorial. But unable to integrate here. “Please help me, what are the steps i have to take, to integrate password hashing mechanism”.

    Reply
  18. Thanks for sharing simplest yet effective examples.
    Little correction on the above program. It gives sql exception when setting ‘lastmodifieddate = null’ in UserDetailsDaoImpl.java.
    I had to change 2 lines of code there in the class (same db – mysql) ie.
    private static final String SQL_USER_ATTEMPTS_UPDATE_ATTEMPTS = “UPDATE USER_ATTEMPTS SET attempts = attempts + 1, lastmodified = now() WHERE username = ?”;
    private static final String SQL_USER_ATTEMPTS_RESET_ATTEMPTS = “UPDATE USER_ATTEMPTS SET attempts = 0, lastmodified = now() WHERE username = ?”;

    Reply
  19. getting this “Invalid username and password!” even if provide correct username and password.

    In catalina.out, following is being printed.

    Populated SecurityContextHolder with anonymous token: ‘org.springframework.security.authentication.AnonymousAuthenticationToken@6fabe8e0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffe9938: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: F7A5817B451666073B753408F6713FBA; Granted Authorities: ROLE_ANONYMOUS’

    Please help to solve this issue. Any help will be appreciated. Thanks in advance.

    Reply
    1. Try run in debug mode (via log setting), observe on the sql output, also, make sure the database connection is correct and established.

      Reply
    2. Try run in debug mode (via log setting), observe on the sql output, also, make sure the database connection is correct and established.

      Reply
      1. can you please share how to unlock the account after some time

        Reply

Leave a Comment

Your email address will not be published. Required fields are marked *