Java 在独立应用程序中使用OpenID

Java 在独立应用程序中使用OpenID,java,openid,Java,Openid,目前,我正在我的独立应用程序中使用Google帐户验证 public static boolean isValidAccount(String email, String password) { if (email == null || password == null) { return false; } try { // URL of target page script. final URL url = new UR

目前,我正在我的独立应用程序中使用Google帐户验证

public static boolean isValidAccount(String email, String password) {
    if (email == null || password == null) {
        return false;
    }

    try {
        // URL of target page script.
        final URL url = new URL("https://www.google.com/accounts/ClientLogin");
        final URLConnection urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // Send POST output.
        final DataOutputStream cgiInput = new DataOutputStream(urlConn.getOutputStream());
        // http://code.google.com/apis/base/faq_gdata.html#clientlogin
        String content = "accountType=" + URLEncoder.encode("HOSTED_OR_GOOGLE") + "&Email=" + URLEncoder.encode(email) + "&Passwd=" + URLEncoder.encode(password) + "&service=" + URLEncoder.encode("mail") + "&source=" + URLEncoder.encode("JStock-1.05b");

        cgiInput.writeBytes(content);
        cgiInput.flush();
        cgiInput.close();
        if (urlConn instanceof HttpURLConnection) {
            // 200 means OK. You OK. I OK. Google OK.
            return ((HttpURLConnection) urlConn).getResponseCode() == 200;
        }
        else {
            return false;
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
我想知道,我是否可以在OpenID上应用类似的概念。我看到有许多使用OpenID的web应用程序示例。然而,我还没有在独立应用程序中看到一个


有可能这样做吗?

因为您实际上是在假装是一个浏览器并执行HTTP请求,所以您应该能够轻松地复制OpenID设置

只需查看web应用程序的HTTP请求,并使用您在此处发布的相同方法,通过使用
HTTPUrlConnection
复制这些请求


然而,有一件事我觉得我需要提及。由于您没有使用HTTPS,网络上的任何人都可能会嗅探用户名和密码。您应该通过安全通道执行所有身份验证和授权

由于您实际上是在假装是一个浏览器并执行HTTP请求,因此您应该能够轻松地复制OpenID设置

只需查看web应用程序的HTTP请求,并使用您在此处发布的相同方法,通过使用
HTTPUrlConnection
复制这些请求


然而,有一件事我觉得我需要提及。由于您没有使用HTTPS,网络上的任何人都可能会嗅探用户名和密码。您应该通过安全通道执行所有身份验证和授权

OpenID没有说明当用户向其提供商进行身份验证时会发生什么。它可能是用户名/密码,可能是cookie,可能是SSL证书,可能是视网膜扫描


您最好的选择可能是打开一个web浏览器小部件,并要求用户以正常的OpenID样式进行身份验证(但您必须找出在缺少web服务器的情况下该怎么办)。

OpenID没有说明用户向其提供商进行身份验证时会发生什么情况。它可能是用户名/密码,可能是cookie,可能是SSL证书,可能是视网膜扫描

您最好的选择可能是打开一个web浏览器小部件,并要求用户以正常的OpenID样式进行身份验证(但您必须考虑在缺少web服务器的情况下该怎么做)

我想知道,我是否可以在OpenID上应用类似的概念。我看到有许多使用OpenID的web应用程序示例。然而,我还没有在独立应用程序中看到一个

public static boolean isValidAccount(String email, String password) {
    if (email == null || password == null) {
        return false;
    }

    try {
        // URL of target page script.
        final URL url = new URL("https://www.google.com/accounts/ClientLogin");
        final URLConnection urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // Send POST output.
        final DataOutputStream cgiInput = new DataOutputStream(urlConn.getOutputStream());
        // http://code.google.com/apis/base/faq_gdata.html#clientlogin
        String content = "accountType=" + URLEncoder.encode("HOSTED_OR_GOOGLE") + "&Email=" + URLEncoder.encode(email) + "&Passwd=" + URLEncoder.encode(password) + "&service=" + URLEncoder.encode("mail") + "&source=" + URLEncoder.encode("JStock-1.05b");

        cgiInput.writeBytes(content);
        cgiInput.flush();
        cgiInput.close();
        if (urlConn instanceof HttpURLConnection) {
            // 200 means OK. You OK. I OK. Google OK.
            return ((HttpURLConnection) urlConn).getResponseCode() == 200;
        }
        else {
            return false;
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
您使用的OpenID提供程序只是Google提供的一个Web服务。您也可以使用一个简单的servlet和一个在线托管的数据库自己创建一个Web服务

我想知道,我是否可以在OpenID上应用类似的概念。我看到有许多使用OpenID的web应用程序示例。然而,我还没有在独立应用程序中看到一个

public static boolean isValidAccount(String email, String password) {
    if (email == null || password == null) {
        return false;
    }

    try {
        // URL of target page script.
        final URL url = new URL("https://www.google.com/accounts/ClientLogin");
        final URLConnection urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // Send POST output.
        final DataOutputStream cgiInput = new DataOutputStream(urlConn.getOutputStream());
        // http://code.google.com/apis/base/faq_gdata.html#clientlogin
        String content = "accountType=" + URLEncoder.encode("HOSTED_OR_GOOGLE") + "&Email=" + URLEncoder.encode(email) + "&Passwd=" + URLEncoder.encode(password) + "&service=" + URLEncoder.encode("mail") + "&source=" + URLEncoder.encode("JStock-1.05b");

        cgiInput.writeBytes(content);
        cgiInput.flush();
        cgiInput.close();
        if (urlConn instanceof HttpURLConnection) {
            // 200 means OK. You OK. I OK. Google OK.
            return ((HttpURLConnection) urlConn).getResponseCode() == 200;
        }
        else {
            return false;
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
您使用的OpenID提供程序只是Google提供的一个Web服务。您也可以使用一个简单的servlet和一个在线托管的数据库自己创建一个Web服务

我想知道,我是否可以在OpenID上应用类似的概念。我看到有许多使用OpenID的web应用程序示例。然而,我还没有在独立应用程序中看到一个

public static boolean isValidAccount(String email, String password) {
    if (email == null || password == null) {
        return false;
    }

    try {
        // URL of target page script.
        final URL url = new URL("https://www.google.com/accounts/ClientLogin");
        final URLConnection urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // Send POST output.
        final DataOutputStream cgiInput = new DataOutputStream(urlConn.getOutputStream());
        // http://code.google.com/apis/base/faq_gdata.html#clientlogin
        String content = "accountType=" + URLEncoder.encode("HOSTED_OR_GOOGLE") + "&Email=" + URLEncoder.encode(email) + "&Passwd=" + URLEncoder.encode(password) + "&service=" + URLEncoder.encode("mail") + "&source=" + URLEncoder.encode("JStock-1.05b");

        cgiInput.writeBytes(content);
        cgiInput.flush();
        cgiInput.close();
        if (urlConn instanceof HttpURLConnection) {
            // 200 means OK. You OK. I OK. Google OK.
            return ((HttpURLConnection) urlConn).getResponseCode() == 200;
        }
        else {
            return false;
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
可能是因为对于一个独立的应用程序来说,使用严重依赖于web范例和HTTP的东西没有多大意义。除此之外,您将如何将用户重定向到其OpenID提供程序?身份验证响应(应该作为来自OpenID提供者的HTTP请求)如何?如何处理

因此,不,我认为你不能/不应该在桌面应用程序中使用OpenID(甚至没有提到,如果没有连接到web,用户将无法进行身份验证,但这是另一回事)

我想知道,我是否可以在OpenID上应用类似的概念。我看到有许多使用OpenID的web应用程序示例。然而,我还没有在独立应用程序中看到一个

public static boolean isValidAccount(String email, String password) {
    if (email == null || password == null) {
        return false;
    }

    try {
        // URL of target page script.
        final URL url = new URL("https://www.google.com/accounts/ClientLogin");
        final URLConnection urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // Send POST output.
        final DataOutputStream cgiInput = new DataOutputStream(urlConn.getOutputStream());
        // http://code.google.com/apis/base/faq_gdata.html#clientlogin
        String content = "accountType=" + URLEncoder.encode("HOSTED_OR_GOOGLE") + "&Email=" + URLEncoder.encode(email) + "&Passwd=" + URLEncoder.encode(password) + "&service=" + URLEncoder.encode("mail") + "&source=" + URLEncoder.encode("JStock-1.05b");

        cgiInput.writeBytes(content);
        cgiInput.flush();
        cgiInput.close();
        if (urlConn instanceof HttpURLConnection) {
            // 200 means OK. You OK. I OK. Google OK.
            return ((HttpURLConnection) urlConn).getResponseCode() == 200;
        }
        else {
            return false;
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
可能是因为对于一个独立的应用程序来说,使用严重依赖于web范例和HTTP的东西没有多大意义。除此之外,您将如何将用户重定向到其OpenID提供程序?身份验证响应(应该作为来自OpenID提供者的HTTP请求)如何?如何处理


所以,不,我认为你不能/不应该在桌面应用程序中使用OpenID(甚至没有提到,如果没有连接到web,用户将无法进行身份验证,但这是另一回事)。

?OpenID不是谷歌提供的Web服务。OpenID是一个开放协议,您可以使用任何您想要的OpenID提供程序。你在说什么?关于他正在使用的东西。而不是OpenID本身。URL背后的东西实际上是OpenID提供者。好吧,Google帐户验证服务不是OpenID提供者,它与OpenID不同,OpenID不依赖web服务,所以不,不是真的:)啊,好的,我现在明白你的意思了。我曲解了这个计划,嗯?OpenID不是谷歌提供的Web服务。OpenID是一个开放协议,您可以使用任何您想要的OpenID提供程序。你在说什么?关于他正在使用的东西。而不是OpenID本身。URL背后的东西实际上是OpenID提供者。好吧,Google帐户验证服务不是OpenID提供者,它与OpenID不同,OpenID不依赖web服务,所以不,不是真的:)啊,好的,我现在明白你的意思了。我误解了OP。