Clean installation of Grails 3.2.9 + Spring Security Plugin 3.1.0.
Default User/Role/UserRole domains created by s2-quickstart com.raf.app User Role
.
BootStrap init code:
...
def init = { servletContext ->
def adminRole = Role.findOrSaveWhere(authority: 'ROLE_ADMIN')
def user = User.findOrSaveWhere(username: 'raf',
password: 'password')
if (!user.authorities.contains(adminRole)){
UserRole.create(user, adminRole)
}
...
I saw in a tutorial that the password for this user should appear encoded in the db (using dbconsole to check this out), but in my case it does not (it's just 'password' like in here).
So I went to the User domain and made these horrible changes:
def beforeUpdate() {
encodePassword()
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : springSecurityService.encodePassword(password)
}
And this on the other hand throws NullPointer exception. Why?
Cannot invoke method encodePassword() on null object
So what do I do to have my passwords encrypted, like I like 'em?
Okay, so I believe the NullPointer was caused by what vahid described.
However, to have my passwords encoded by default, I had to set
autowire: true
in Configs/application.yml. Don't ask me why, but it works.