简体   繁体   English

使用GAE数据存储进行身份验证时,实体用户返回null

[英]Authenticating with GAE Datastore return null on Entity User

I'm trying to create my own authentication. 我正在尝试创建自己的身份验证。 Everytime I try to login, and the username is not found in the GAE datastore, I get to have INTERNAL_SERVER_ERROR. 每次尝试登录时,在GAE数据存储区中都找不到用户名时,我会遇到INTERNAL_SERVER_ERROR。

It says that: 它说:

java.lang.NullPointerException
at com.pawnsoftware.User.authenticate(User.java:16)
at com.pawnsoftware.UserLoginServlet.doPost(UserLoginServlet.java:24)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)

and so on... 等等...

How do you avoid having this error? 您如何避免出现此错误?

The error is on the line that says: 错误在显示以下内容的行上:

if (user==null) {
    message = "Username not found.";
}

Authenticate User: 验证用户:

public static String authenticate(String username, String password) {
    String message;
    Entity user = UserUtil.findUserEntity(username);
    String pass = user.getProperty("password").toString();
    if (user==null) {
        message = "Username not found.";
    } else if (user!=null && !password.equals(pass)) {
        message = "Password is incorrect.";
    } else if (user!=null && password.equals(pass)) {
        message = "Successfully logged in!";
    } else {
        message = "Sorry, cannot find the username and password.";
    } 
    return message;
}

Find User Entity: 查找用户实体:

public static Entity findUserEntity (String username) {
    Key userKey = KeyFactory.createKey("User", username);
    try {
      return datastore.get(userKey);
    } catch (EntityNotFoundException e) {
      return null;
    }
}

Update on authentication: 认证更新:

public static String authenticate(String username, String password) {
    String message;
    Entity user = UserUtil.findUserEntity(username);
    password = encrypt(password);
    String pass = "";   
        try {
            pass = user.getProperty("password").toString();
        } catch (NullPointerException e) {
            message = "Username not found.";
        }
    if (user==null) {
        message = "Username not found.";
    } else if (user!=null && !password.equals(pass)) {
        message = "Password is incorrect.";
    } else if (user!=null && password.equals(pass)) {
        message = "Successfully logged in!";
    } else {
        message = "Sorry, cannot find the username and password.";
    } 
    return message;
}

如果use为null,则此行将失败:

String pass = user.getProperty("password").toString();

You don't display if you get get a EntityNotFoundException and return null . 如果得到EntityNotFoundException并返回null则不显示。 This, in turn, can cause a NullPointerException for the variable user : 反过来,这可能导致变量user出现NullPointerException

user.getProperty("password").toString();

You could add a guard statement here: 您可以在此处添加保护声明:

if (user != null) {
   pass = user.getProperty("password").toString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GAE数据存储实体关系 - GAE Datastore Entity relationship 私人消息的GAE数据存储实体 - GAE Datastore entity for private messages Google 数据存储区实体不返回 ID。 实体 ID 为 null - Google Datastore Entity does not return ID. Entity ID is null 并发写入 GAE 数据存储实体的不同属性 - Concurrent writes to different properties of GAE Datastore Entity 如何在GAE数据存储区中将实体属性定义为唯一? - How to define an entity property, in GAE datastore, to be unique? GAE数据存储区保存了一个实体,然后不使用Java / Objectify对其进行更新 - GAE Datastore saves an entity then doesn't update it using Java / Objectify 在不使用PersistantManager的情况下从GAE DataStore中的实体获取特定值 - Fetching particular value from entity in GAE DataStore without PersistantManager GAE数据存储区:仅在多次调用delete()之后才删除实体 - GAE datastore: Entity deleted only after multiple calls to delete() 带有JPA和REST的GAE:使用数据存储区生成实体(如json或xml)。 - GAE with JPA and REST: Produce Entity (as json or xml) with the datastore.Key 为什么我的GAE数据存储区查询返回的游标为空? - Why is the cursor returned from my GAE datastore query null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM