【问题标题】:Thymeleaf - Getting full name of authenticated userThymeleaf - 获取经过身份验证的用户的全名
【发布时间】: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

并使用:&lt;span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"&gt;&lt;/span&gt;

正在给我:Method getUser() cannot be found on type org.springframework.security.ldap.userdetails.LdapUserDetailsImpl

此信息似乎来自:org.springframework.security.ldap.userdetails.LdapUserDetailsImpl,只有几个选项,例如用户名,而不是 AD 可以拥有的其他信息。

【问题讨论】:

  • &lt;span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"&gt;&lt;/span&gt; 不适合我。我正在使用thymeleaf-extras-springsecurity5

标签: spring-boot spring-security thymeleaf


【解决方案1】:

这就是我解决问题的方法:

第一

我向提供者添加了一个UserDetailsContextMapperorg.springframework.security.ldap.userdetails.InetOrgPersonContextMapper,如下所示:

@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
  ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(adDomain, adUrl);
  provider.setConvertSubErrorCodesToExceptions(true);
  provider.setUseAuthenticationRequestCredentials(true);          
  provider.setUserDetailsContextMapper(userDetailsContextMapper());  <---

  return provider;
}

@Bean
public UserDetailsContextMapper userDetailsContextMapper() {
  return new LdapUserDetailsMapper() {
      @Override
      public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
          InetOrgPersonContextMapper personContextMapper = new InetOrgPersonContextMapper();
          return personContextMapper.mapUserFromContext(ctx, username, authorities);                  
      }           
  };
}

第二

然后在我的页眉中添加:

<span th:text ="${#authentication.getPrincipal().getDisplayName()}"></span>

显示全名。

通过这种方法,您还可以显示所有其他与广告相关的字段,例如:

<span th:text ="${#authentication.getPrincipal().getMail()}"></span>
<span th:text ="${#authentication.getPrincipal().getTelephoneNumber()}"></span>
<span th:text ="${#authentication.getPrincipal().getRoomNumber()}"></span>

【讨论】:

  • 您好,我尝试了您提到的相同方法。对我来说,用户界面坏了,值没有显示出来。你用的是哪个版本的 Springboot、Spring security 和 thymeleaf?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多