GAE / J - 联合认证&无限递归

时间:2012-03-18 03:43:47

标签: java google-app-engine

我正在使用http://code.google.com/appengine/articles/openid.html提供的说明在App Engine中启用联合身份验证。我无法解决说明中提到的“无限递归”问题,有人可以帮我理解如何解决这个问题。

我的web.xml如下所示:

    <security-constraint>
    <web-resource-collection>
        <web-resource-name></web-resource-name>
        <url-pattern>/mylogin.jsp</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>


<servlet>
    <servlet-name>LoginRequiredServlet</servlet-name>
    <servlet-class>com.jagdish.LoginRequiredServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>LoginRequiredServlet</servlet-name>
    <url-pattern>/_ah/login_required</url-pattern>
</servlet-mapping>

Google群组http://groups.google.com/group/google-appengine/browse_thread/thread/96c4248f5d289fba?pli=1上发布了解决方案,但我无法理解如何实施。

我是否需要通过删除<url-pattern>/_ah/login_required</url-pattern>

来在web.xml中处理此问题

我试过了,因为我无法部署应用程序而无法正常工作。

或者我需要处理“LoginRequiredServlet”这里是我的LoginRequiredServlet代码。

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class LoginRequiredServlet extends HttpServlet {

    private static final Map<String, String> openIdProviders;
    static {
        openIdProviders = new HashMap<String, String>();
        openIdProviders.put("Google", "www.google.com/accounts/o8/id");
        openIdProviders.put("Yahoo", "yahoo.com");
        openIdProviders.put("MySpace", "myspace.com");
        openIdProviders.put("AOL", "aol.com");
        openIdProviders.put("MyOpenId.com", "myopenid.com");
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser(); // or req.getUserPrincipal()
        Set<String> attributes = new HashSet();

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        if (user != null) {
            out.println("Hello <i>" + user.getNickname() + "</i>!");
            out.println("[<a href=\""
                    + userService.createLogoutURL(req.getRequestURI())
                    + "\">sign out</a>]");
        } else {
            out.println("Hello world! Sign in at: ");
            for (String providerName : openIdProviders.keySet()) {
                String providerUrl = openIdProviders.get(providerName);
                String loginUrl = userService.createLoginURL(req
                        .getRequestURI(), null, providerUrl, attributes);
                out.println("[<a href=\"" + loginUrl + "\">" + providerName + "</a>] ");
            }
        }
    }
}

请帮我解决这个递归问题,谢谢。

1 个答案:

答案 0 :(得分:0)

这就是我解决这个问题的方法 -

在LoginRequiredServlet.java中我正在检查

String returnURL = req.getParameter("continue");

我在String returnURL中获取返回URL。我使用该url,然后创建

loginUrl = userService.createLoginURL(returnURL, null, providerUrl, attributes);