我使用的是Grails 2.4.3和grails-app / conf / spring / resources.groovy,
beans = {
someBean(com.domain.SomeBeanImpl) {
springSecurityService = ref('springSecurityService')
}
}
但是在SomeBeanImpl
类中,
class SomeBeanImpl {
def springSecurityService
def method() {
def user = springSecurityService.currentUser //null, even after successful log-in
}
}
在我的控制器和服务中,我通常可以检索springSecurityService.currentUser
。
为什么自定义bean上的 null ?如何正确检索它?
已编辑:
我使用了来自http://multi-tenant.github.io/grails-multi-tenant-single-db/docs/v0.8.3/guide/4.%20Scripts.html#4.2.%20mt-spring-security的grails mt-spring-security
,
并在此类中使用springSecurityService bean,
/**
* Spring Security Tenant Resolver
* Extracts the tenantId
* @see http://multi-tenant.github.com/grails-multi-tenant-single-db/
*/
@Log4j
class SpringSecurityTenantResolver implements TenantResolver {
def springSecurityService
Integer resolve(HttpServletRequest request) throws TenantResolveException {
def user = springSecurityService.currentUser
/*
if (!user) {
throw new TenantResolveException('Tenant could not be resolved using SpringSecurity')
}
*/
log.debug "${request.requestURL} : ${user?.username} : ${user?.tenantId}"
return user?.tenantId
}
}
答案 0 :(得分:-1)
尝试将bean.autowire = 'byName'
添加到bean定义