Grails Spring Security 3服务springSecurityService为空

标签 grails grails-plugin

用户中的 springSecurityService 服务始终为空

我似乎无法通过以下方式引用包本身

import grails.plugin.springsecurity.*;

尽管确实出现了 spring 安全登录页面,但 gradle 并没有提示缺少任何东西。

似乎没有发生 DI。

用户
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`'
    }
}

我的构建:
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
}

最佳答案

自 Grails 3.2.8 起,GORM 实体中的服务注入(inject)默认禁用。因此,如果您希望服务 springSecurityService 被注入(inject)到之前的域类中,您需要打开 Autowiring 。
grails-app/conf/application.yml
chalice :
格姆:
# 是否 Autowiring 实体。
# 出于性能原因默认禁用。
自动接线:真

关于Grails Spring Security 3服务springSecurityService为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32862623/

相关文章:

jquery - "Reverse Search"Grails App中的搜索功能

hibernate - 替代grails多列唯一约束(乐观插入)

grails - 从groovy堆栈跟踪中获取源代码信息

java - 使用具有多对多关系的 Grails 的 withCriteria 函数的重复问题

tomcat - 使用 grails flash 对象和负载平衡

caching - 在Grails中设置全局 map

Grails:是否可以从 afterInsert() 或 afterUpdate() 查询更新后的保存对象?

grails - Grails Spring Security:在双角色之间切换

使用 run-app 运行时未找到 Grails 插件资源

grails - 从另一个grails插件运行时,Grails Assets 管道失败