Skip to content

Instantly share code, notes, and snippets.

@TatuLund
Created June 10, 2024 08:44
Show Gist options
  • Select an option

  • Save TatuLund/3fbb70e7f91aa04f67a3f212c20fa039 to your computer and use it in GitHub Desktop.

Select an option

Save TatuLund/3fbb70e7f91aa04f67a3f212c20fa039 to your computer and use it in GitHub Desktop.
An example how to configure SpringSecurity SessionConcurrency with VaadinWebSecurity. This is a built-in feature in SpringSecurity which can be used for example to allow the same user to login only once at the time in the system. When you try to login on another computer you will be given an error until you logout from the previous one. I.e. one…
package org.vaadin.example;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy;
import org.springframework.security.web.session.HttpSessionEventPublisher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import com.vaadin.flow.spring.security.VaadinWebSecurity;
@EnableWebSecurity
@Configuration
public class SecurityConfiguration
extends VaadinWebSecurity {
@Bean
SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
@Bean
public ConcurrentSessionControlAuthenticationStrategy concurrentSessionControlAuthenticationStrategy() {
return new ConcurrentSessionControlAuthenticationStrategy(sessionRegistry());
}
@SuppressWarnings("rawtypes")
@Bean
public ServletListenerRegistrationBean httpSessionEventPublisher() {
return new ServletListenerRegistrationBean(new HttpSessionEventPublisher());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth.requestMatchers(new AntPathRequestMatcher("/public/**"))
.permitAll())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.invalidSessionUrl("/")
.sessionConcurrency(cust -> cust
.maximumSessions(1).expiredUrl("/")
.sessionRegistry(sessionRegistry())
.maxSessionsPreventsLogin(true))
.sessionAuthenticationStrategy(concurrentSessionControlAuthenticationStrategy())
.sessionFixation().changeSessionId());
super.configure(http);
// This is important to register your login view to the
// navigation access control mechanism:
setLoginView(http, LoginView.class);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment