跳到主要內容

發表文章

目前顯示的是有「Spring」標籤的文章

LdapAuthenticationProvider - Support Secure connection

Introduction 前一篇文章 中,告訴大家如何使用LdapAuthenticationProvider去透過LDAP server做驗證。然而驗證動作透過明文傳遞資料是非常不安全的,有心人士隨便就可以竊取你的帳號密碼。如果今天包是出在你的軟體上,客戶會對你們的軟體失去信心(幹譙是一定的)。因此本篇主要告訴大家,如何透過安全連線做驗證。 How to? 首先需要注意的是這幾個名詞: SSL、TLS與StartTLS。 SSL與TLS: 使用ldaps,通常port為636,為加密連線。TLS是用來取代SSL的;一般用於client-server溝通,如https。 StartTLS: 使用ldap,通常port為389,是非加密連線的擴充。連線時會透過定義好的溝通方式,將連線升級為加密連線,可以是SSL也可以是TLS;一般用於server內部溝通。 TLS和StartTLS非常容易混淆,甚至認為它是同種東西。像LdapAdmin、Linux、Cisco Firesight等就是把StartTLS當TLS,而Apache Studio、SonicWall是稱為StartTLS或StartTLS extension。 SSL 要使用SSL只要將protocol改為ldaps並注意使用的port即可,我分別以驗證成功與失敗做例子: @Test public void testSSL ( ) { 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 ( ) ;   LdapAuthenticationProvi...

LdapAuthenticationProvider - 透過LDAP做認證

Introduction LdapAuthenticationProvider是Spring-Security整合LDAP認證的實做。本篇文章要告訴大家,如何透過它去驗證使用者並取得此使用者的對應群組。 How to? 要達成我們的目標,有幾個步驟需要完成: 連線設置: 設置LdapContextSource物件,先前有寫過相關文章。 使用者搜尋設置: 設置FilterBasedLdapUserSearch與BindAuthenticator物件。 群組搜尋設置: 設置DefaultLdapAuthoritiesPopulator物件。 執行認證: 透過LdapAuthenticationProvider執行驗證。 讓我們透過範例來做說明。 我的範例 我希望能透過LdapAuthenticationProvider去認證ow=sw下的tonylin並取得其群組名稱mis。 連線設置 即建立LdapContextSource物件,我使用DefaultSpringSecurityContextSource完成我的實作。 LdapContextSource contextSource = new DefaultSpringSecurityContextSource ( "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 ( ) ; 如果你想透過多台LDAP server做認證,可以傳入String List給DefaultSpringSecurityContextSource,也可以透過多個provider方式去達到目的。差別在於前者的設定必須相同;後者則有較高的控制靈活度。 使用者搜尋設置 LdapAuthenticationProvider將認證的動作交給LdapAuthenticator負責。實作是透...

Support SAM-Account-Name of AD Provider

Introduction Windows Active Directory提供User Principle Name(簡稱UPN)與SAM Account Name(簡稱SAM)兩種登入方式: 然而ActiveDirectoryLdapAuthenticationProvider僅支援UPN的驗證方式。因此本篇文章主要告訴大家如何支援SAM驗證方式。 How to? ActiveDirectoryLdapAuthenticationProvider驗證UPN的方式,是透過使用者輸入的帳號密碼,並藉由JNDI去做搜尋。而搜尋過濾的條件為: ( & ( objectClass = user ) ( userPrincipalName = { 0 } ) ) 所以我們也許可以透過JNDI並搭配搜尋過濾條件去驗證SAM;在開始修改Provider前,我要先確認JNDI是否有辦法支援SAM。我撰寫以下程式碼做確認: Domain: TEST.COM SAM: TEST\test filter: (&(objectClass=user)(samaccountname=test)) LdapContextSource contextSource = new DefaultSpringSecurityContextSource ( "ldap://10.134.15.138:389" ) ; contextSource. setBase ( "DC=TEST,DC=COM" ) ; //contextSource.setUserDn("test@TEST.COM"); contextSource. setUserDn ( "TEST \\ test" ) ; contextSource. setPassword ( "123456" ) ; contextSource. afterPropertiesSet ( ) ;   LdapTemplate ldapTemplate = new LdapTemplate ( contextSource ) ; ldapTemplate. afterPropertiesSet ( ) ;   Search...

Spring-Security with LDAP物件關係

Introduction 本篇主要記載Spring-Security-LDAP中,我們有使用到的物件去做說明。我們目的是整合LDAP與AD驗證,其中為了滿足我們的需求,有對不少Spring所提供的物件做擴充。這部分有機會再另外分享。 Configuration With Code 以我的範例來說,首先會透過資料庫做登入認證,接著會是AD,最後是LDAP。而透過程式碼配置的方式,會去extend WebSecurityConfigurerAdapter;接著override configure的method,AuthenticationManagerBuilder可以讓你配置AuthenticationProvider: @Autowired @Qualifier ( "dataSource" ) DataSource datasource ;   @Autowired @Qualifier ( "ldapConfig" ) Properties ldapConfig ;   @Autowired @Qualifier ( "adConfig" ) Properties adConfig ;   @Override protected void configure ( AuthenticationManagerBuilder aAuth ) throws Exception { aAuth. jdbcAuthentication ( ) . dataSource ( datasource ) ;   String domain = ( String ) adConfig. get ( "ad.domain" ) ; String url = ( String ) adConfig. get ( "ad.url" ) ; ActiveDirectoryLdapAuthenticationProvider adProvider = new ActiveDirectoryLdapAuthenticationProvider ( domain, url ) ; aAuth. authenticationProvid...

LDAP Simple Authentication with Spring API

Normal 此範例為: 給予一個admin的dn與password做query,然後針對某一個user做驗證。在此範例中ldapTemplate.authenticate的第一個參數,即使你沒有設定一個對應base,也會從contextSource所設定的base開始找尋。我是以cn為使用者的名稱。 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 ( ...