【问题标题】:SecureSocial : Extending BasicProfile adding attributes in Securesocial 3.0-M4, play 2.4SecureSocial : 在 Securesocial 3.0-M4 中扩展 BasicProfile 添加属性,玩 2.4
【发布时间】:2015-12-24 17:20:42
【问题描述】:

我正在使用游戏框架 2.4 构建一个网站,其中用户将是最重要的实体。

我想使用带有用户名/密码或电子邮件/密码注册策略的 securesocial (3.0-M4) 模块,但我找不到扩展 BasicProfile 类以添加比已在默认类(例如:名字、姓氏、电子邮件等)

我想添加以下属性: - 性别 - 出生日期 - ...

如果有人知道最好的方法,我会非常高兴! :D

我浏览了整个文档一千次,几乎浏览了整个网络。我认为下一步对我来说是上吊:P

【问题讨论】:

    标签: java scala playframework playframework-2.3 securesocial


    【解决方案1】:

    据我了解,BasicProfile 表示所有 Auth 提供者共有的最少信息。虽然这可能不是 100% 正确,但我认为这是 BasicProfile 背后的假设。 有 currently an issue open on GitHub 来改变它,还有一个 PR 据说可以修复它(老实说我从未尝试过)。

    在我看来,目前最好的选择是使用装饰器/适配器/包装器模式并编写在用户和 BasicProfile 之间来回转换的方法。假设您使用的是 Java、PlayFramework 和 Ebeans,您可以执行以下操作(根据您的需要进行修改):

    public class UserProfile extends Model{
    
    
        //Fields from BasicProfile
        public String providerId;
        public String firstName;
        public String lastName;
        public String fullName;
        public String email;
        public String avatarUrl;
    
        String gender;
        Date dateOfBirth;
        //Define other custom fields
    
        //Your custom PasswordInfo model
        public PasswordInfo passwordInfo;
    
        public UserProfile(BasicProfile basicProfile) {
    
            this.providerId = basicProfile.providerId();
            // this.authUserId = Long.valueOf(profile.);
            if (basicProfile.firstName().isDefined())
                firstName = basicProfile.firstName().get();
    
            if (basicProfile.lastName().isDefined())
                lastName = basicProfile.lastName().get();
            if (basicProfile.fullName().isDefined())
                fullName = basicProfile.fullName().get();
            if (basicProfile.email().isDefined())
                email = basicProfile.email().get();
            if (basicProfile.avatarUrl().isDefined())
                avatarUrl = basicProfile.avatarUrl().get();
            if (basicProfile.passwordInfo().isDefined()) {
                String hasher = basicProfile.passwordInfo().get().hasher();
                String password = basicProfile.passwordInfo().get().password();
                String salt = basicProfile.passwordInfo().get().salt().isDefined() ? basicProfile.passwordInfo().get().salt().get()
                        : null;
                //Your own custom PasswordInfo model (not securesocial)
                passwordInfo = new PasswordInfo(hasher, password, salt, this);
    
            }
    
        }
    
    
        public BasicProfile getSecureSocialBasicProfile() {
            BasicProfile basicProfile = null;
    
            final scala.Option<securesocial.core.PasswordInfo> info = scala.Option.apply(
                    new securesocial.core.PasswordInfo(passwordInfo.hasher, passwordInfo.password, scala.Option.apply(passwordInfo.salt)));
    
            basicProfile = new BasicProfile(providerId, id.toString(), scala.Option.apply(firstName),
                    scala.Option.apply(lastName), scala.Option.apply(fullName), scala.Option.apply(email),
                    scala.Option.apply(avatarUrl), AuthenticationMethod.UserPassword(), scala.Option.empty(),
                    scala.Option.empty(), info);
            return basicProfile;
    
        }
    
        .....
        .....
    
    }
    

    通过上述模型和方法来回转换,您可以轻松地将属性/属性添加到用户配置文件中。希望对您有所帮助。

    【讨论】:

    • 您好,感谢您的回答。我得出了这个结论,我发现它有点复杂,最终注册过程对我来说不够灵活。所以我决定尝试使用 deadbolt 2 和 play-authenticate 的新关联,这似乎为我的项目提供了我可以在 java 中找到的最佳解决方案。我来自php世界,我真的很想掌握java,但是我很惊讶地发现它并没有我想象的那么多插件。你知道一个很好的平台来找到最好的 web/rest-api 开发资源吗?再次感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2013-11-28
    相关资源
    最近更新 更多