2016-02-03 33 views
0

在我的build.gradle我加入这行代码Grails的3.0和Spring安全错误

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 
} 
+0

您是否已将编译依赖项添加到现有的依赖项块中,或者您是否仅使用此行创建了新的依赖项块? – droggo

+0

现有的依赖块,这是否有区别@droggo –

+0

如果存在,它应该没问题。尝试使用gradle而不是IDE来编译项目。例如项目文件夹中的“gradlew classes”。如果错误是一样的,你能提供整个build.gradle吗? – droggo

回答

0

后“应用插件:“战争“'添加一行说: apply plugin:”maven“

在你的仓库部分

,后“mavenLocal()”中添加一条线,说: mavenCentral()

这最后的建议是刚整容,但我会移动你行: 编译“org.grails。插件:spring-security-core:3.0.3“

并将其放在 之后编译”org.grails.plugins:scaffolding“一行。保留最初的一组grails依赖关系非常好,因为它们来自最初的“grails create-app”命令。这个规则的例外是如果你想用别的东西替换logback日志,但这是另一回事。

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

0

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

对于安装,如果您的文件为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

0

通过导入

import grails.plugin.springsecurity.annotation.Secured 

,你将能够解决您的问题。

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