繁体   English   中英

Spring Security Core Grails插件问题

[英]Spring Security Core Grails Plugin Issue

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

grails install-plugin spring-security-core

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

grails s2-quickstart com.springsecurity SpringUser SpringRole

因此,基本上,它已经为我创建了必需的登录和注销控制器,域控制器以及一些视图/ 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中)存储到数据库,并且没有表,因此引发了一些异常。

任何帮助将不胜感激...

提前致谢..

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

如果在DataSource.groovy中使用dbCreate = create-drop或dbCreate = update,则将为您创建域类的所有表。 如果禁用了dbCreate,则需要创建关联的表,但是在将一个或多个域类添加到Grails应用程序时,这是必需的。

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

终于明白了...

刚刚在hibernate.cfg.xml中注释了以下一行

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

不知道您是否看到了这个,但是这里有一个非常详细的演练方法:

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

具体来说,您的休眠代码似乎未包含在休眠会话中(您的测试代码可能未正确设置),因此出现了错误消息。 通常,您要使用hibernate.hbm2ddl.auto配置hibernate,使其自动创建表等。

有关hibernate.hbm2ddl.auto的更多信息,请参见此处:

Hibernate hbm2ddl.auto可能的值及其作用是什么?

格兰特

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM