简体   繁体   English

在独立应用程序中使用OpenID

[英]Using OpenID in Standalone Application

Currently, I am using Google Account Verification in my standalone application. 目前,我正在独立应用程序中使用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;
}

May I know is it possible, that I can apply the similar concept on OpenID. 我是否可以在OpenID上应用类似的概念。 I saw there are many web application example using OpenID. 我看到有很多使用OpenID的Web应用程序示例。 However, I haven't seen one in standalone application. 但是,我还没有在独立应用程序中看到一个。

Is it possible to do so? 有可能这样做吗?

Since you're essentially pretending to be a browser and performing HTTP requests, you should be able to easily duplicate an OpenID setup. 由于您本质上是在假装自己是浏览器并执行HTTP请求,因此您应该能够轻松地复制OpenID设置。

Just look at what HTTP requests the web apps do, and replicate that using the same method you posted here, by using an HTTPUrlConnection . 只需查看HTTP请求Web应用程序执行的操作,然后使用HTTPUrlConnection使用您在此处发布的相同方法复制该HTTPUrlConnection

However, one thing I feel I need to mention. 但是,我觉得我需要提一件事。 Since you're not using HTTPS, anyone on your network could be sniffing username and passwords. 由于您没有使用HTTPS,因此您网络上的任何人都可能在嗅探用户名和密码。 You should perform all authentication and authorizations over a secure channel. 您应该通过安全通道执行所有身份验证和授权。

OpenID doesn't say anything about what happens while the user is authenticating to their provider. 当用户向其提供程序进行身份验证时,OpenID不会说什么。 It could be a username/password, it could be a cookie, it could be an SSL certificate, it could be a retinal scan. 它可以是用户名/密码,可以是cookie,可以是SSL证书,可以是视网膜扫描。

Your best bet may be opening a web browser widget and asking the user to authenticate in the normal OpenID style (but you'd have to figure out what to do for lack of a web server). 最好的选择是打开Web浏览器窗口小部件,并要求用户以常规的OpenID样式进行身份验证(但由于缺少Web服务器,您必须弄清楚该怎么做)。

May I know is it possible, that I can apply the similar concept on OpenID. 我是否可以在OpenID上应用类似的概念。 I saw there are many web application example using OpenID. 我看到有很多使用OpenID的Web应用程序示例。 However, I haven't seen one in standalone application. 但是,我还没有在独立应用程序中看到一个。

The OpenID provider you used is just a webservice provided by Google. 你使用的OpenID提供商仅仅是由谷歌提供的Web服务 You can create a webservice yourself as well using a simple servlet and a database hosted online somewhere. 您也可以使用简单的servlet和在线托管在某个地方的数据库来自己创建Web服务。

May I know is it possible, that I can apply the similar concept on OpenID. 我是否可以在OpenID上应用类似的概念。 I saw there are many web application example using OpenID. 我看到有很多使用OpenID的Web应用程序示例。 However, I haven't seen one in standalone application. 但是,我还没有在独立应用程序中看到一个。

Maybe because it doesn't make much sense for a standalone application to use something that rely heavily on web paradigms and HTTP. 可能是因为,对于一个独立的应用程序而言,使用严重依赖于Web范例和HTTP的东西并没有多大意义。 Amongst other things, how are you going to redirect a user to its OpenID provider? 除其他事项外,您将如何将用户重定向到其OpenID提供程序? What about the authentication response (that should come in as a HTTP request from the OpenID provider)? 身份验证响应如何处理(应该作为来自OpenID提供程序的HTTP请求进入)? How to deal with that? 怎么处理呢?

So, no, I don't think you can/should use OpenID for your desktop application (not even mentioning that, if not connected to the web, users wouldn't be able to authenticated but that's another story). 因此,不,我认为您不能/不应该为您的桌面应用程序使用OpenID(更不用说,如果不连接到网络,则用户将无法进行身份验证,但这是另一回事了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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