3

springSecurityService service in User is always null

I can't seem to reference the package itself in the following manner

import grails.plugin.springsecurity.*;

though spring security login page does appear, and gradle doesn't complain on anything missing.

seems like no DI is taking place.

User

package com.sciencecanvas.mykingdom


class User implements Serializable {

    private static final long serialVersionUID = 1

    transient springSecurityService

    String username
    String password
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    User(String username, String password) {
        //this()
        this.username = username
        this.password = password
    }

    @Override
    int hashCode() {
        username?.hashCode() ?: 0
    }

    @Override
    boolean equals(other) {
        is(other) || (other instanceof User && other.username == username)
    }

    @Override
    String toString() {
        username
    }

    Set<Role> getAuthorities() {
        UserRole.findAllByUser(this)*.role
    }

    def beforeInsert() {
        encodePassword()
    }

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

    protected void encodePassword() {
        password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
    }

    static transients = ['springSecurityService']

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

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

my build :

buildscript {
    ext {
        grailsVersion = project.grailsVersion
    }
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {        
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
        classpath "org.grails.plugins:hibernate:4.3.10.5"
    }
}

plugins {
    id "io.spring.dependency-management" version "0.5.2.RELEASE"
    id 'com.jfrog.bintray' version '1.2'
}

version "0.1"
group "monopolyserver"

apply plugin: 'maven-publish'
apply plugin: "spring-boot"
apply plugin: "war"
apply plugin: "asset-pipeline"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"

ext {
    grailsVersion = project.grailsVersion
    gradleWrapperVersion = project.gradleWrapperVersion
}

assets {
    minifyJs = true
    minifyCss = true
}

repositories {
    mavenLocal()
    maven { url "https://repo.grails.org/grails/core" }
}

dependencyManagement {
    imports {
        mavenBom "org.grails:grails-bom:$grailsVersion"
    }
    applyMavenExclusions false
}

dependencies {

    //custom plugins
    compile 'org.grails.plugins:spring-security-core:3.0.0.M1'

    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.springframework.boot:spring-boot-starter-tomcat"
    compile 'mysql:mysql-connector-java:5.1.36'
    compile "org.grails:grails-dependencies"
    compile "org.grails:grails-web-boot"

    compile "org.grails.plugins:hibernate"
    compile "org.grails.plugins:cache"
    compile "org.hibernate:hibernate-ehcache"
    compile "org.grails.plugins:scaffolding"

    //compile ':spring-security-core:2.0-RC5'

    runtime "org.grails.plugins:asset-pipeline"

    testCompile "org.grails:grails-plugin-testing"
    testCompile "org.grails.plugins:geb"

    // Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.)
    testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'

    console "org.grails:grails-console"
}

task wrapper(type: Wrapper) {
    gradleVersion = gradleWrapperVersion
}
shaydel
  • 589
  • 4
  • 15

3 Answers3

6

Service injection in GORM entities is disabled by default since Grails 3.2.8. Because of that, if you want the service springSecurityService to get injected in the previous domain class you would need to turn on autowire. grails-app/conf/application.yml grails: gorm: # Whether to autowire entities. # Disabled by default for performance reasons. autowire: true

葛仪豪
  • 71
  • 1
  • 2
  • 1
    Thanks, took a while to find out what was going on. For those interested it all traces back to this issue: https://github.com/grails/grails-core/issues/640#issuecomment-287768403 – rsilva4 May 22 '17 at 20:36
  • Anyone using Grails 3.3.0 or later might want to use grails.gorm.default.mapping = { autowire true } in /conf/application.groovy, as noted in https://docs.grails.org/latest/ref/Domain%20Classes/Usage.html – Kosi May 10 '22 at 20:11
4

It's because you commented out this() in the constructor - that line of code calls the generated default constructor which does DI. This is described in the "what's new" section of the docs. Either delete the constructor and use the traditional map constructor, or restore that line of code.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
1

You can try Holders.applicationContext.getBean('springSecurityService') instead of injecting it.

Uday
  • 619
  • 3
  • 7