grails 2.4.5 アプリケーションを grails 3.1.11 に移行しています。アプリケーションには、ユーザーが db または ldap サーバーから認証できるようにするカスタム authprovider があります。ユーザーが ldap ユーザーの場合、ログイン資格情報は db からではなくても ldap から検証されます。ロールはデータベースからロードされます。このシナリオは、grails 2.4.5 で正常に動作します。
grails 3.1.11 に移行すると、データベースにアクセスしようとすると、「org.hibernate.HibernateException: No Session found for current thread」が CustomLdapAuthenticationProvider でスローされます。@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
.....
.....
.....
}
}
助言がありますか?セッションを手動で管理する必要がありますか?
編集:
grails 3 バージョンの認証メソッドに @Transactional アノテーションを追加すると、問題が解決しました。
@Override
@Transactional
public Authentication authenticate(Authentication authentication) throws AuthenticationException {