grails spring security多个自定义(域)类

时间:2015-08-18 21:44:46

标签: grails spring-security

我的网站有两种类型的用户:卖家和买家。这两个域类都不同(卖方有拍卖,买方有出价)。如何使用Spring安全性插件创建两个域类?

  1. 将User类扩展到卖方和买方 - 它将无法正常工作,因为我必须为春季安全注册一个类

  2. 将用户对象作为卖方和买方的属性。那会有用吗?有人曾尝试过吗?

  3. 我在互联网上找不到一个例子。我发现的只是如何通过修改User类或扩展它来自定义插件。如果你能指出一些好的方向。我是Grails的新手。感谢。

1 个答案:

答案 0 :(得分:1)

这实际上取决于卖方买方的含义。

如果它与Amazon.com类似;他们如何允许一个帐户(又名用户)拥有卖家资料和客户(买家)资料,然后您可以向用户添加卖家和买家资产。

class User { 
    Seller seller
    Buyer buyer
}

然后根据需要选择合适的个人资料

springSecurityService.currentUser.seller
// or
springSecurityService.currentUser.buyer

如果用户只能是买方或卖方,那么一对一的关联将是最好的,但它需要买方和卖方的共同超类。

class User { 
    Static hasOne = [profile: UserProfile]
}

class UserProfile { }

class Seller extends UserProfile {
    User user
}

class Buyer extends UserProfile {
    User user
}

但是使用这种方法,您将无法知道您所拥有的类型。因为

springSecurityService.currentUser.profile

可以返回任何一种类型。如果类型很重要,那么这是一个警告标志,即一对一不是正确的选择。替代方案可能是用户域中的用户类型属性。

相关问题