获取GroovyCastException:无法将类为“java.lang.String”的对象“chand”强制转换为grails应用程序中的类“com.vproc.member.MyUserDetails”
我正在使用grails应用程序,作为管理员,我可以将订阅者添加到我的应用程序中。当我尝试通过subscriber/create添加新订阅者时,一封邮件将转到提供的电子邮件id,然后订阅者必须单击电子邮件中提供的链接。但当订户填写所有信息并单击时,我得到以下错误获取GroovyCastException:无法将类为“java.lang.String”的对象“chand”强制转换为grails应用程序中的类“com.vproc.member.MyUserDetails”,grails,groovy,spring-security,Grails,Groovy,Spring Security,我正在使用grails应用程序,作为管理员,我可以将订阅者添加到我的应用程序中。当我尝试通过subscriber/create添加新订阅者时,一封邮件将转到提供的电子邮件id,然后订阅者必须单击电子邮件中提供的链接。但当订户填写所有信息并单击时,我得到以下错误 Cannot cast object 'chand' with class 'java.lang.String' to class 'com.vproc.member.MyUserDetails'. Stacktrace follows:
Cannot cast object 'chand' with class 'java.lang.String' to class 'com.vproc.member.MyUserDetails'. Stacktrace follows:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'chand' with class 'java.lang.String' to class 'com.vproc.member.MyUserDetails'
从SubscriberService.groovy获取以下方法时出错
package com.vrpoc.service.member
import grails.plugin.springsecurity.SpringSecurityUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import com.vproc.member.Customer;
import com.vproc.member.MyUserDetails;
import com.vproc.member.Organization;
import com.vproc.member.Person;
import com.vproc.member.Subscriber;
import com.vproc.member.SubscriberRole;
@Transactional
class MyUserDetailsService implements UserDetailsService{
def springSecurityServivce
static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException {
return loadUserByUsername(username)
}
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Subscriber.withTransaction {
status ->
Person user = Person.findByUsername(username)
if (!user)
throw new UsernameNotFoundException( 'User not found', username)
def session = RequestContextHolder.currentRequestAttributes().session
def params = RequestContextHolder.currentRequestAttributes().params
def newOrgId = params["orgId"]
List<Subscriber> subscribers = []
List<Organization> orgs = []
def currentSubscriber = null
if ( session["orgs"] == null && newOrgId == null){
subscribers = Subscriber.findAllByParty( user) // get all subscribers for this person
subscribers.each { orgs.add(it.customer.party ) } // get all customer and then orgs for subscribers
session["orgs"] = orgs // add orgs to session
session["currentOrg"] = orgs.get(0);
currentSubscriber = subscribers.get(0)
currentSubscriber.contacts.size()
}else if ( newOrgId){
def currentOrgId = newOrgId
Organization organization = Organization.get(currentOrgId)
session["currentOrg"] = organization;
Customer customer = Customer.findByParty( organization)
currentSubscriber = Subscriber.findByCustomerAndParty( customer, user)
}
println currentSubscriber.customer.party.orgName
/*
def newContextOrgId = session["currentOrgId"]
def currentSubscriber = session["currentSubscriber"]
if ( newContextOrgId == null ){
def customer = Customer.findByParty( Organization.get(newContextOrgId))
def person = Person.get(springSecurityServivce.principal.id );
subscriber = Subscriber.findByPartyAndCustomer( person, customer)
session["currentSubscriber"] = subscriber
} */
def authorities = SubscriberRole.findAllBySubscriber(currentSubscriber).collect {
new GrantedAuthorityImpl(it.role.authority)
} as Set
return new MyUserDetails(user.username, user.password, user.enabled, !user.accountExpired, !user.passwordExpired, !user.accountLocked,
authorities ?: NO_ROLES, user.id, user.firstName , user.lastName, currentSubscriber )
}
}
}
MyUserDetailsService.groovy
package com.vrpoc.service.member
import grails.plugin.springsecurity.SpringSecurityUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import com.vproc.member.Customer;
import com.vproc.member.MyUserDetails;
import com.vproc.member.Organization;
import com.vproc.member.Person;
import com.vproc.member.Subscriber;
import com.vproc.member.SubscriberRole;
@Transactional
class MyUserDetailsService implements UserDetailsService{
def springSecurityServivce
static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException {
return loadUserByUsername(username)
}
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Subscriber.withTransaction {
status ->
Person user = Person.findByUsername(username)
if (!user)
throw new UsernameNotFoundException( 'User not found', username)
def session = RequestContextHolder.currentRequestAttributes().session
def params = RequestContextHolder.currentRequestAttributes().params
def newOrgId = params["orgId"]
List<Subscriber> subscribers = []
List<Organization> orgs = []
def currentSubscriber = null
if ( session["orgs"] == null && newOrgId == null){
subscribers = Subscriber.findAllByParty( user) // get all subscribers for this person
subscribers.each { orgs.add(it.customer.party ) } // get all customer and then orgs for subscribers
session["orgs"] = orgs // add orgs to session
session["currentOrg"] = orgs.get(0);
currentSubscriber = subscribers.get(0)
currentSubscriber.contacts.size()
}else if ( newOrgId){
def currentOrgId = newOrgId
Organization organization = Organization.get(currentOrgId)
session["currentOrg"] = organization;
Customer customer = Customer.findByParty( organization)
currentSubscriber = Subscriber.findByCustomerAndParty( customer, user)
}
println currentSubscriber.customer.party.orgName
/*
def newContextOrgId = session["currentOrgId"]
def currentSubscriber = session["currentSubscriber"]
if ( newContextOrgId == null ){
def customer = Customer.findByParty( Organization.get(newContextOrgId))
def person = Person.get(springSecurityServivce.principal.id );
subscriber = Subscriber.findByPartyAndCustomer( person, customer)
session["currentSubscriber"] = subscriber
} */
def authorities = SubscriberRole.findAllBySubscriber(currentSubscriber).collect {
new GrantedAuthorityImpl(it.role.authority)
} as Set
return new MyUserDetails(user.username, user.password, user.enabled, !user.accountExpired, !user.passwordExpired, !user.accountLocked,
authorities ?: NO_ROLES, user.id, user.firstName , user.lastName, currentSubscriber )
}
}
}
不知道为什么会这样。请告诉我您需要在此处显示更多代码。尝试使用springSecurityService.currentUser代替springSecurityService.principalhi@tim_yates,尽管新订户已成功添加到系统中。但首先我必须从应用程序中注销,然后我可以使用新的subscrberon本地主机登录。嗨,@tim_yates根据你的建议,我甚至无法登录到我的应用程序。还有其他建议吗你也可以添加MyUserDetails吗?嗨@dmahapatro,我已经更新了我的问题。请看一看。