I'm triying to stablish concurrent session control with non-xml Spring Security, so a user can´t login if he is already logged in another device. I used .sessionManagement() .maximumSessions(1) .maxSessionsPreventsLogin(true), but using chrome and firefox I still can log in concurrently.

I have tried configuring HttpSessionEventPublisher as instructed by other post, but still able to log in concurrently.

This is my WebSecurityConfigurerAdapter :

@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {

private AccessDeniedHandler accessDeniedHandler;

UsuarioRepository usuarioRepository;

public UserDetailsService mongoUserDetails() {
    return new DinamicaUserDetailsService();

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    UserDetailsService userDetailsService = mongoUserDetails();

protected void configure(HttpSecurity http) throws Exception {

                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))


I expect been shown an error while triying to log in in chrome while i'm still logged in firefox, but second concurrent logging is sucessfull.

1 Answers

Agam On

While creating the session, session registry compare the the object of UserDetails to check if there is already a session for that principal.

Since you are using a custom UserDetails service DinamicaUserDetailsService, you should override hashcode and equals method to make sure they are matched for same user. For example you can compare user id or any other unique attribute of user.

public boolean equals(Object user) {
    if(user == null) return false;
    return (user.getId() == getId());