2013-11-02 48 views
0

我想配置彈簧安全2.0-RC2插件與我的Grails應用程序的工作。我能夠通過散列密碼將默認管理員用戶插入到mongodb中。我還配置了彈簧安全性,以使用emailAddress而不是username作爲身份驗證的用戶名字段。Grails的春季安全插件驗證失敗

當我嘗試登錄(使用正確的憑據),我得到驗證失敗錯誤。我有點難以理解我做錯了什麼。我可能錯過了一些導致這不起作用的小事。我的配置如下。

Config.groovy我有標準配置並指定usernamePropertyName指向電子郵件地址字段而不是用戶名。

grails.plugin.springsecurity.userLookup.userDomainClassName = 'model.Person' 
grails.plugin.springsecurity.userLookup.usernamePropertyName='email' 
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'model.PersonRole' 
grails.plugin.springsecurity.authority.className = 'model.Role' 
grails.plugin.springsecurity.securityConfigType = SecurityConfigType.InterceptUrlMap 

//Configure URL Restrictions 
grails.plugin.springsecurity.interceptUrlMap = [ 
    '/login/**':   [ 
    'IS_AUTHENTICATED_ANONYMOUSLY' 
    ], 
    '/static/**':  [ 
    'IS_AUTHENTICATED_ANONYMOUSLY' 
    ], 
    '/**':    [ 
    'IS_AUTHENTICATED_REMEMBERED'] 
] 

grails.plugin.springsecurity.password.algorithm = 'SHA-256' 

我再有一個是由彈簧產生的安全性,然後修改,從而改變用戶名電子郵件地址Person.groovy文件。生成的PersonRole.groovyRole.groovy尚未修改。

package model 

class Person { 
    transient springSecurityService 

    String id 
    String firstName 
    String lastName 
    String emailAddress 
    String password 

    boolean enabled = true 
    boolean accountExpired 
    boolean accountLocked 
    boolean passwordExpired 

    static transients = ['springSecurityService'] 

    static constraints = { 
    emailAddress blank: false, unique: true 
    password blank: false 
    } 

    static mapping = { password column: '`password`' } 

    Set<Role> getAuthorities() { 
    PersonRole.findAllByPerson(this).collect { it.role } as Set 
    } 

    def beforeInsert() { 
    encodePassword() 
    } 

    def beforeUpdate() { 
    if (isDirty('password')) { 
     encodePassword() 
    } 
    } 

    protected void encodePassword() { 
    password = springSecurityService.encodePassword(password) 
    } 
} 

在我BootStrap.groovy我創建了一個默認的admin用戶,除非已經存在:

def adminUser = Person.findByEmailAddress('[email protected]') ?: new Person(
    firstName: 'Admin', 
    lastName: 'User', 
    emailAddress: '[email protected]', 
    password: springSecurityService.encodePassword('admin'), 
    enabled: true).save(failOnError: true) 

我還創建一個自定義auth.gsp文件如下,但我已經使用默認的一個具有相同也嘗試結果。

<form action="${postUrl}" method="POST" autocomplete="off"> 
    <h4>Sign in</h4> 
    <g:if test="${flash.message}"> 
     <div class="alert alert-danger" style="padding: 10px">${flash.message}</div> 
    </g:if> 
    <p> 
    <input type="email" class="form-control" placeholder="Email address" name="j_username" autofocus /> 
    </p> 
    <p> 
    <input type="password" class="form-control" placeholder="Password" name="j_password" /> 
    </p> 
    <input type="submit" class="btn btn-lg btn-primary btn-block" value="${message(code: 'springSecurity.login.button')}" /> 
</form> 

那麼,有沒有人看到我失蹤,會停止身份驗證工作?

+1

該物業是'emailAddress'不'email'就像你在你的配置 –

+0

阿好對象有,我重構忘了更改設置那裏。我的問題是這個和被接受的答案的結合。謝謝! – Michael

回答

2

您正在對密碼進行雙重編碼。它在Person類中的beforeInsert中完成,並且您在BootStrap代碼中再次執行此操作。更改

password: springSecurityService.encodePassword('admin'), 

password: 'admin', 
+0

謝謝!我知道我必須做一些我可以忽略的事情。我沒有仔細關注生成的代碼與我作爲BootStrap類的示例找到的代碼的關係。 – Michael

+0

另外,正如在另一條評論中指出的,我沒有正確設置'usernamePropertyName'屬性。 – Michael