2013-03-01 45 views
2

我使用的是Grails 1.3.4和Spring Security Core 1.0.1。使用Spring Security的「記住我」選項時登錄錯誤

如果我選擇的選項「記住我」當我登錄有時當我再次加載網頁它給這個錯誤:

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 

有誰知道爲什麼我得到這個錯誤?

_Hearer.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> 

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>" 
       } 
      } 
      } 
    } 

回答

1

我覺得這是一個有關,因爲它是由SiteMesh的休眠處理後使用的那種標籤layouts限制會議提交。

解決方案是在會話中封裝(withNewSession關閉域類)。喜歡的東西:

LayoutTagLib是一個標籤庫,你也可以用命令創建創建,標籤庫

grails create-tag-lib com.app.layout.LayoutTagLib 

重要:與任何現有的域類應用程序的更換「SomeDomain」。

class LayoutTagLib { 
    static namespace = "my" 

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

out是將在頁面中打印的html。因此,可以使用頭文件模板中的任何內容進行更改。

那麼你有一個_header.gsp是否正確?只需用您的taglib調用替換<sec:ifLoggedIn>塊。

<my:header /> 

編輯:

你需要用你的查詢的該withNewSession內。也可以從頭文件中移除內容,只需調用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>"""    
      } 
     } 
     } 
} 

_header.gsp

<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> 
+0

我需要與各個領域類來包裝? – maaz 2013-03-04 08:17:58

+0

不,只有一個。關閉對所有人都是一樣的。 – 2013-03-04 14:01:53

+0

請解釋一下..我需要把這個代碼..我是新來的grails ... 一步一步的答案是非常感謝.-謝謝.. – maaz 2013-03-06 06:32:21