According to thymeleaf security page I can get the logged username and roles as below:
Logged user: <span sec:authentication="name">Bob</span>
Roles: <span sec:authentication="principal.authorities">[ROLE_USER, ROLE_ADMIN]</span>
I have a web application where authentication is done through active directory using ActiveDirectoryLdapAuthenticationProvider
as below:
@Bean
@Override
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(adDomain,
adUrl);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
Then after the user logged in I have a header page that I use in all my pages with the above sec:authentication="name"
thymeleaf tag to show the username, but I wanted to see if there's a way to show the full name instead.
Solution suggested here is not working for me:
I'm using: thymeleaf-extras-springsecurity5
and using: <span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"></span>
Is giving me: Method getUser() cannot be found on type org.springframework.security.ldap.userdetails.LdapUserDetailsImpl
It seems this information comes from: org.springframework.security.ldap.userdetails.LdapUserDetailsImpl
and there just a few options are, like the username, but not the rest of the information an AD can have.