/** * Default no-argument constructor that defaults the internal {@link LdapContextFactory} instance to a * {@link JndiLdapContextFactory}. */ public DefaultLdapRealm() { //Credentials Matching is not necessary - the LDAP directory will do it automatically: setCredentialsMatcher(new AllowAllCredentialsMatcher()); //Any Object principal and Object credentials may be passed to the LDAP provider, so accept any token: setAuthenticationTokenClass(AuthenticationToken.class); this.contextFactory = new JndiLdapContextFactory(); }
@Before public void setUp() { factory = new JndiLdapContextFactory() { //Fake a JNDI environment for the tests: @Override protected LdapContext createLdapContext(Hashtable env) throws NamingException { return createNiceMock(LdapContext.class); } }; }
/** * This is the only test that does not fake the JNDI environment. It is provided for 100% test coverage. * * @throws NamingException thrown because the host is always broken. */ @Test(expected = NamingException.class) public void testGetLdapContext() throws NamingException { factory = new JndiLdapContextFactory(); //garbage URL to test that the context is being created, but fails: String brokenHost = UUID.randomUUID().toString(); factory.setUrl("ldap://" + brokenHost + ":389"); factory.getLdapContext((Object) "foo", "bar"); }
@Test public void testGetLdapContextDefault() throws NamingException { factory = new JndiLdapContextFactory() { @Override protected LdapContext createLdapContext(Hashtable env) throws NamingException { assertEquals("ldap://localhost:389", env.get(Context.PROVIDER_URL)); assertEquals("foo", env.get(Context.SECURITY_PRINCIPAL)); assertEquals("bar", env.get(Context.SECURITY_CREDENTIALS)); assertEquals("simple", env.get(Context.SECURITY_AUTHENTICATION)); assertNull(env.get(SUN_CONNECTION_POOLING_PROPERTY)); return createNiceMock(LdapContext.class); } }; factory.setUrl("ldap://localhost:389"); factory.getLdapContext((Object) "foo", "bar"); }
@SuppressWarnings({"deprecation"}) @Test public void testGetLdapContextStringArguments() throws NamingException { factory = new JndiLdapContextFactory() { @Override protected LdapContext createLdapContext(Hashtable env) throws NamingException { assertEquals("ldap://localhost:389", env.get(Context.PROVIDER_URL)); assertEquals("foo", env.get(Context.SECURITY_PRINCIPAL)); assertEquals("bar", env.get(Context.SECURITY_CREDENTIALS)); assertEquals("simple", env.get(Context.SECURITY_AUTHENTICATION)); assertNull(env.get(SUN_CONNECTION_POOLING_PROPERTY)); return createNiceMock(LdapContext.class); } }; factory.setUrl("ldap://localhost:389"); factory.getLdapContext("foo", "bar"); }
@Test public void testGetSystemLdapContext() throws NamingException { factory = new JndiLdapContextFactory() { @Override protected LdapContext createLdapContext(Hashtable env) throws NamingException { assertEquals("ldap://localhost:389", env.get(Context.PROVIDER_URL)); assertEquals("foo", env.get(Context.SECURITY_PRINCIPAL)); assertEquals("bar", env.get(Context.SECURITY_CREDENTIALS)); assertEquals("simple", env.get(Context.SECURITY_AUTHENTICATION)); assertNotNull(env.get(SUN_CONNECTION_POOLING_PROPERTY)); return createNiceMock(LdapContext.class); } }; factory.setSystemUsername("foo"); factory.setSystemPassword("bar"); factory.setUrl("ldap://localhost:389"); factory.getSystemLdapContext(); }
@Test public void testGetSystemLdapContextPoolingDisabled() throws NamingException { factory = new JndiLdapContextFactory() { @Override protected LdapContext createLdapContext(Hashtable env) throws NamingException { assertEquals("ldap://localhost:389", env.get(Context.PROVIDER_URL)); assertEquals("foo", env.get(Context.SECURITY_PRINCIPAL)); assertEquals("bar", env.get(Context.SECURITY_CREDENTIALS)); assertEquals("simple", env.get(Context.SECURITY_AUTHENTICATION)); assertNull(env.get(SUN_CONNECTION_POOLING_PROPERTY)); return createNiceMock(LdapContext.class); } }; factory.setSystemUsername("foo"); factory.setSystemPassword("bar"); factory.setPoolingEnabled(false); factory.setUrl("ldap://localhost:389"); factory.getSystemLdapContext(); }
/** * Default no-argument constructor that defaults the internal {@link LdapContextFactory} instance to a * {@link JndiLdapContextFactory}. */ public DefaultLdapRealm() { //Credentials Matching is not necessary - the LDAP directory will do it automatically: setCredentialsMatcher(new AllowAllCredentialsMatcher()); //Any Object principal and Object credentials may be passed to the LDAP provider, so accept any token: setAuthenticationTokenClass(AuthenticationToken.class); this.contextFactory = new JndiLdapContextFactory(); }
/** * Default no-argument constructor that defaults the internal {@link LdapContextFactory} instance to a * {@link JndiLdapContextFactory}. */ public JndiLdapRealm() { //Credentials Matching is not necessary - the LDAP directory will do it automatically: setCredentialsMatcher(new AllowAllCredentialsMatcher()); //Any Object principal and Object credentials may be passed to the LDAP provider, so accept any token: setAuthenticationTokenClass(AuthenticationToken.class); this.contextFactory = new JndiLdapContextFactory(); }
protected void initializeLDAP(String ldapUrl, String systemUser, String systemUserPassword, String userNameTemplate) { JndiLdapContextFactory jndiLdapContextFactory = new JndiLdapContextFactory(); jndiLdapContextFactory.setUrl(ldapUrl); jndiLdapContextFactory.setSystemUsername(systemUser); jndiLdapContextFactory.setSystemPassword(systemUserPassword); ldapRealm = new JndiLdapRealm(); ldapRealm.setContextFactory(jndiLdapContextFactory); ldapRealm.setUserDnTemplate(userNameTemplate); ldapRealm.init(); } }
private void configureRealm( Config config ) JndiLdapContextFactory contextFactory = new JndiLdapContextFactory(); Map<String,Object> environment = contextFactory.getEnvironment(); Long connectionTimeoutMillis = config.get( SecuritySettings.ldap_connection_timeout ).toMillis();