thymeleaf 보안 페이지 에 따르면 다음과 같이 로깅 된 사용자 이름과 역할을 얻을 수 있습니다.
Logged user: <span sec:authentication="name">Bob</span>
Roles: <span sec:authentication="principal.authorities">[ROLE_USER, ROLE_ADMIN]</span>
ActiveDirectoryLdapAuthenticationProvider
다음과 같이 Active Directory를 통해 인증이 수행되는 웹 응용 프로그램이 있습니다.
@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 태그가 있는 모든 페이지 에서 사용자 이름을 표시 하는 헤더 페이지가 있지만 대신 전체 이름을 표시하는 방법이 있는지 확인하고 싶었습니다.
여기 에 제안 된 솔루션 이 작동하지 않습니다.
나는 사용하고있다 : 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가 가질 수있는 나머지 정보는 아닙니다.
이것이 내 문제를 해결 한 방법입니다.
먼저
다음 UserDetailsContextMapper
과 org.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>
전체 이름을 표시합니다.
이 접근 방식을 사용하면 다음과 같은 다른 모든 AD 관련 필드도 표시 할 수 있습니다.
<span th:text ="${#authentication.getPrincipal().getMail()}"></span>
<span th:text ="${#authentication.getPrincipal().getTelephoneNumber()}"></span>
<span th:text ="${#authentication.getPrincipal().getRoomNumber()}"></span>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다