私はADを介してログインしているユーザーがいますが、今ではその情報の一部を取得したいと考えています。ここで私は一緒に遊んでいたサンプル・テスト・エンドポイントです:Java SpringはActive Directoryから属性を取得しますUserDetails
@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
これでできます"getAttribute"などを好きにする方法がないようです。 「mail」や「telephoneNumber」などのAD属性の1つを取得したい場合、どうすれば入手できますか?
編集:
だから、ちょうど私が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;
}
}
を、私はこのオブジェクトを取得しようとします:
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;
}
この場合、あなたは右、あなたがctx.getStringAttributeから抜け出すか、どんなものにそれらのフィールドをCustomUserMapperにいくつかの追加フィールドを作成して割り当てる必要があります?これは後でコントローラによってどのように使用されますか?この特定のタイプにキャストするだけですか? – MichaelB
追加のフィールドを(必要に応じて)カスタム 'UserDetails'オブジェクトに追加する必要があります。次に、LDAP UserDetailsマッパーを拡張するマッパーは、必要に応じてUserDetailsオブジェクトのフィールドに値を設定します。コントローラから、必要に応じてSecurityContextからUserDetailsのバージョンを取得することができます。 – MathanMV
私のアップデートを見ることができますか?私はすべてのUserDetailsクラスを拡張して属性を取得しようとしましたが、まだ動作していません。 – MichaelB