2010-11-11 50 views
0

剛纔我看到了春天的安全Grails核心插件的基本信息,並在我的大盤項目安裝了它:春季安全Grails核心插件的問題

grails install-plugin spring-security-core 

之後,我已經使用S2-快速入門通過插件提供:

grails s2-quickstart com.springsecurity SpringUser SpringRole 

因此,它基本上爲我創建了所需的登錄和註銷控制器,域控制器和一些視圖/ gsp文件。

立即用於測試目的,我需要測試控制器的一個,所以我已經創建了被命名爲確保與下面的代碼一個樣品控制器:從文檔我已經發現一個步驟,其中

package com.springsecurity; 
import grails.plugins.springsecurity.Secured; 

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

現在它向我展示了創建一個默認用戶並從Bootstrap.groovy角色。所以,我有寫了下面的代碼段BootStrap.groovy中:

def adminRole = new SpringRole(authority: 'ROLE_ADMIN').save(flush: false) 
def userRole = new SpringRole(authority: 'ROLE_USER').save(flush: false) 
String password = springSecurityService.encodePassword('password') 
def testUser = new SpringUser(username: 'me', enabled: true, password: password) 
testUser.save(flush: false) 
SpringUserSpringRole.create testUser, adminRole, true 
assert SpringUser.count() == 1 
assert SpringRole.count() == 2 
assert SpringUserSpringRole.count() == 1 

有一件事我想知道這裏是,我沒有在後臺創建的任何表呢。那麼在這一步需要或上面的代碼將單個用戶存儲在會話中?

通過上面的代碼我得到在運行項目時以下異常:

2010-11-11 11:42:47,932 [main] ERROR context.GrailsContextLoader - Error executing bootstraps: getFlushMode is not valid without active transaction 
org.hibernate.HibernateException: getFlushMode is not valid without active transaction 
     at $Proxy16.getFlushMode(Unknown Source) 
     at BootStrap$_closure1.doCall(BootStrap.groovy:29) 
     at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251) 
     at grails.util.Environment.executeForEnvironment(Environment.java:244) 
     at grails.util.Environment.executeForCurrentEnvironment(Environment.java:220) 
     at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:164) 
     at grails.web.container.EmbeddableServer$start.call(Unknown Source) 
     at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158) 
     at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy) 
     at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280) 
     at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy) 
     at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149) 
     at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy) 
     at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116) 
     at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy) 
     at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59) 
     at RunApp$_run_closure1.doCall(RunApp.groovy:33) 
     at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381) 
     at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415) 
     at gant.Gant$_dispatch_closure7.doCall(Gant.groovy) 
     at gant.Gant.withBuildListeners(Gant.groovy:427) 
     at gant.Gant.this$2$withBuildListeners(Gant.groovy) 
     at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source) 
     at gant.Gant.dispatch(Gant.groovy:415) 
     at gant.Gant.this$2$dispatch(Gant.groovy) 
     at gant.Gant.invokeMethod(Gant.groovy) 
     at gant.Gant.executeTargets(Gant.groovy:590) 
     at gant.Gant.executeTargets(Gant.groovy:589) 
Application context shutting down... 
Application context shutdown. 

看到上面的錯誤後,我覺得它實際上是試圖存儲指定對象(在引導.groovy)到數據庫,並沒有表,所以它拋出一些例外。

任何幫助將高度讚賞...

在此先感謝..

+0

1.你沒不要提到它創建了SpringUser和SpringRole域類,但我想它是如此。 2.您是否將該代碼放入BootStrap的init {}方法中?域類操作應該在那裏工作。 3.您可以嘗試刪除flush:false,它不會損害功能,只會添加更多的DB往返。 4.您也可以嘗試在顯式事務中執行此操作,例如SpringRole.withTransaction {} – 2010-11-11 09:42:47

回答

1

得到它終於...

在hibernate.cfg.xml只是註釋以下行

<property name="current_session_context_class">thread</property> 
+0

我花了半天的時間尋找解決方案,謝謝 – bbZ 2014-09-01 16:19:38

3

的錯誤消息是「getFlushMode也不是沒有積極的交易有效」,這並沒有什麼關係是否存在是否有表格。

如果您在DataSource.groovy中使用dbCreate = create-drop或dbCreate = update,則將爲您的域類創建所有表。如果您已經禁用了dbCreate,那麼是的,您需要創建關聯的表格,但是無論何時將一個或多個域類添加到Grails應用程序,這都是必需的。

看着Grails用戶郵件列表,它看起來像是一個jar文件與你添加到lib目錄的東西衝突,或者是另一個插件添加的東西。一位用戶發現Drools 4.0是他看到這個錯誤時的問題。你有插件,包括Hibernate的jar或Hibernate依賴的其他庫,例如ANTLR的?

+0

感謝您的快速響應...當我註釋爲使用Bootstrap.groovy創建用戶而添加的9行時,沒有錯誤的休眠和項目運行良好...所以這意味着錯誤發生,由於那段代碼只... – Nirmal 2010-11-11 07:49:47

0

不知道,如果你看到了這一點,但沒有對如何做到這一點在這裏一個非常詳細的演練:

http://blog.springsource.com/2010/08/11/simplified-spring-security-with-grails/

具體看來你的Hibernate代碼未包含在Hibernate的Session中(其中你測試代碼可能沒有正確設置),從而出現錯誤消息。通常,您希望使用hibernate.hbm2ddl.auto配置hibernate以使其自動創建表等。

有關hibernate.hbm2ddl的更多信息。汽車,你可以看看這裏:

Hibernate hbm2ddl.auto possible values and what they do?

格蘭特