使用SpringSecurityCore插件的Grails中的ClassCastException用户对象层次结构

时间:2014-05-27 07:19:45

标签: grails spring-security classcastexception

我在使用Groovy编译器2.0.8,Java 1.7.0的Tomcat 7上使用Grails 2.2.4,生产中的SpringSecurityCore 2.0 RC时遇到了一些奇怪的行为。

我们构建了一个实现基本行为的插件。我们在其中包含了一个名为User的人员类的SpringSecurityCore插件。基本插件中有一些域类具有User字段。

然后我们构建一个包含我们基本插件的应用程序。该应用程序有一个名为RUser的特殊应用程序类,它从User扩展。 RUser设置为SpringSecurityCore作为person class。

问题:

应用程序运行大约1到3天没有错误,但是然后抛出ClassCastExceptions尝试在插件域类的User字段上设置RUser对象。

用户类:

Plugin User class:

class User implements Serializable {
  transient springSecurityService
  String username
  String password
  ...
}

Application User class:

class RUser extends User implements Serializable{
  Cart cart 
  boolean businessUser = false
  boolean taxIncluded = false
  ...
}

Application Config.groovy:
grails.plugin.springsecurity.userLookup.userDomainClassName = 'RUser'

我们基本插件的参与课程:

class UserRole implements Serializable {
  User user
  Role role

  static UserRole create(User user, Role role, boolean flush = false) {
    new UserRole(user: user, role: role).save(flush: flush, insert: true)
  }
 ...
}

class UserHistory implements Serializable{
  User user
  List historyEntries
  static hasMany = [historyEntries:HistoryEntry]
  ...
}

在此示例情况下会出现错误:

在过滤器中:

54: UserHistory.withTransaction{
      def currentUser =springSecurityService.currentUser
      def history = UserHistory.findByUser(currentUser)
      if(!history){
96:     history = new UserHistory(user:user).save(flush:true)
      }
    }

2014-05-23 13:52:17,279 ERROR errors.GrailsExceptionResolver| IllegalArgumentException occurred when processing request: [GET] /index
java.lang.ClassCastException@7ff92284. Stacktrace follows:
java.lang.reflect.UndeclaredThrowableException
at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:687)
at de.neomatt.history.HistoryFilters$_closure1_closure2_closure4.doCall(HistoryFilters.groovy:54)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.reflect.InvocationTargetException
at de.neomatt.history.HistoryFilters$_closure1_closure2_closure4_closure5.doCall(HistoryFilters.groovy:96)
... 10 more
Caused by: org.springframework.beans.MethodInvocationException: Property 'springSecurityService' threw exception; nested exception is java.lang.IllegalArgumentException: java.lang.ClassCastException@7ff92284
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
... 11 more
Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException@7ff92284
... 13 more
2014-05-23 13:52:17,288 ERROR errors.GrailsExceptionResolver| IllegalArgumentException occurred when processing request: [GET] /reservation_demo/ToolsOne
Method name must not be null. Stacktrace follows:
java.lang.IllegalArgumentException: Method name must not be null
at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.retrieveAction(ProxyAwareMixedGrailsControllerHelper.java:41)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

在控制器中:

    RegistrationCode.withTransaction {
     def user = lookupUserClass().findByUsername(registrationCode.username)
     def role = Role.findByAuthority(roleName)
281: UserRole.create user, role
    }

2014-05-26 13:02:24,780 ERROR errors.GrailsExceptionResolver| IllegalArgumentException occurred when processing request: [GET] /app/register/verifyRegistration
java.lang.ClassCastException@a7c83d. Stacktrace follows:
java.lang.IllegalArgumentException: java.lang.ClassCastException@a7c83d
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at de.neomatt.security.UserRole.create(UserRole.groovy:32)
at de.neomatt.security.RegisterController$_closure4_closure14.doCall(RegisterController.groovy:281)
at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:687)
at de.neomatt.security.RegisterController$_closure4.doCall(RegisterController.groovy:271)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

ClassCastExceptions遍布各处,其中在User字段上设置了类型为RUser的对象。从springSecurityService.currentUser或使用lookupUserClass()。findBy(...)方法检索RUser对象。

protected Class<?> lookupUserClass() {
  grailsApplication.getDomainClass(lookupUserClassName()).clazz
}

如前所述,应用程序在生产环境中的未定义时间段内运行时没有错误(没有重新加载,没有配置更改)。同一战争的新部署解决了接下来几个小时/天的问题。

有什么建议吗?

@neomatt

0 个答案:

没有答案