Domanda

Per ulteriori sfondo, vedi http://grails.markmail.org/message/62w2xpbgneapmhpd

Sto cercando di deridere il metodo () nel mio BootStrap.groovy Shiro SecurityUtils.getSubject. Ho deciso su questo approccio perché il soggetto costruttore nella sua ultima versione Shiro non è disponibile nella versione corrente del plugin Nimble (che sto usando). Ho deciso di provare a giocare con il SecurityUtils.metaClass, ma ho la sensazione che mi manca qualcosa di molto fondamentale su come metaclassi funzionano. Per avere un riferimento, ecco la mia classe rintracciabile:

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

Nel mio BootStrap.groovy, ho questo:

   def suMetaClass = new ExpandoMetaClass(SecurityUtils)

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

   suMetaClass.initialize()

   SecurityUtils.metaClass = suMetaClass

E funziona ... più o meno. Se stampo fuori l'argomento da BootStrap.groovy ottengo "Canned Soggetto". Se cerco di creare e salvare le istanze di sottoclassi di corriere rintracciabile, ottengo:

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.

Mi sto perdendo qualcosa integrale su come metaclassi funzionano?

È stato utile?

Soluzione

ho capito cosa stava succedendo. Nel mio BootStrap stavo facendo qualcosa di simile:

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
}

Ho aggiunto alcune dichiarazioni di debug e ho visto che gli errori che stavo vedendo stavano accadendo alla fine della chiusura init. Ho fatto qualche googling per ricontrollare come lavare la mia sessione Hibernate. Poi ho fatto le seguenti modifiche:

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
}

che sembra aver completamente risolto il problema e ora dovrei essere in grado di rimuovere i blocchi try / catch ingombranti da corriere rintracciabile. : -)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top