Grails 3 Spring Security没有Hibernate Session

Bil*_*han 5 grails hibernate grails-plugin grails-3.1 grails-spring-security

我正在将grails 2.4.5应用程序迁移到grails 3.1.11.Application有一个自定义authprovider,允许用户从db或ldap服务器进行身份验证.如果用户是ldap用户,则从ldap验证登录凭据,如果不是来自db.角色从数据库加载.这种情况在grails 2.4.5中运行良好

当迁移到grails 3.1.11时,当我想要访问数据库时,在CustomLdapAuthenticationProvider中抛出"org.hibernate.HibernateException:当前线程找不到会话".如果我把@Transactional放在方法上,错误就会消失.我不知道这是正确的方法,因为我假设grails会在Web请求中处理会话.

我的代码就是这样

....
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
                        .....
                        .....
                        .....

}
}
Run Code Online (Sandbox Code Playgroud)

有什么建议?我必须手动管理会话吗?

编辑:

添加@Transactional注释来验证grails 3版本的方法解决了我的问题.

@Override
@Transactional
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Run Code Online (Sandbox Code Playgroud)