21 3月 2011

Spring Security (3) Basic Configuration fo <authentication-provider>

<authentication-manager>共本上提供了兩種Authentication Provider,一個是authentication-provider,另一個是ldap-authentication-provider

預設的authentication-provide就是採用DaoAuthenticationProvider,有三種基本的帳號密碼檢核與提供人員資料方式

  • user-service:建立一個in-memory的UserDetailService,帳密可以自properties file載入或在xml以<user>建立
  • jdbc-user-service:Spring Security提供了SQL Schema,所以相關的帳密可以自DB載入,只要給jdbc-user-service一個DataSource就可以使用,或是現有欄位定稱與Spring Security不同,那也可以透過提供users-by-username-query、authorities-by-username-query等SQL來取得資料。
  • ldap-user-service:再明顯不過了吧,透過ldap取得帳密資料,在預設的情形下是沒意義的....

通常DB裡存的密碼不會是明碼(如果真的是明碼....),所以Spring Security也提供了基本的編碼方式,像sha、md5都有支援,我們可以透過<password-encoder hash="type">來設定這個authentication provider要用哪種編碼;如果我們的編碼方式不在Spring Security的支援範圍之內,也可以自行提供一個PasswordEncoder的實作。

如果可以完全接受SpringSecurity提供的Schema作為系統的帳密設定,那當然是很不錯的一件事,但我想這種事不太容易發生...所以修改的方式通常有二,一是自已提供UserDetailsService的實作將user service換掉,二是用自已的Authentication Provider實作,直接把authentication provider換掉囉。不過換Authentication Provider的原因通常不是因為帳密的DB Schema不同,通常是因為檢核的方式不同而更換,看看Spring Security提供的其他Authentication Provider名稱就知道,像JaasAuthenticationProvider、OpenIDAuthenticationProvider、CasAuthenticaionProvider等就知道需要更換Authentication Provider大約是在什麼情形才需要更動。

 

 

沒有留言: