0

由于一些问题,我刚刚将我的应用程序从 Grails 3.2.0 升级到3.2.1 ,并且用户身份验证开始失败。我正在使用 Grails Spring Security Core 插件版本3.1.1

我收到以下异常:

org.springframework.security.authentication.InternalAuthenticationServiceException:
    Cannot cast object 'User(email:user@example.com)' with class 'com.test.User' to class 'com.test.User'
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:126)
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:144)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
    at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
    at grails.plugin.springsecurity.web.authentication.GrailsUsernamePasswordAuthenticationFilter.attemptAuthentication(GrailsUsernamePasswordAuthenticationFilter.groovy:53)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)

Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException:
    Cannot cast object 'User(email:user@example.com)' with class 'com.test.User' to class 'com.test.User'
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
    at com.test.User.findWhere(User.groovy)
    at com.test.User$findWhere.call(Unknown Source)
    at grails.plugin.springsecurity.userdetails.GormUserDetailsService.$tt__loadUserByUsername(GormUserDetailsService.groovy:60)
    at grails.plugin.springsecurity.userdetails.GormUserDetailsService$_loadUserByUsername_closure1.doCall(GormUserDetailsService.groovy)

这仅在我们将它部署在 Apache Tomcat(使用8.5.6)上时才会失败,但它在使用grails run-app.

以下是修改后的依赖build.gradle项(其余配置与create-app生成的相同):

// "compile" changed to "provided"
provided "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails.plugins:spring-security-core:3.1.1"
compile "org.grails.plugins:asynchronous-mail:2.0.0.RC4"
compile "org.mongodb:bson:3.3.0"
compile "org.codehaus.groovy.modules.http-builder:http-builder:0.7.1"
runtime "mysql:mysql-connector-java:5.1.39"
// https://github.com/spring-projects/spring-boot/issues/6761
runtime "com.google.code.gson:gson:2.5"
// Commented this to avoid issue (https://github.com/grails/grails-core/issues/10196)
//provided "org.codehaus.groovy:groovy-ant"

相同的设置在 Grails 3.2.0 中工作。

关于这个例外的任何想法?

4

1 回答 1

1

似乎是 Grails 3.2.1 本身的问题。发出跟踪的 grails/grails-core#10244

解决方法是limitScanningToApplication在您的grails-app/init/PACKAGE/Application.groovy

import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration

class Application extends GrailsAutoConfiguration {
    static void main(String[] args) {
        GrailsApp.run(Application, args)
    }

    @Override
    boolean limitScanningToApplication() {
        return false
    }
}
于 2016-10-19T13:18:32.513 回答