【问题标题】:Google App Engine getCurrentUser() returns nullGoogle App Engine getCurrentUser() 返回 null
【发布时间】:2013-01-23 11:25:33
【问题描述】:

我正在开发一个需要访问帐户中所有用户的应用程序。 所有操作均使用管理帐户在 Google Apps for Business 帐户上完成。

一般的想法是在没有任何登录屏幕/URL 的情况下从仪表板访问我的应用程序并检索必要的数据(在这种情况下是用户)。这可能吗?

我不知道我是否必须设置 OAuth(我的应用程序部署在 apppot 上并添加到 GApps 帐户) - 就像我之前所说的,我只想启动我的应用程序,它应该从当前登录的获取登录凭据GApps 用户。

但是获取当前用户返回 null。这是我的 servlet 以及获取当前用户的方法:

public class ExperimentalServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        resp.setContentType("text/plain");
        resp.getWriter().println("User: " + (user == null ? "<null>" : user.getNickname()));
    }

}

appengine-web.xml

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>name-of-my-application</application>
    <threadsafe>true</threadsafe>
    <version>1</version>
</appengine-web-app>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

<servlet>
    <servlet-name>experimental</servlet-name>
    <servlet-class>my.app.domain.ExperimentalServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>experimental</servlet-name>
    <url-pattern>/experimental</url-pattern>
</servlet-mapping>

<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

【问题讨论】:

    标签: java google-app-engine


    【解决方案1】:

    如果 getCurrentUser 返回 null,则表示没有用户登录。

    在这种情况下,您可以自己编写代码以重定向到登录页面 或者您将您的应用配置为所有/某些网址都需要登录。

    然后appengine会自动将用户重定向到登录屏幕,并在成功登录后返回。

    在您的 appengine 应用仪表板中,您可以配置允许哪些用户登录、所有用户或仅允许 Gapps 域中的用户登录。

    如果您将其设置为您的 Gapps 域,并且您配置了您应用中的所有 url 都需要身份验证,那么如果 Gapps 用户进入您的应用,他将自动登录,如果他没有登录他的 Gapps 帐户,将向用户显示登录屏幕。

    这里是如何配置安全网址的链接

    https://developers.google.com/appengine/docs/java/configyaml/appconfig_yaml#Secure_URLs

    【讨论】:

    • 看起来像这样(除了带有重定向的奇怪名称):resp.sendRedirect(userService.createLoginURL(req.getRequestURI())); 解决了这个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2017-05-22
    相关资源
    最近更新 更多