alexkramer / grails-hibernate-filter

Hibernate Filters Support for Grails 3, Hibernate 5, and GORM 6
Apache License 2.0
4 stars 8 forks source link

No Session found for current thread #6

Closed sudosandwich3 closed 7 years ago

sudosandwich3 commented 7 years ago

HibernateFilterInterceptor assumes a session exists in the current thread. In the case of error or the user not being authorized, this will not defined and an exception is thrown. There probably needs to be a check here:

boolean before() {
    def session = sessionFactory.currentSession
    DefaultHibernateFiltersHolder.defaultFilters.each {
        session.enableFilter it
    }
    true
}
vsachinv commented 7 years ago

@sudosandwich3 I have also faced the same issue. Did you find any fix or fixed using inline hibernate-plugin?

alexkramer commented 7 years ago

Can you provide either written steps to reproduce this or an actual test case? I am having trouble understanding how to get into this scenario.

As for a workaround, the interceptor is a spring bean. Did you try to define your own implementation of the interceptor that overrides the interceptor included by this plugin?

vsachinv commented 7 years ago

@alexkramer I have created a custom hibernate interceptor filter bean to override now.

class HibernateFilterInterceptor implements Interceptor {

private static Logger log = LoggerFactory.getLogger(HibernateFilterInterceptor)

SessionFactory sessionFactory

RxHibernateFilterInterceptor() {
    matchAll()
            .excludes(uri: "/static/**").excludes(uri: "/img/**")
            .excludes(uri: "/js/*").excludes(uri: "/css/**")
            .excludes(uri: "/assets/**").excludes(uri: "/**.**")
            .excludes(uri: "/images/**")
            .excludes(uri: "/console/**")
}

boolean before() {
    Session session
    try {
        session = sessionFactory.currentSession
    } catch (HibernateException ex) {
        log.debug("No Hibernate session found for HibernateFilterInterceptor")
        log.debug(ex.message)
        session = null
    }
    if (session) {
        DefaultHibernateFiltersHolder.defaultFilters.each {
            session.enableFilter it
        }
    }
    true
}

}

alexkramer commented 7 years ago

The fix for this should be merged and published to the bintray repo as 0.5.3.