有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Google应用程序引擎getCurrentUser()返回null

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

一般的想法是访问我的应用程序,而无需从仪表板上获取任何登录屏幕/URL,并检索必要的数据(在本例中为用户)。可能吗

我不知道我是否必须设置OAuth(我的应用程序部署在appspot上,并添加到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网站。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>

网络。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>

共 (1) 个答案

  1. # 1 楼答案

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

    在这种情况下,您可以自己编写代码,重定向到登录页面 或者,您可以配置应用程序,使所有/部分URL都需要登录

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

    在appengine应用程序仪表板中,您可以配置允许哪些用户登录、全部登录或仅允许来自Gapps域的用户登录

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

    下面是如何配置安全URL的链接

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