Grails Spring Security 3 服务 springSecurityService 为空

Posted

技术标签:

【中文标题】Grails Spring Security 3 服务 springSecurityService 为空【英文标题】:Grails Spring Security 3 service springSecurityService is null 【发布时间】:2015-12-28 00:49:45 【问题描述】:

User中的springSecurityService服务始终为null

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

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

【问题讨论】:

你见过***.com/questions/29286659/…>吗? 是的。不过有一个插件 -> grails-plugins.github.io/grails-spring-security-core/guide/… . 下载链接-> dl.bintray.com/grails/plugins/org/grails/plugins/… 【参考方案1】:

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

【讨论】:

谢谢,花了一段时间才知道发生了什么。对于那些感兴趣的人,这一切都可以追溯到这个问题:github.com/grails/grails-core/issues/640#issuecomment-287768403【参考方案2】:

你可以试试 Holders.applicationContext.getBean('springSecurityService') 而不是注入它。

【讨论】:

谢谢。我尝试了 transcient、autowired 和所有东西,但只有这个有效【参考方案3】:

这是因为您在构造函数中注释掉了 this() - 那行代码调用了生成的执行 DI 的默认构造函数。这在文档的“新增功能”部分中进行了描述。要么删除构造函数并使用传统的地图构造函数,要么恢复那行代码。

【讨论】:

这工作!虽然找不到与 this() 相关的文档。 抱歉,在"domain classes" section - 请参阅以“实用”开头的注释 链接坏了,能重新加载吗

以上是关于Grails Spring Security 3 服务 springSecurityService 为空的主要内容,如果未能解决你的问题,请参考以下文章

Grails 3 Spring Security 覆盖登录表单

Grails 3 和 Spring Security:当用户未登录时返回 401

grails 3 spring security boot starter登录页面定制

Grails - 卸载 Spring Security Core

Grails 使用 spring-security-core-3.0.6+ 重定向注销后

Grails 1.3.5 和 Spring Security Core