开发者

Using open ID with Google app engine

开发者 https://www.devze.com 2023-02-01 17:11 出处:网络
I have an app running on Google app engine.I want to change the sign-in from using google, to using open id, in Java.

I have an app running on Google app engine. I want to change the sign-in from using google, to using open id, in Java.

So, I tried following this guide:

http://code.google.com/appengine/articles/openid.html

  1. I changed the authentication to federated login on the application settings dashboard.

  2. I put this in the web.xml:

    <servlet>
        <servlet-name>LoginRequiredServlet</servlet-name>
        <servlet-class>XXXXXX.server.LoginRequiredServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginRequiredServlet</servlet-name>
        <url-pattern>/_ah/login_required</url-pattern>
    </servlet-mapping>
    
  3. I created a class called LoginRequiredServlet, with the code that is on the above link. I renamed their class OpenIdDemoServlet to LoginRequiredServlet.

  4. 开发者_StackOverflow社区
  5. I redeployed. Upon going to my landing page, it only takes me a google login page. What exactly do I have to do to so it can show a few links to other OpenID providers?

What am I missing?

Thanks.


Try this, I'm quoting http://code.google.com/appengine/articles/openid.html except that I have changed the class name from OpenIdDemoServlet to LoginRequiredServlet.

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

private static final Map<String, String> openIdProviders;
static {
    openIdProviders = new HashMap<String, String>();
    openIdProviders.put("Google", "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>] ");
        }
    }
}

}

Also update your web.xml so as to force logging in. the code below will force logging for all urls on the website.

0

精彩评论

暂无评论...
验证码 换一张
取 消