【问题标题】:grails spring security multiple custom (domain) classesgrails spring security 多个自定义(域)类
【发布时间】:2015-11-12 00:31:55
【问题描述】:

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

  1. 将 User 类扩展到卖方和买方 - 它不起作用,因为我必须为 spring security 注册一个类

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

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

【问题讨论】:

    标签: grails spring-security


    【解决方案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
    

    可以返回任一类型。如果类型很重要,那么这是一个警告信号,表明一对一不是正确的选择。另一种方法可能是用户域中的 用户类型 属性。

    【讨论】:

    • 我还要补充一点,结合其中一种方法(或类似方法),您应该考虑创建一个custom UserDetailsService。 Spring Security 想要的只是用户名、密码、启用、锁定等,而这些数据可以来自任何地方。如果默认域类不够灵活,那么编写自己的 UserDetailsService 来收集数据是一种非常常见的自定义。
    • 非常感谢 Emmanuel & Burt 的建议。如果我遇到任何问题,会告诉你们。
    猜你喜欢
    • 2011-09-05
    • 2013-02-11
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2016-07-09
    • 2015-12-13
    相关资源
    最近更新 更多