Использование groovy metaClass для макета Shiro SecurityUtils в начальной загрузке - PullRequest
0 голосов
/ 10 ноября 2009

Дополнительную информацию см. http://grails.markmail.org/message/62w2xpbgneapmhpd

Я пытаюсь смоделировать метод Shiro SecurityUtils.getSubject () в моем BootStrap.groovy. Я выбрал этот подход, потому что Subject Builder в последней версии Shiro недоступен в текущей версии плагина Nimble (который я использую). Я решил попробовать поиграть с SecurityUtils.metaClass, но у меня есть ощущение, что мне не хватает чего-то очень фундаментального в том, как работают метаклассы. Для справки вот мой класс Trackable:

    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, я получаю «Консервированный предмет». Если я пытаюсь создать и сохранить экземпляры подклассов Trackable, я получаю:

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.

Я что-то упустил из-за того, как работают метаклассы?

1 Ответ

1 голос
/ 12 ноября 2009

Я понял, что происходит. В моем BootStrap я делал что-то вроде этого:

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
}

Я добавил несколько отладочных операторов и увидел, что ошибки, которые я видел, происходили в конце закрытия init. Я немного погуглил, чтобы дважды проверить, как очистить сеанс Hibernate. Затем я внес следующие изменения:

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
}

Это, похоже, полностью решило проблему, и теперь я должен быть в состоянии удалить громоздкие блоки try / catch из Trackable. : -)

...