简体   繁体   中英

Google App Engine getCurrentUser() returns null

I'm developing an application which requires access to all users in the account. All operations are done on the Google Apps for Bussiness account using administrative account.

The general idea is to access my application without any login screens/URLs from the dashboard and retrieve necessary data (users in this case). Is that possible?

I don't know if I have to set OAuth (my app is deployed on the appspot and added to the GApps account) - like I said earlier I just want to launch my app and it should get login credentials from currently logged GApps user.

However getting current user returns null. This is my servlet with method for getting current user:

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>

if getCurrentUser returns null, then there is no User logged in.

in that case you can either code it by your own to redirect to a login page or you configure your app that login is needed for all/some urls.

then appengine will automatic redirect the user to login screen, and back after successfull login.

in your appengine app dashboard you can configure which users are allowed to login, all, or just the one from your Gapps domain.

if you set it to your Gapps domain, and you configured that all urls in your app needs authentication, then if a Gapps user enters your app, he will be logged in automatic, if he is not logged in in his Gapps account, login screen will shown to the user.

here is the link how to configure secure urls

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM