我正在尝试根据GORM guide
中的文档设置基于鉴别器的多租户系统确定使用系统的租户我想使用Spring Security Plugin并根据用户ID显示数据。
这是我的租户解答:
class MyTenantResolver implements AllTenantsResolver {
def springSecurityService
@Override
Iterable<Serializable> resolveTenantIds() {
return new DetachedCriteria(User)
.distinct('id')
.list()
}
@Override
Serializable resolveTenantIdentifier() throws TenantNotFoundException {
return springSecurityService.currentUser.id
}
}
我在resources.groovy文件中添加了springsecurityService:
beans = {
myTenantResolver(MyTenantResolver) {
springSecurityService = ref("springSecurityService")
}
}
不幸的是,这会导致循环依赖:
The dependencies of some of the beans in the application context form a cycle:
grailsCacheFilter
↓
(inner bean)#6c1f419f
↓
urlMappingsHandlerMapping
↓
openSessionInViewInterceptor
┌─────┐
| hibernateDatastore
↑ ↓
| hibernateConnectionSourceFactory
↑ ↓
| myTenantResolver
↑ ↓
| springSecurityCoreSpringSecurityService
↑ ↓
| transactionManager
└─────┘
如果能够与登录用户进行核对,我能做些什么?
(使用Grails 3.2.8和Spring Security 3.1.1)
答案 0 :(得分:2)
最后通过RequestContextHolder:
获取会话来解决它class MyTenantResolver implements AllTenantsResolver {
@Override
Iterable<Serializable> resolveTenantIds() {
return new DetachedCriteria(Customer)
.distinct('id')
.list()
}
@Override
Serializable resolveTenantIdentifier() throws TenantNotFoundException {
User.withTransaction {
def session = RequestContextHolder.currentRequestAttributes().getSession()
return User.read(session.getAttribute('SPRING_SECURITY_CONTEXT').authentication.principal.id).customerId
}
}
}
答案 1 :(得分:0)
您可能想看一下示例项目over on grails-samples。它具有功能齐全的“用户作为租户”租户解析器,可用作您自己工作的基础。