自定义用户登录AppEngine

时间:2015-04-08 11:41:41

标签: login gae-userservice

我有一个Java Servlet后端,其数据存储连接到我的应用程序;我正在尝试使用Android Studio LoginActivity模板实现登录系统,使用用户的电子邮件和密码(不是处理Google帐户登录的PlusBaseActivity),但我不知道如何从这里继续:

怎么说用户已登录?以及如何使用我的数据存储区持久保存?我在这里读到:How to login User using UserService on AppEngine Java我只需要调用方法resp.sendRedirect(userService.createLoginURL(req.getRequestURI())),我已经这样做了:

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      int size = checkDatastore(); // 0 if empty, > 0 if not empty
      if(size==0){
          populateDatastore();
      }

      String asyncMessage = req.getParameter("order");

      if(asyncMessage.equals("login")){
          mail = req.getParameter("email");
          psw = req.getParameter("password");

          UserService userService = UserServiceFactory.getUserService();
          User user = userService.getCurrentUser();

          String message="";
          resp.setContentType("text/plain");
          PrintWriter out = resp.getWriter();
          if(user == null) {

             //Sends a temporary redirect response to the client using the
             // specified redirect location URL and clears the buffer.
             String uri = userService.createLoginURL(req.getRequestURI());
             resp.sendRedirect(uri);
             User user1 = userService.getCurrentUser();
             message="No one is logged in!\n" + "Sent from App Engine at " + new Date();

            out.println(message);
            out.flush();
        }if(user !=null) {
            //  login(user);
            message = "Hello, " + user.getEmail() +
                    ", "+user.getNickname()+"!" + "\nSent from App Engine at "+ new Date();

            out.println(message);
            out.flush();
        }
    }
}

sendRedirect()方法只给我一个URI。做什么的?

此外,User user = userService.getCurrentUser()始终返回null。怎么样?

1 个答案:

答案 0 :(得分:1)

那是因为resp.sendRedirect(userService.createLoginURL(req.getRequestURI())) UserService仅在将登录与Google帐户集成时才有效,如this文档中所示。 如果要实现个性化登录系统,可以通过多种方式实现。当然,你需要一个Servlet检查新用户'数据和数据存储,以持久存储新帐户注册。