gpt4 book ai didi

java - Google App Engine getCurrentUser() 返回 null

转载 作者:行者123 更新时间:2023-11-29 05:49:29 24 4
gpt4 key购买 nike

我正在开发一个需要访问帐户中所有用户的应用程序。所有操作均使用管理帐户在 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-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>

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

最佳答案

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

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

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

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

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

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

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

关于java - Google App Engine getCurrentUser() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14478554/

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