0

私は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

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

これでできます"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; 
} 

答えて

1

あなたはスプリングLDAP 1を拡張することで、独自のユーザー詳細マッパーを実装することができます。

package example.active.directory.authentication; 

import org.springframework.ldap.core.DirContextOperations; 
import org.springframework.security.core.GrantedAuthority; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.ldap.userdetails.LdapUserDetailsMapper; 

import java.time.LocalDateTime; 
import java.time.OffsetDateTime; 
import java.time.format.DateTimeFormatter; 
import java.util.Arrays; 
import java.util.Collection; 

public class CustomUserMapper extends LdapUserDetailsMapper{ 

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

     UserDetails details = super.mapUserFromContext(ctx, username, authorities); 
     String[] changedValues = ctx.getStringAttributes("whenchanged"); 
     /// Do something here, like map to your custom UserDetails object. 
     return details;   
    } 
} 

あなたがそのメソッドにブレークポイントを設定した場合、あなたはあなたのデバッガで使用可能なすべての異なる属性を探索することができるはずです。

これは私が与えた別の答えのようになります。Update users informations during login against LDAP AD using Spring

+0

この場合、あなたは右、あなたがctx.getStringAttributeから抜け出すか、どんなものにそれらのフィールドをCustomUserMapperにいくつかの追加フィールドを作成して割り当てる必要があります?これは後でコントローラによってどのように使用されますか?この特定のタイプにキャストするだけですか? – MichaelB

+0

追加のフィールドを(必要に応じて)カスタム 'UserDetails'オブジェクトに追加する必要があります。次に、LDAP UserDetailsマッパーを拡張するマッパーは、必要に応じてUserDetailsオブジェクトのフィールドに値を設定します。コントローラから、必要に応じてSecurityContextからUserDetailsのバージョンを取得することができます。 – MathanMV

+0

私のアップデートを見ることができますか?私はすべてのUserDetailsクラスを拡張して属性を取得しようとしましたが、まだ動作していません。 – MichaelB

関連する問題