Play SecureSocial Persistance with Java

不打扰是莪最后的温柔 提交于 2019-12-18 13:39:18

问题


I started using play 2.1 for java and SecureSocial. Currently I'm trying to add some persistance to the Users that register/sign up to the site but I'm having trouble trying to represent the model that uses SecureSocial to save/retrieve data in their examples to a persistence model.

Also in the web page says that we can implement SocialUser Object, but I really don't know how I can use it because is a scala object. An example of how could I call or extend that Object would be nice.

Thanks in advance.


回答1:


I am using ebean to save the users.

My user class looks like this:

package models;

import javax.persistence.Entity;
import javax.persistence.Id;

import play.db.ebean.Model;


@Entity
public class LocalUser extends Model  {

    private static final long serialVersionUID = 1L;

    @Id
    public String id;

    public String provider;

    public String firstName;

    public String lastName;

    public String email;

    public String password;

    public static Finder<String, LocalUser> find = new Finder<String, LocalUser>(
            String.class, LocalUser.class
    );

    @Override
    public String toString() {
        return this.id + " - " + this.firstName;
    }
}

My token class:

package models;


import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

import play.db.ebean.Model;

@Entity
public class LocalToken extends Model {

    private static final long serialVersionUID = 1L;

    @Id
    public String uuid;

    public String email;

    public Date createdAt;

    public Date expireAt;

    public boolean isSignUp;

    public static Finder<String, LocalToken> find = new Finder<String, LocalToken>(
            String.class, LocalToken.class
    );
}

And last but not least my UserService:

package services;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;

import models.LocalToken;
import models.LocalUser;
import play.Application;
import play.Logger;
import scala.Option;
import scala.Some;
import securesocial.core.AuthenticationMethod;
import securesocial.core.Identity;
import securesocial.core.PasswordInfo;
import securesocial.core.SocialUser;
import securesocial.core.UserId;
import securesocial.core.java.BaseUserService;
import securesocial.core.java.Token;

public class SqlUserService extends BaseUserService {

    public SqlUserService(Application application) {
        super(application);
    }

    @Override
    public void doDeleteExpiredTokens() {
        if (Logger.isDebugEnabled()) {
            Logger.debug("deleteExpiredTokens...");
        }
        List<LocalToken> list = LocalToken.find.where().lt("expireAt", new DateTime().toString()).findList();
        for(LocalToken localToken : list) {
            localToken.delete();
        }
    }

    @Override
    public void doDeleteToken(String uuid) {
        if (Logger.isDebugEnabled()) {
            Logger.debug("deleteToken...");
            Logger.debug(String.format("uuid = %s", uuid));
        }
        LocalToken localToken = LocalToken.find.byId(uuid);
        if(localToken != null) {
            localToken.delete();
        }
    }

    @Override
    public Identity doFind(UserId userId) {
        if (Logger.isDebugEnabled()) {
            Logger.debug("find...");
            Logger.debug(String.format("id = %s", userId.id()));
        }
        LocalUser localUser = LocalUser.find.byId(userId.id());
        if(localUser == null) return null;
        SocialUser socialUser = new SocialUser(new UserId(localUser.id, localUser.provider), 
            localUser.firstName, localUser.lastName, String.format("%s %s", localUser.firstName, localUser.lastName),
            Option.apply(localUser.email), null, new AuthenticationMethod("userPassword"),
            null, null, Some.apply(new PasswordInfo("bcrypt", localUser.password, null))
        );  
        if (Logger.isDebugEnabled()) {
            Logger.debug(String.format("socialUser = %s", socialUser));
        }
        return socialUser;
    }


    @Override
    public Identity doFindByEmailAndProvider(String email, String providerId) {
        if (Logger.isDebugEnabled()) {
            Logger.debug("findByEmailAndProvider...");
            Logger.debug(String.format("email = %s", email));
            Logger.debug(String.format("providerId = %s", providerId));
        }
        List<LocalUser> list = LocalUser.find.where().eq("email", email).eq("provider", providerId).findList();
        if(list.size() != 1) return null;
        LocalUser localUser = list.get(0);
        SocialUser socialUser = new SocialUser(new UserId(localUser.id, localUser.provider), 
                localUser.firstName, localUser.lastName, String.format("%s %s", localUser.firstName, localUser.lastName),
                Option.apply(localUser.email), null, new AuthenticationMethod("userPassword"),
                null, null, Some.apply(new PasswordInfo("bcrypt", localUser.password, null))
            );  
            if (Logger.isDebugEnabled()) {
                Logger.debug(String.format("socialUser = %s", socialUser));
            }
            return socialUser;
    }

    @Override
    public Token doFindToken(String token) {
        if (Logger.isDebugEnabled()) {
            Logger.debug("findToken...");
            Logger.debug(String.format("token = %s", token));
        }
        LocalToken localToken = LocalToken.find.byId(token);
        if(localToken == null) return null;
        Token result = new Token();
        result.uuid = localToken.uuid;
        result.creationTime = new DateTime(localToken.createdAt);
        result.email = localToken.email;
        result.expirationTime = new DateTime(localToken.expireAt);
        result.isSignUp = localToken.isSignUp;
        if (Logger.isDebugEnabled()) {
            Logger.debug(String.format("foundToken = %s", result));
        }
        return result;
    }

    @Override
    public Identity doSave(Identity user) {
        if (Logger.isDebugEnabled()) {
            Logger.debug("save...");
            Logger.debug(String.format("user = %s", user));
        }
        LocalUser localUser = null;
        localUser = LocalUser.find.byId(user.id().id());
        if (localUser == null) {
            localUser = new LocalUser();
            localUser.id = user.id().id();
            localUser.provider = user.id().providerId();
            localUser.firstName = user.firstName();
            localUser.lastName = user.lastName();
            localUser.email = user.email().get();
            localUser.password = user.passwordInfo().get().password();
            localUser.save();
        } else {
            localUser.id = user.id().id();
            localUser.provider = user.id().providerId();
            localUser.firstName = user.firstName();
            localUser.lastName = user.lastName();
            localUser.email = user.email().get();
            localUser.password = user.passwordInfo().get().password();
            localUser.update();
        }
        return user;
    }

    @Override
    public void doSave(Token token) {
        if (Logger.isDebugEnabled()) {
            Logger.debug("save...");
            Logger.debug(String.format("token = %s", token.uuid));
        }
        LocalToken localToken = new LocalToken();
        localToken.uuid = token.uuid;
        localToken.email = token.email;
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            localToken.createdAt = df.parse(token.creationTime.toString("yyyy-MM-dd HH:mm:ss"));
            localToken.expireAt = df.parse(token.expirationTime.toString("yyyy-MM-dd HH:mm:ss"));
        } catch (ParseException e) {
            Logger.error("SqlUserService.doSave(): ", e);
        }
        localToken.isSignUp = token.isSignUp;
        localToken.save();
    }
}


来源:https://stackoverflow.com/questions/16093023/play-securesocial-persistance-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!