gpt4 book ai didi

java - 使用 Java 实现安全的社交持久性

转载 作者:搜寻专家 更新时间:2023-11-01 04:03:02 26 4
gpt4 key购买 nike

我开始使用 play 2.1 for java 和 SecureSocial。目前,我正在尝试向注册/注册该站点的用户添加一些持久性,但我在尝试将使用 SecureSocial 将其示例中的数据保存/检索数据表示为持久性模型时遇到了问题。

网页上还说我们可以实现 SocialUser 对象,但我真的不知道如何使用它,因为它是一个 Scala 对象。我如何调用或扩展该对象的示例会很好。

提前致谢。

最佳答案

我正在使用 ebean 来拯救用户。

我的用户类是这样的:

封装模型;

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;
}
}

我的 token 类:

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
);
}

最后但同样重要的是我的 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();
}
}

关于java - 使用 Java 实现安全的社交持久性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com