0

This is a creat tutorial and it works very well. But if I want to include some kind of...variable...that holds which username I had logged into. How should I do that in Vaadin?

https://vaadin.com/docs/v14/flow/tutorial/login-and-authentication

In this case, I have two rolles in InMemoryUserDetailsManager and I want to check which user I have used to login to my page.

@EnableWebSecurity
@Configuration
@PropertySource("classpath:login.properties")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    private static final String LOGIN_PROCESSING_URL = "/login";
    private static final String LOGIN_FAILURE_URL = "/login?error";
    private static final String LOGIN_URL = "/login";
    private static final String LOGOUT_SUCCESS_URL = "/login";
    public static final String LOGOUT = "/logout";

    @Value("${login.masterPassword}")
    private String masterPassword;

    @Value("${login.masterName}")
    private String masterName;
    
    @Value("${login.masterRights}")
    private String masterRights;
    
    @Value("${login.slavePassword}")
    private String slavePassword;

    @Value("${login.slaveName}")
    private String slaveName;
    
    @Value("${login.slaveRights}")
    private String slaveRights;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .requestCache().requestCache(new CustomRequestCache())
            .and().authorizeRequests()
            .requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()

            .anyRequest().authenticated()

            .and().formLogin()
            .loginPage(LOGIN_URL).permitAll()
            .loginProcessingUrl(LOGIN_PROCESSING_URL)
            .failureUrl(LOGIN_FAILURE_URL)
            .and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL);
    }

    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers(
            "/VAADIN/**",
            "/favicon.ico",
            "/robots.txt",
            "/manifest.webmanifest",
            "/sw.js",
            "/offline.html",
            "/icons/**",
            "/images/**",
            "/styles/**",
            "/h2-console/**");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(inMemoryUserDetailsManager());
    }

    @Bean
    public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
        // Create master user
        UserDetails masterUser =
            User.withUsername(masterName)
                .password("{noop}" + masterPassword)
                .roles(masterRights)
                .build();
        
         // Create master user
            UserDetails slaveUser =
                User.withUsername(slaveName)
                    .password("{noop}" + slavePassword)
                    .roles(slaveRights)
                    .build();
        return new InMemoryUserDetailsManager(masterUser, slaveUser);
    }
}
euraad
  • 2,467
  • 5
  • 30
  • 51

1 Answers1

0

This solved the issue

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
euraad
  • 2,467
  • 5
  • 30
  • 51