문제

추가 배경은 참조하십시오 http://grails.markmail.org/message/62w2xpbgeneapmhpd

나는 bootstrap.groovy에서 shiro securityutils.getSubject () 메소드를 조롱하려고합니다. 최신 Shiro 버전의 피사체 빌더는 Nimble Plugin의 현재 버전에서 사용할 수 없기 때문에이 접근법을 결정했습니다. 나는 SecurityUtils.metaclass를 가지고 노는 것을 결정했지만 메타 클래스가 어떻게 작동하는지에 대해 매우 근본적인 것을 놓친 느낌이 들었습니다. 참고로 다음은 내 추적 가능한 클래스입니다.

    abstract class Trackable {
       User createdBy
       Date dateCreated
       User lastUpdatedBy
       Date lastUpdated

       static constraints = {
           lastUpdated(nullable:true)
           lastUpdatedBy(nullable:true)
           createdBy(nullable:true)
       }

       def beforeInsert = {
           def subject

           try {
               subject = SecurityUtils.subject
           } catch (Exception e) {
               log.error "Error obtaining the subject.  Message is [${e.message}]"
           }

           createdBy = (subject ? User.get( subject.principal ) :
User.findByUsername("admin"))
       }

       def beforeUpdate = {
           def subject

           try {
               subject = SecurityUtils.subject
           } catch (Exception e) {
               log.error "Error obtaining the subject.  Message is [${e.message}]"
           }

           lastUpdatedBy = (subject ? User.get( subject.principal ) :
User.findByUsername("admin"))
       }
   }

내 bootstrap.groovy에서 나는 이것을 가지고있다 :

   def suMetaClass = new ExpandoMetaClass(SecurityUtils)

   suMetaClass.'static'.getSubject = {[getPrincipal:{2}, toString:{"Canned Subject"}] as Subject}

   suMetaClass.initialize()

   SecurityUtils.metaClass = suMetaClass

그리고 그것은 효과가 있습니다 ... 일종의. bootstrap.groovy에서 피사체를 인쇄하면 "통조림 주제"를 얻습니다. 추적 가능한 서브 클래스 인스턴스를 만들고 저장하려고하면 다음을 얻습니다.

No SecurityManager accessible to this method, either bound to
the org.apache.shiro.util.ThreadContext or as a vm static
singleton.  See the org.apache.shiro.SecurityUtils.getSubject()
method JavaDoc for an explanation of expected environment
configuration.

메타 클라스가 어떻게 작동하는지에 대해 필수적인 것을 놓치고 있습니까?

도움이 되었습니까?

해결책

나는 무슨 일이 일어나고 있는지 알아 냈습니다. 내 부트 스트랩에서 나는 다음과 같은 일을하고있었습니다.

def init = { servletContext->
  switch (Environment.current.name) {
    case "local":
      def suMetaClass = new ExpandoMetaClass(SecurityUtils)
      suMetaClass.'static'.getSubject = {[getPrincipal:{2}, toString:{"Canned Subject"}] as Subject}
      suMetaClass.initialize()
      SecurityUtils.metaClass = suMetaClass

      new TrackableSubClass().save()

      //Create some other domain instances

      SecurityUtils.metaClass = null
  }
  //Create a couple domain instances that aren't environment specific
}

나는 디버그 진술을 추가하고 내가보고있는 오류가 시작 폐쇄의 끝에서 일어나고 있음을 알았습니다. 최대 절전 모드 세션을 플러시하는 방법을 두 번 확인하기 위해 인터넷 검색을했습니다. 그런 다음 다음을 변경했습니다.

def sessionFactory

def init = { servletContext->
  switch (Environment.current.name) {
    case "local":
      def suMetaClass = new ExpandoMetaClass(SecurityUtils)
      suMetaClass.'static'.getSubject = {[getPrincipal:{2}, toString:{"Canned Subject"}] as Subject}
      suMetaClass.initialize()
      SecurityUtils.metaClass = suMetaClass

      new TrackableSubClass().save()

      //Create some other domain instances

      sessionFactory.currentSession.flush()

      SecurityUtils.metaClass = null
  }
  //Create a couple domain instances that aren't environment specific
}

그것은이 문제를 완전히 해결 한 것으로 보이며 이제는 번거로운 시도/캐치 블록을 추적 가능한에서 제거 할 수 있어야합니다. :-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top