【发布时间】:2020-06-03 20:31:06
【问题描述】:
根据 thymeleaf security page 我可以得到记录的用户名和角色如下:
Logged user: <span sec:authentication="name">Bob</span>
Roles: <span sec:authentication="principal.authorities">[ROLE_USER, ROLE_ADMIN]</span>
我有一个 Web 应用程序,其中使用 ActiveDirectoryLdapAuthenticationProvider 通过活动目录完成身份验证,如下所示:
@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;
}
然后在用户登录后,我有一个标题页,我在所有页面中使用上面的sec:authentication="name" thymeleaf 标记来显示用户名,但我想看看是否有办法显示全名。
建议的解决方案 here 不适合我:
我正在使用:thymeleaf-extras-springsecurity5
并使用:<span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"></span>
正在给我:Method getUser() cannot be found on type org.springframework.security.ldap.userdetails.LdapUserDetailsImpl
此信息似乎来自:org.springframework.security.ldap.userdetails.LdapUserDetailsImpl,只有几个选项,例如用户名,而不是 AD 可以拥有的其他信息。
【问题讨论】:
-
<span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"></span>不适合我。我正在使用thymeleaf-extras-springsecurity5
标签: spring-boot spring-security thymeleaf