2016-02-03 35 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 

,你將能夠解決您的問題。

它會再次說它不能解析符號「安全」,但你需要忽略它,因爲代碼將根據您的期望完全運行。