Spring Security Core Grails插件问题

时间:2010-11-11 06:44:54

标签: spring grails spring-security grails-plugin

我刚刚阅读了spring security core grails插件的基本信息,并将其安装在我的grail项目中:

grails install-plugin spring-security-core

之后我使用了插件提供的s2-quickstart:

grails s2-quickstart com.springsecurity SpringUser SpringRole

所以基本上它已经为我创建了所需的登录和注销控制器,域控制器和一些view / gsp文件。

现在出于测试目的,我需要测试一个控制器,所以我创建了一个名为Secure的样本控制器,其代码如下:

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.

看到上面的错误之后,我觉得它实际上是在尝试将指定的对象(在Bootstrap.groovy中)存储到数据库中,并且没有表,所以它会抛出一些异常。

任何帮助都将受到高度赞赏......

提前致谢..

3 个答案:

答案 0 :(得分:3)

错误消息是“getFlushMode在没有活动事务的情况下无效”,它与是否存在表无关。

如果您在DataSource.groovy中使用dbCreate = create-drop或dbCreate = update,那么将为您创建域类的所有表。如果您已禁用dbCreate然后是,则需要创建关联表,但是每次向Grails应用程序添加一个或多个域类时都需要这样做。

查看Grails用户邮件列表,看起来这是一个jar文件与您添加到lib目录中的内容或另一个插件添加的内容之间的冲突。一个用户在看到此错误时发现Drools 4.0是问题所在。你有包含Hibernate jar的插件,或者Hibernate所依赖的其他库,例如ANTLR?

答案 1 :(得分:1)

终于有了......

刚刚在hibernate.cfg.xml

中注释了以下一行
<property name="current_session_context_class">thread</property>

答案 2 :(得分:0)

不确定您是否看过这个,但有一个非常详细的演练如何在此处执行此操作:

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

具体来说,你的hibernate代码似乎不包含在一个hibernate会话中(你的测试代码可能没有正确设置),因而也就是错误信息。通常,您希望使用hibernate.hbm2ddl.auto配置hibernate以使其自动创建表等。

有关hibernate.hbm2ddl.auto的更多信息,请访问:

Hibernate hbm2ddl.auto possible values and what they do?

捐赠

相关问题