2016-04-20 15 views
1

は、私はそれは私が私達のLDAP管理者が文句を言わないところ、私は必要なLDIFを見つけることを教えているので、私の要件には適用されないと思うLDIFのアプローチを使用しているSpringBoot hereSpringBoot、ldifを使用せずにLDAPで認証する方法は?

でLDAP認証の例をしようとしています。 springbootの前は、ldifを使用しない独自のLDAP実装を使用していました。 SECURITY_AUTHENTICATION.simpleだけでldifを使用しないことを検証する方法はありますか? 以下は、基本的なJavaでldapのセキュリティをどのように行うかです。基本的なユーザー名パスワードだけでldifを使わずに、春にどのようにすればいいですか?

boolean isLdapRegistred(String username, String password) { 
    boolean result = false; 
    try { 

     Hashtable<String, String> env = new Hashtable<String, String>(); 
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     env.put(Context.PROVIDER_URL, "ldap://10.x.x.x:389");   
     env.put(Context.SECURITY_AUTHENTICATION, "simple");   
     env.put(Context.SECURITY_PRINCIPAL, "OUR-DOMAIN\\" + username); 
     env.put(Context.SECURITY_CREDENTIALS, password); 

     // Create the initial context 
     DirContext ctx = new InitialDirContext(env); 
     result = ctx != null; 
     if (ctx != null) 
     ctx.close(); 
     System.out.println(result); 
     return result; 
    } catch (Exception e) { 
     System.out.println("oops"); 
     return result; 
    } 

} 

以下は、SpringBootsの例で、ldifの代わりに自分の資格情報を使用する必要があります。

LDIFなし
@Configuration 
protected static class AuthenticationConfiguration extends 
     GlobalAuthenticationConfigurerAdapter { 

    @Override 
    public void init(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .ldapAuthentication() 
       .userDnPatterns("uid={0},ou=people") 
       .groupSearchBase("ou=groups") 
       .contextSource().ldif("classpath:test-server.ldif"); 
    } 
} 
+0

'.contextSource()。url(" ldap://10.xxx ").porを試したことがありますか? .contextSource()。ldif( "classpath:test-server.ldif"); '?の代わりにt(" 389 " – jny

+0

FYI、@jny、 'port()'メソッドの入力パラメータは、Stringではなくintです。 –

答えて

2

、そして春を使用して、あなたのような何かを行うことができます。

@Configuration 
@EnableWebSecurity 
public class HttpSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(ldapAuthenticationProvider()); 
    } 

    @Bean 
    public AuthenticationProvider ldapAuthenticationProvider() throws Exception { 
     DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(ldapServerUrl); 
     contextSource.setUserDn(ldapManagerDn); 
     contextSource.setPassword(ldapManagerPassword); 
     contextSource.afterPropertiesSet(); 
     LdapUserSearch ldapUserSearch = new FilterBasedLdapUserSearch(ldapUserSearchBase, ldapUserSearchFilter, contextSource); 
     BindAuthenticator bindAuthenticator = new BindAuthenticator(contextSource); 
     bindAuthenticator.setUserSearch(ldapUserSearch); 
     LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator, new DefaultLdapAuthoritiesPopulator(contextSource, ldapGroupSearchBase)); 
     return ldapAuthenticationProvider; 
    } 
} 
0

をこの1は私のために完璧に取り組んできましたが、私はそれに小さな変更を加える必要があります。私はその後、このポイントに入る前の日のためにカスタム認証を使用すると、この

@Component 
public class CustomAuthenticationProvider implements AuthenticationProvider { 

    private Logger log = Logger.getLogger(CustomAuthenticationProvider.class); 

    @Override 
    public Authentication authenticate(Authentication authentication) 
     throws AuthenticationException { 

     String email = authentication.getName(); 
     String password = authentication.getCredentials().toString(); 

     log.info("email : " + email); 
     log.info("password : " + password); 

     try { 
      if (authenticate(email, password)) { 

       // use the credentials 
       // and authenticate against the third-party system 
       return new UsernamePasswordAuthenticationToken(
         email, password, new ArrayList<>()); 
      } else { 
       return null; 
      } 
     } catch (NamingException ex) { 
      log.info(ex); 
     } 
     return null; 
    } 

    boolean isLdapRegistred(String username, String password) { 
    boolean result = false; 
    try { 

     Hashtable<String, String> env = new Hashtable<String, String>(); 
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     env.put(Context.PROVIDER_URL, "ldap://10.x.x.x:389");   
     env.put(Context.SECURITY_AUTHENTICATION, "simple");   
     env.put(Context.SECURITY_PRINCIPAL, "OUR-DOMAIN\\" + username); 
     env.put(Context.SECURITY_CREDENTIALS, password); 

     // Create the initial context 
     DirContext ctx = new InitialDirContext(env); 
     result = ctx != null; 
     if (ctx != null) 
     ctx.close(); 
     System.out.println(result); 
     return result; 
    } catch (Exception e) { 
     System.out.println("oops"); 
     return result; 
    } 

} 


    @Override 
    public boolean supports(Class<?> authentication) { 
     return authentication.equals(
      UsernamePasswordAuthenticationToken.class); 
    } 
} 

と別のクラスの

よう
@Configuration 
@EnableWebSecurity 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    private Logger log = Logger.getLogger(WebSecurityConfiguration.class); 
    @Autowired 
    private CustomAuthenticationProvider authProvider; 

    @Override 
    protected void configure(
     AuthenticationManagerBuilder auth) throws Exception { 

     auth.authenticationProvider(authProvider); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().anyRequest().authenticated() 
      .and() 
      .httpBasic(); 
    } 
} 

を作ることができます 他の賢明を受けた

@Configuration 
    @EnableWebSecurity 
    public class HttpSecurityConfig extends WebSecurityConfigurerAdapter { 
     @Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.authenticationProvider(ldapAuthenticationProvider()); 
     } 

     @Bean 
     public AuthenticationProvider ldapAuthenticationProvider() throws Exception { 
      DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
Arrays.asList("ldapServerUrl:port"),rootDn); 
      contextSource.afterPropertiesSet(); 
      LdapUserSearch ldapUserSearch = new FilterBasedLdapUserSearch(ldapUserSearchBase, ldapUserSearchFilter, contextSource); 
      BindAuthenticator bindAuthenticator = new BindAuthenticator(contextSource); 
      bindAuthenticator.setUserSearch(ldapUserSearch); 
      LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator, new DefaultLdapAuthoritiesPopulator(contextSource, ldapGroupSearchBase)); 
      return ldapAuthenticationProvider; 
     } 
    } 

魔法が起こる