【问题标题】:Grails 3.0 and Spring Security ErrorGrails 3.0 和 Spring 安全错误
【发布时间】:2016-05-13 02:34:22
【问题描述】:

在我的 build.gradle 中我添加了这行代码

 dependencies {

    compile "org.grails.plugins:spring-security-core:3.0.3"

   }

然后当我尝试使用它时

    import grails.plugin.springsecurity.annotation.Secured

    @Secured('ROLE_ADMIN')
     class SecureController {
def index() {
    render 'Secure access only'
}

它“无法解析符号 springsecurity” 我得到了错误

 Error:(5, 1) Groovyc: unable to resolve class Secured ,  unable to find class for annotation

任何帮助将不胜感激。

Build.gradle **** 编辑

这是整个 build.gradle 文件

    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.4.RELEASE"
}

  version "0.1"
  group "securityrolesspring"

    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 {
compile "org.grails.plugins:spring-security-core:3.0.3"
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 "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"

   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
}

【问题讨论】:

  • 您是在现有的依赖块中添加了编译依赖,还是只用这一行创建了新的依赖块?
  • 现有的依赖块,这有什么不同吗@droggo
  • 如果存在应该没问题。尝试使用 gradle 而不是 IDE 编译项目。例如项目文件夹中的gradlew classes。如果错误相同,您可以提供整个 build.gradle 吗?
  • 如何仅使用 gradle 编译项目?我对这个@droggo 有点陌生
  • 是的我现在更新整个 build.gradle @droggo

标签: grails spring-security grails-3.0


【解决方案1】:

在 'apply plugin: "war"' 之后添加一行内容: 应用插件:“maven”

在您的存储库部分中,在“mavenLocal()”之后添加一行内容: mavenCentral()

这最后的建议只是装饰性的,但我会改变你的说法: 编译“org.grails.plugins:spring-security-core:3.0.3”

并将其放在 编译“org.grails.plugins:scaffolding”行。保持最初的 grails 依赖项集与最初的“grails create-app”命令几乎一样,这是一种很好的形式。此规则的例外情况是,如果您想用其他内容替换 logback 日志记录,那是另一回事了。

最后,您还可以将 spring-security-core 依赖项更改为现已发布的 3.0.4 版本。

【讨论】:

    【解决方案2】:

    Spring 安全核心已更新 @Secured Annotations 的包位置。我在我的项目中使用 Grails 3.2.3 和 Spring Security Core 3.1.1 并且运行良好。以下是我使用的配置,并且与之前版本的 spring security core 和 Grails (2.X) 一样工作。

    对于安装,如果您的 build.gradle 文件,您必须在依赖项块中添加条目。 installation section for Spring Security Core的官方文档中的详细信息

    dependencies {
       ...
       compile 'org.grails.plugins:spring-security-core:3.1.1'
       ...
    

    要在您的控制器中使用安全注释,对导入 (org.springframework.security.access.annotation.Secured) 包和您必须分配的 @Secure 注释进行了更新,以“价值”您想要的安全声明去做。例如@Secured('ROLE_ADMIN') 的注解,必须像下面的例子一样改变。

    import org.springframework.security.access.annotation.Secured
    
    @Secured(value=["hasRole('ROLE_ADMIN')"])
    class SecureController {
        def index() {
        render 'Secure access only'
    }
    

    详细配置在官方文档页面updates for secured annotation in version 3,以及如何define secured annotations

    【讨论】:

      【解决方案3】:

      通过导入

      import grails.plugin.springsecurity.annotation.Secured
      

      您将能够解决您的问题。

      它会再次说它无法解析符号“Secured”,但您需要忽略它,因为代码将完全按照您的预期运行。

      【讨论】:

        猜你喜欢
        • 2015-08-12
        • 2012-06-15
        • 2016-03-14
        • 2012-06-01
        • 2015-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-01
        相关资源
        最近更新 更多