gpt4 book ai didi

Java Spring 从 Active Directory UserDetails 获取属性

转载 作者:行者123 更新时间:2023-12-01 17:53:32 27 4
gpt4 key购买 nike

我有一个通过 AD 登录的用户,现在我想获取他们的一些信息。这是我正在使用的示例测试端点:

@RequestMapping(value={"/secure/test"}, method=RequestMethod.GET)
public ResponseEntity<?> getSecureTest(HttpServletRequest request) {
String str = "Test Response";

request.getSession().setAttribute("testVar", "SessionVariable");

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
String currentUserName = authentication.getName();
str = str + "\n -- " + currentUserName + "\n\n";
str = str + userDetails.getUsername(); // matches authentication.getName()
return new ResponseEntity<>(str, HttpStatus.OK);
} else {
str = str + "failed auth";
return new ResponseEntity<>(str, HttpStatus.UNAUTHORIZED);
}
}

我可以获得身份验证,并从中获得 UserDetails,但我相信出来的实现是 LdapUserDetailsImpl

https://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/ldap/userdetails/LdapUserDetailsImpl.html

这似乎没有任何像“getAttribute”之类的方法。如果我想获取 AD 属性之一,例如“mail”或“telephoneNumber”,如何获取?

编辑:

因此,为了尝试提取“title”属性,我扩展了 LdapUserDetailsImpl:

public class CustomUserDetails extends LdapUserDetailsImpl {

private String title;

public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
}

我扩展了 LdapUserDetailsMapper:

public class CustomDetailsContextMapper extends LdapUserDetailsMapper {

@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
LdapUserDetailsImpl ldapUserDetailsImpl = (LdapUserDetailsImpl) super.mapUserFromContext(ctx, username, authorities);
CustomUserDetails customUserDetails = new CustomUserDetails();
customUserDetails.setTitle(ctx.getStringAttribute("title"));
return customUserDetails;
}
}

在我的 Controller 中,我尝试获取这个对象:

CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();

但这给了我一个转换错误......我错过了什么?

WebSecurityConfigurerAdapter 里面有这些东西:

@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
authManagerBuilder.userDetailsService(userDetailsService());
}

@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProviderES()));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(ldapdomain, ldapurl);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);

return provider;
}

最佳答案

首先通过在您的安全配置中添加以下内容来设置您的提供商。如果未设置,则默认为不具有所有属性的简单 LdapUserDetailsMapper。

provider.setUserDetailsContextMapper(userDetailsContextMapper());

@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(ldapdomain, ldapurl);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
provider.setUserDetailsContextMapper(userDetailsContextMapper());
return provider;
}

@Bean
public UserDetailsContextMapper userDetailsContextMapper() {
return new CustomUserMapper();
}

然后创建一个扩展 LdapUserDetailsMapper 的自定义映射器

public class CustomUserMapper extends LdapUserDetailsMapper{

@Override
public CustomUserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities){

// set from userDetails
UserDetails details = super.mapUserFromContext(ctx, username, authorities);

// set directly from ctx
CustomUserDetails customUserDetails = new CustomUserDetails();
customUserDetails.setFirstName(ctx.getStringAttribute("givenName"));
customUserDetails.setLastName(ctx.getStringAttribute("sn"));

return customUserDetails;
}

}

关于Java Spring 从 Active Directory UserDetails 获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47417778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com