2017-12-15 7 views

答えて

0

あなたがSpring Securityに精通していることを希望します。 WebSecurityConfigurerAdapterを拡張し、AD認証プロバイダを構成する構成クラスを記述します。下記のコードを参照してください。あなたのアプリに合うようにantMatcherを変更してください。 依存関係を追加するspring-security-ldap、spring-boot-starter-securityも同様です。

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

@Autowired 
    private Environment env; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.httpBasic().and().authorizeRequests().antMatchers(AppConstants.LOGIN_URI).fullyAuthenticated().and().formLogin().permitAll(); 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     String ldapUrl = env.getProperty(AppConstants.LDAP_URL); 
     String ldapDomain = env.getProperty(AppConstants.LDAP_DOMAIN); 
     ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(ldapDomain, 
       ldapUrl); 
     provider.setConvertSubErrorCodesToExceptions(true); 
     provider.setUseAuthenticationRequestCredentials(true); 
     auth.authenticationProvider(provider); 
    } 

ユーザが割り当てられているグループを取得するには、コントローラクラスにAuthentication/Principalのインスタンスを挿入します。 Get 電子メール、マネージャーなどのユーザーに対してすべての属性を取得するには、カスタムマッパーを作成する必要があります。以下のコードを参照してください。

@GetMapping(value = { AppConstants.REST_API_LOGIN }) 
    public ResponseEntity<User> authenticateUser(Authentication auth) { 
     List<String> ldapRoles = new ArrayList<String>(); 
     auth.getAuthorities().forEach(a -> ldapRoles.add(a.getAuthority())); 
/* 
Write service class methods to compare ldapRoles against app specific roles from DB and exception handlers to handle exceptions 
*/ 
    User user = userService.getUser(auth); 
    if (!user.isAuthorized()) { 
     logger.info("User:" + auth.getName() + " is not authorized to access program."); 
     throw new UserAuthenticationException(AppConstants.NOT_VALID_MEMBER, user); 
    } 
    logger.info("User:" + auth.getName() + " logged in"); 
    return new ResponseEntity<User>(user, HttpStatus.OK); 
} 
関連する問題