Get session capture at a command object in Grails

How can I get a session from a command object?

I tried:

import org.springframework.security.context.SecurityContextHolder as SCH

class MyCommand {
   def session = RCH.currentRequestAttributes().getSession()
}

It throws

java.lang.IllegalStateException: no stream-related requests were found: are you referring to the request attributes outside the actual web request or are processing the request outside the source stream? If you are actually working in a web request and still receive this message, your code probably works outside of the DispatcherServlet / DispatcherPortlet: in this case, use RequestContextListener or RequestContextFilter to set the current request.

+3
source share
3 answers

Spring Security SecurityContextHolder, Grails. Acegi, , ThreadLocal, org.codehaus.groovy.grails.plugins.springsecurity.SecurityRequestHolder, :

import org.codehaus.groovy.grails.plugins.springsecurity.SecurityRequestHolder as SRH
class MyCommand {
   def someMethod() {
      def session = SRH.request.session
   }
}

, , , HTTP-.

+5

RequestContextHolder ( Spring / ):

import org.springframework.web.context.request.RequestContextHolder as RCH
class MyCommand {
   def someMethod() {
       def session = RCH.requestAttributes.session
   }
}
+2

:

import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
HttpSession session = GrailsWebRequest.lookup().currentRequest.session

SecurityRequestHolder.request null -, .

+1
source

Source: https://habr.com/ru/post/1744238/


All Articles