Grails 使用Spring Security';时登录时出错;s";“记住我”;选项

Grails 使用Spring Security';时登录时出错;s";“记住我”;选项,grails,spring-security,Grails,Spring Security,我正在使用Grails1.3.4和SpringSecurityCore1.0.1 如果我在登录时选择“记住我”,有时在再次加载页面时会出现以下错误: errors.GrailsExceptionResolver Error executing tag <g:render>: Error executing tag <sec:ifLoggedIn>: Cannot create a session after the response has been committed

我正在使用Grails1.3.4和SpringSecurityCore1.0.1

如果我在登录时选择“记住我”,有时在再次加载页面时会出现以下错误:

errors.GrailsExceptionResolver Error executing tag <g:render>: Error executing tag <sec:ifLoggedIn>: Cannot create a session after the response has been committed at D:/Workspace/steer/grails-app/views/templates/_header.gsp:18 at D:/Workspace/steer/grails-app/views/layouts/main.gsp:29
org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag <g:render>: Error executing tag <sec:ifLoggedIn>: Cannot create a session after the response has been committed at D:/Workspace/steer/grails-app/views/templates/_header.gsp:18 at D:/Workspace/steer/grails-app/views/layouts/main.gsp:29
at D__Workspace_steer_grails_app_views_layouts_main_gsp$_run_closure2.doCall(D__Workspace_steer_grails_app_views_layouts_main_gsp:66)
at D__Workspace_steer_grails_app_views_layouts_main_gsp$_run_closure2.doCall(D__Workspace_steer_grails_app_views_layouts_main_gsp)
at D__Workspace_steer_grails_app_views_layouts_main_gsp.run(D__Workspace_steer_grails_app_views_layouts_main_gsp:75)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag <sec:ifLoggedIn>: Cannot create a session after the response has been committed at D:/Workspace/steer/grails-app/views/templates/_header.gsp:18
at D__Workspace_steer_grails_app_views_templates__header_gsp.run(D__Workspace_steer_grails_app_views_templates__header_gsp:35)
... 4 more
Caused by: java.lang.IllegalStateException: Cannot create a session after the response has been committed
at D__Workspace_steer_grails_app_views_templates__header_gsp$_run_closure1.doCall(D__Workspace_steer_grails_app_views_templates__header_gsp:22)
at D__Workspace_steer_grails_app_views_templates__header_gsp$_run_closure1.doCall(D__Workspace_steer_grails_app_views_templates__header_gsp)
at grails.plugins.springsecurity.SecurityTagLib$_closure6.doCall(SecurityTagLib.groovy:130)
... 5 more
errors.grailExceptionResolver错误执行标记:错误执行标记:在D:/Workspace/steer/grails app/views/templates/_header提交响应后无法创建会话。gsp:18在D:/Workspace/steer/grails app/views/layouts/main.gsp:29
org.codehaus.groovy.grails.web.taglib.exceptions.grailstageexception:执行标记时出错:执行标记时出错:在D:/Workspace/steer/grails app/views/templates/_header提交响应后无法创建会话。gsp:18在D:/Workspace/steer/grails app/views/layouts/main.gsp:29
在D__工作区_steer _grails _应用程序_视图_布局_main _gsp$_run _closure2.doCall(D_工作区_steer _grails _应用程序_视图_布局_main _gsp:66)
在D__工作区_steer _grails _应用程序_视图_布局_main _gsp$_run _closure2.doCall(D_工作区_steer _grails _应用程序视图_布局_main _gsp)
在D__工作区_方向盘_grails _应用程序_视图_布局_主布局_gsp.运行(D_工作区_方向盘_grails _应用程序_视图_主布局_gsp:75)
运行(Thread.java:662)
原因:org.codehaus.groovy.grails.web.taglib.exceptions.grailstageexception:执行标记时出错:在D:/Workspace/steer/grails app/views/templates/_header提交响应后无法创建会话。gsp:18
在D__工作区_steer _grails _应用程序_视图_模板_标题_gsp.run(D_工作区_steer _grails _应用程序_视图_模板_标题_gsp:35)
... 4更多
原因:java.lang.IllegalStateException:提交响应后无法创建会话
在D\u工作区\u steer\u grails\u应用程序\u视图\u模板\u标题\u gsp$\u运行\u closure1.doCall(D\u工作区\u steer\u grails\u应用程序\u视图\u模板\u标题\u gsp:22)
在D__工作区_steer _grails _应用程序_视图_模板_页眉_gsp$_运行_closure1.doCall(D_工作区_steer _grails _应用程序_视图_模板_页眉_gsp)
位于grails.plugins.springsecurity.SecurityTagLib$\u closure6.doCall(SecurityTagLib.groovy:130)
... 还有5个
有人知道我为什么会犯这个错误吗

