差異處
這裏顯示兩個版本的差異處。
java:ldap:spring:simpleauthentication [2016/04/02 18:44] tony |
java:ldap:spring:simpleauthentication [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>ldap spring spring-ldap spring-security}} | ||
- | ====== LDAP Simple Authentication with Spring API ====== | ||
- | ===== Normal ===== | ||
- | 此範例為: 給予一個admin的dn與password做query,然後針對某一個user做驗證。在此範例中ldapTemplate.authenticate的第一個參數,即使你沒有設定一個對應base,也會從contextSource所設定的base開始找尋。 | ||
- | <code java> | ||
- | LdapContextSource contextSource = new LdapContextSource(); | ||
- | contextSource.setUrl("ldap://192.168.1.13:389"); | ||
- | contextSource.setBase("dc=testldap,dc=org"); | ||
- | contextSource.setUserDn("cn=admin,dc=testldap,dc=org"); | ||
- | contextSource.setPassword("123456"); | ||
- | contextSource.afterPropertiesSet(); | ||
- | |||
- | LdapTemplate ldapTemplate = new LdapTemplate(contextSource); | ||
- | try { | ||
- | ldapTemplate.afterPropertiesSet(); | ||
- | Filter filter = new EqualsFilter("cn", "tonylin"); | ||
- | boolean authed = ldapTemplate.authenticate("ou=sw", filter.encode(), "123456"); | ||
- | Assert.assertTrue(ldapTemplate.authenticate("ou=sw", filter.encode(), "123456")); | ||
- | Assert.assertFalse(ldapTemplate.authenticate("ou=sw", filter.encode(), "654321")); | ||
- | } catch (Exception e) { | ||
- | Assert.fail(e.getMessage()); | ||
- | } | ||
- | </code> | ||
- | ===== With SSL ===== | ||
- | <code java> | ||
- | LdapContextSource contextSource = new LdapContextSource(); | ||
- | contextSource.setUrl("ldaps://192.168.1.13:636"); | ||
- | contextSource.setBase("dc=testldap,dc=org"); | ||
- | contextSource.setUserDn("cn=admin,dc=testldap,dc=org"); | ||
- | contextSource.setPassword("123456"); | ||
- | contextSource.afterPropertiesSet(); | ||
- | |||
- | LdapTemplate ldapTemplate = new LdapTemplate(contextSource); | ||
- | try { | ||
- | ldapTemplate.afterPropertiesSet(); | ||
- | Filter filter = new EqualsFilter("cn", "tonylin"); | ||
- | boolean authed = ldapTemplate.authenticate("ou=sw", filter.encode(), "123456"); | ||
- | System.out.println(authed); | ||
- | } catch (Exception e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | </code> | ||
- | ===== ===== | ||
- | ---- | ||
- | \\ | ||
- | ~~DISQUS~~ |