2013-01-23 132 views
1

我正在開發一個應用程序,它需要訪問帳戶中的所有用戶。 所有操作均使用管理帳戶在Google Apps for Bussiness帳戶中完成。Google App Engine getCurrentUser()返回null

總體思路是從儀表板訪問我的應用程序,不需要任何登錄屏幕/ 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> 

的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> 

回答

3

如果getCurrentUser返回空值,那麼就沒有用戶登錄。

在這種情況下,您可以通過自己的代碼將其重定向到登錄頁面 或者你配置你的應用程序,所有/某些url需要登錄。

然後appengine會自動將用戶重定向到登錄屏幕,並在成功登錄後返回。

在您的appengine應用程序儀表板中,您可以配置允許哪些用戶登錄,全部或僅從Gapps域中登錄。

如果您將其設置爲您的Gapps域,並且您已配置應用中的所有網址需要身份驗證,那麼如果Gapps用戶輸入您的應用,他將自動登錄,如果他沒有登錄到他的Gapps帳戶,登錄屏幕將顯示給用戶。

這裏是鏈接如何配置安全網址

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

+1

貌似這個(除了重定向怪異名稱):'resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));解決了這個問題。謝謝! –