27 comments on “Spring Security : limit login attempts example

  1. 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.
    =/

  2. 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

  3. 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.

  4. 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

    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

  5. 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)
    );

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

  7. 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”.

  8. 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 = ?”;

  9. 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.

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

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

Leave a Comment

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