5

I am migrating a grails 2.4.5 application to grails 3.1.11. Application has a custom authprovider which allows users to authenticate from db or a ldap server. If a user is ldap user, login credentials are verified from ldap, if not from db. Roles are loaded from database. This scenario works fine in grails 2.4.5

When migrated to grails 3.1.11, "org.hibernate.HibernateException: No Session found for current thread" is thrown in CustomLdapAuthenticationProvider when i want to reach database. If i put @Transactional over the method, error goes away. I don't know that is the correct way because i assumed grails would handle the session already in a web request.

My code is like that

....
class CustomLdapAuthenticationProvider extends LdapAuthenticationProvider {

def ldapAuthProvider
def daoAuthenticationProvider
def springSecurityService
def userDetailsService
def dataSource
def grailsApplication

CustomLdapAuthenticationProvider(LdapAuthenticationProvider ldapAuthenticationProvider) {
    super(ldapAuthenticationProvider.authenticator, ldapAuthenticationProvider.authoritiesPopulator)
}

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.principal?.toString()?.toLowerCase()
    String password = authentication.credentials
    Boolean isExistingLdapUser = User.withCriteria {
                        eq("personUsername", username)
                        eq("personIsldap", true)
                    }[0] as Boolean
                    if (isExistingLdapUser) {
                        authentication = ldapAuthProvider.authenticate(authentication)
                        springSecurityService.loggedIn ? Person.findByPersonUsername(authentication.principal.username) : null
                        .....
                        .....
                        .....

}
}

Any suggestions? Do i have to manage session manually?

edit:

Adding @Transactional annotation to authenticate method for grails 3 version solved my problem.

@Override
@Transactional
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Bilgehan
  • 117
  • 6
  • 1
    Thought Grails managed transactions in services. So yes if it works with @transactional works add it. It's not without so unsure of what real issue besides the addition is – V H Sep 23 '16 at 17:02
  • Have you found an answer for this issue ? If so, could you please post it ? Thank you. – ionutab Oct 25 '18 at 10:14
  • @ionutab I added @Transactional annotation for grails 3 version `@Override @Transactional public Authentication authenticate(Authentication authentication) throws AuthenticationException {` – Bilgehan Oct 26 '18 at 12:18

0 Answers0