\u hear.gsp

        <%@ page import="com.mycompany.myapp.partymodel.roles.SystemUserRole" %>
    <sec:ifLoggedIn>
    <%
    def userId = session.SPRING_SECURITY_CONTEXT?.authentication?.principal?.id
    def userDetails = SystemUserRole.get(userId)
     %>
    <div class="mast_head">
        <ul>
            <li>Welcome ${userDetails?.party?.firstName}</li>
            <li> | </li>
            <li><a href="${createLink(controller: "systemUserRole", action: "editprofile")}">Profile</a></li>
            <li> | </li>
            <li><a href="${createLink(controller: "systemUserRole", action: "changepassword")}">Edit Password</a></li>
            <li> | </li>
            <li><a href="${createLink(controller: "logout", action: "index")}">Sign Out</a></li>
        </ul>
    </div>
    </sec:ifLoggedIn>
    <div id="grailsLogo" class="logo">
        <a href="http://www.mycompany.in/"><img src="${resource(dir:'images',file:'mycompany_trans.png')}" alt="Grails" border="0" height=67 /></a>
    </div>
<my:header/> <!-- This line will call your taglib!! -->
<div id="grailsLogo" class="logo">
        <a href="http://www.mycompany.in/"><img src="${resource(dir:'images',file:'mycompany_trans.png')}" alt="Grails" border="0" height=67 /></a>
    </div>

  • 欢迎${userDetails?.party?.firstName}
  • |
  • |
  • |
LayoutTagLib.groovy

        package com.mycompany.myapp.layout
    import  com.mycompany.myapp.trips.Trip

    class LayoutTagLib {
        static namespace = "my"

          def header = { attrs ->
            Trip.withNewSession {
              sec.ifLoggedIn() {
                out << "<div class="mast_head">
        <ul>
            <li>Welcome ${userDetails?.party?.firstName}</li>
            <li> | </li>
            <li><a href="${createLink(controller: "systemUserRole", action: "editprofile")}">Profile</a></li>
            <li> | </li>
            <li><a href="${createLink(controller: "systemUserRole", action: "changepassword")}">Edit Password</a></li>
            <li> | </li>
            <li><a href="${createLink(controller: "logout", action: "index")}">Sign Out</a></li>
        </ul>
    </div>"
              }
            }
          }
    }
package com.mycompany.myapp.layout
导入com.mycompany.myapp.trips.Trip
类布局{
静态名称空间=“我的”
def头={attrs->
Trip.withNewSession{
第ifLoggedIn()节{

out我认为这是对在
布局中使用此类标记的限制,因为它是在提交hibernate会话后由SiteMesh处理的

解决方案是将其包装在一个会话中(
withNewSession
域类的关闭)。类似于:

LayoutTagLib是一个TagLib,您可以使用命令create TagLib创建

grails create-tag-lib com.app.layout.LayoutTagLib
重要提示:用应用程序的任何现有域类替换“SomeDomain”

class LayoutTagLib {
  static namespace = "my"

  def header = { attrs ->
    SomeDomain.withNewSession {
      sec.ifLoggedIn() {
        out << "Logged In"
      }
    }
  }
}
编辑:

您需要使用newsession将查询包装在
中,并从标题中删除内容,只需调用taglib即可。要获取用户数据,可以使用
springSecurityService

package com.mycompany.myapp.layout
import  com.mycompany.myapp.trips.Trip

class LayoutTagLib {
    //dependency injection of the plugin service
    def springSecurityService

    static namespace = "my"

      def header = { attrs ->

        Trip.withNewSession {

          sec.ifLoggedIn() {

            def userId = springSecurityService.currentUser.id
            def userDetails = SystemUserRole.get(userId)

            //better transform this in a private method or a template!
            out << """<div class="mast_head">
              <ul>
                <li>Welcome ${userDetails?.party?.firstName}</li>
                <li> | </li>
                <li><a href="${createLink(controller: "systemUserRole", action: "editprofile")}">Profile</a></li>
                <li> | </li>
                <li><a href="${createLink(controller: "systemUserRole", action: "changepassword")}">Edit Password</a></li>
                <li> | </li>
                <li><a href="${createLink(controller: "logout", action: "index")}">Sign Out</a></li>
              </ul>
            </div>"""              
           }
         }
      }
}
package com.mycompany.myapp.layout
导入com.mycompany.myapp.trips.Trip
类布局{
//插件服务的依赖注入
def springSecurityService
静态名称空间=“我的”
def头={attrs->
Trip.withNewSession{
第ifLoggedIn()节{
def userId=springSecurityService.currentUser.id
def userDetails=SystemUserRole.get(userId)
//最好在私有方法或模板中进行转换!

我需要用每个域类包装吗?不,只有一个。闭包对所有域类都是一样的。请解释一下。我需要把代码放在哪里。我是grails的新手…非常感谢逐步回答。-谢谢。我更新了问题。如果我替换掉(thml,它将打印在页面中),它会给出错误。。
<my:header/> <!-- This line will call your taglib!! -->
<div id="grailsLogo" class="logo">
        <a href="http://www.mycompany.in/"><img src="${resource(dir:'images',file:'mycompany_trans.png')}" alt="Grails" border="0" height=67 /></a>
    </div>