Grails 3.0和Spring安全性错误

时间:2016-02-03 18:26:14

标签: grails spring-security grails-3.0

在我的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

非常感谢任何帮助。

enter image description here

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
}

3 个答案:

答案 0 :(得分:0)

应用插件之后:" war"'添加一行说: 申请插件:" maven"

在你的知识库部分," mavenLocal()"添加一行说: mavenCentral()

这最后的建议只是装饰性的,但我会移动你的线: 编译" org.grails.plugins:spring-security-core:3.0.3"

并将其放置在  编译" org.grails.plugins:脚手架"线。保持初始grails依赖关系的良好形式几乎与最初的grails create-app'相同。命令。此规则的例外情况是,如果您想用其他内容替换logback日志记录,那就是另一个故事。

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

答案 1 :(得分:0)

Spring安全核心已更新@Secured Annotations的软件包位置。我在我的项目中使用Grails 3.2.3和Spring Security Core 3.1.1并且运行良好。以下是我使用的配置,并且正如弹簧安全核心和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

答案 2 :(得分:0)

导入

import grails.plugin.springsecurity.annotation.Secured

您将能够解决您的问题。

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