我想配置彈簧安全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.groovy
和Role.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>
那麼,有沒有人看到我失蹤,會停止身份驗證工作?
該物業是'emailAddress'不'email'就像你在你的配置 –
阿好對象有,我重構忘了更改設置那裏。我的問題是這個和被接受的答案的結合。謝謝! – Michael