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 {