2011-06-01 115 views
3

昨天我開始將我們的Grails應用從Acegi插件0.5.2升級到Spring安全插件。我遇到了幾個問題,也許任何人都可以在這裏幫忙?Grails,從Acegi升級到Spring安全插件

在做了必要的修改之後(如Burt Beckwith在Migrating from the Acegi Plugin上所記錄),我能夠再次啓動Grails應用程序(woohoo!)。但是,登錄無效了。

我們的情況如下:我們將Grails應用程序純粹用於其應用程序邏輯。身份驗證是通過webservices使用用戶名和密碼完成的。作爲後端,我們使用應用程序的數據庫(dao)和LDAP後端。目前,我們已經禁用LDAP,以使測試更容易。

這是確實的驗證代碼:

def authenticate(String username, String password) { 
try { 
     println "Trying authentication with user " + username + " and password " + password + "." 
     def tempToken = new UsernamePasswordAuthenticationToken(username, password) 
     println "Temptoken is " + tempToken 
     def token = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)) 
     println "Authentication token received was: " + token 
    } catch (AuthenticationException authenticationException) { 
     return false 
    } 
    return true  
} 

這將打印到日誌:

Trying authentication with user admin and password admin. 
Temptoken is org.springf[email protected]1f: Principal: admin; Password: [PROTECTED]; Authenticated: false; Details: null; Not granted any authorities 

然後這一切停止。

我們使用的域類是相當簡單的。我們不使用用戶角色之類的類來進行人員與其權限之間的連接。相反,我們使用多對多映射,因爲它一直爲我們工作,並且易於維護。

我們的權威域類:

class Authority { 
static hasMany = [people: Person] 

/** description */ 
String description 
/** ROLE String */ 
String authority = '' 

String authorityType 

static constraints = { 
    authority(help:'x',class:'wide',blank: false,unique:true) 
    description(help:'x',class:'extrawide') 
    authorityType(help:'x',class:'wide') 
    people(help:'x',selectSort:'username',display:false) 
} 

String toString() { 
     return authority; 
} 
} 

而且我們的人域類:

class Person { 

static hasMany = [authorities: Authority] 
static belongsTo = Authority 

//Authority primaryGroup 

/** Username */ 
String username 
/** User Real Name*/ 
String userRealName 
String familyName 
String givenName 

/** MD5 Password */ 
String passwd 
/** enabled */ 
boolean enabled 

String email 
boolean emailShow 

/** description */ 
String description = '' 



static constraints = { 
    username(blank: false, unique: true,help:'x',class:'wide') 
    userRealName(blank: false,help:'x',class:'wide') 
    familyName(blank: false,help:'x',class:'wide') 
    givenName(blank: false,help:'x',class:'wide') 
    email(help:'x',class:'wide') 
    emailShow(help:'x') 
    enabled(help:'x') 
    passwd(blank: false,password:true,show:false,help:'x',class:'wide') 
    authorities(nullable:true,help:'x',sortable:true,selectSort:'authority')   
} 

String toString() { 
     return username; 
    } 
} 

Config.groovy中,我們定義:

security { 
active = false 
cacheUsers = false 

grails.plugins.springsecurity.providerNames = ['daoAuthenticationProvider', 'anonymousAuthenticationProvider', 'rememberMeAuthenticationProvider'] 

grails.plugins.springsecurity.userLookUp.userDomainClassName = "Person" 
grails.plugins.springsecurity.authority.className = "Authority" 

至於文檔去,這應該一切工作(所以它爲「舊」Acegi設置)。

爲了收集更多見解,我簡要地激活了LDAP並找到了相同的問題。 WireShark告訴我,在登錄過程中沒有進行LDAP調用。我的猜測是,Authenticate函數中的代碼有問題,或者SpringSecurity不知道如何提取我們的域類。

我很樂意閱讀任何見解!

+0

什麼是幫助,類和select在約束條件中排序?謝謝? – 2011-06-01 20:16:32

回答

0

看起來該令牌無效,因此authenticate()調用失敗並生成一個異常(IIRC它只在調用成功時返回一個值)。

我的第一個嘗試是檢查數據庫是否有一行用戶:'admin',密碼:'admin'爲環境。

經過一番思考,我會仔細檢查Person類中的密碼屬性。要麼通過域名類映射,要麼通過更好地通過

userLookup.passwordPropertyName = "passwd" 

更新:第三件事要檢查。

active = false 
+0

嗨,我只是測試了你的建議,但沒有效果。但我確實認爲錯誤地命名密碼字段是一個問題,但不是我的主要問題。用戶名/密碼是正確的,算法也是如此(雙重檢查)。謝謝你的幫助! – 2011-06-01 21:37:02

+0

謝謝,活動設置沒有被拾取,因爲它不會附帶grails.plugins.springsecurity。 (自Spring安全插件以來的一項要求),但我將其更改爲主動,甚至進行了測試。可悲的是,這不是我正在狩獵的鬼魂...... – 2011-06-02 20:34:58

+0

好的。您是否在捕獲AuthenticationException時嘗試打印某些日誌以查看發生的確切問題? – Gonfva 2011-06-03 09:35:38