且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不输入用户名和密码来验证一个移动应用程序?

更新时间:2023-09-28 13:05:22

我也做了以下来实现这一点:

I have done the following to achieve this:

  • 当应用程序第一次启动时,我测试,如果 有一个认证令牌和 如果它仍然是有效的
  • 如果没有,我用[startActivityForResult] [1]打开我的登录活动
  • 在该LoginActivity使用的WebView 并打开验证应用程序从Web应用程序页面(如 https://www.yourdomain.com/authapp )。
  • 如果用户没有登录到Web应用程序,他现在已经做到这一点。成功登录后,他被重定向到验证应用程序页
  • 在验证应用程序页包含文本您想移动应用程序来访问你的数据和许可和取消按钮。
  • 如果用户点击许可的web应用程序generats一个认证令牌,其写入DATABSE并重定向到一个响应页面,所生成的认证令牌附加到URL(例如 https://www.yourdomain.com/authresponse?auth_token=dshf84z4388f4h
  • 该移动应用程序从URL中提取令牌和交谈时,服务器使用它进行认证。

  • When the App first starts, I test if there is an authentication token and if it is still valid
  • If not, I use [startActivityForResult][1] to open my login activity
  • The LoginActivity uses a WebView and opens the "authenticate app" page (e.g. https://www.yourdomain.com/authapp) from the web application.
  • If the user is not logged into the webapp, he has to do this now. Upon successful login, he gets redirected to the "authenticate app" page
  • The "authenticate app" page contains the text "would you like the mobile app to access you data" and a "grant" and "cancel" button.
  • If the user hits "grant" the web app generats a authentication token, writes it to the databse and redirects to a response page, attaching the generated authentication token to the URL (e.g. https://www.yourdomain.com/authresponse?auth_token=dshf84z4388f4h)
  • The mobile application extracts the token from the URL and uses it for authentication when talking to the server.

在WebLogin活动如下:(注:你必须重写shouldOverrideUrlLoading留在同一个web视图,否则,一个新的浏览器打开时,您会收到一些重定向)

The WebLogin Activity looks like this: (note: you have to override "shouldOverrideUrlLoading" to stay in the same WebView. Otherwise, a new browser is open when you receive some redirect)

公共类WebLogin延伸活动{

public class WebLogin extends Activity {

@覆盖 保护无效的onCreate(包savedInstanceState){     super.onCreate(savedInstanceState);

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

WebView webview = new WebView(this);
webview.setWebViewClient(new WebViewClient() {  


@Override  
public boolean shouldOverrideUrlLoading(WebView view, String url){
    view.loadUrl(url);
    return true;  
}  


@Override
public void onPageFinished(WebView view, String url) {


    if(StringUtils.contains(url, "?auth_token=")){


        // extract and save token here


        setResult(RESULT_OK);
        finish();
    }
}

}); webview.loadUrl(https://www.yourdomain.com/authapp); webview.getSettings()setJavaScriptEnabled(真)。 的setContentView(web视图);

}); webview.loadUrl("https://www.yourdomain.com/authapp"); webview.getSettings().setJavaScriptEnabled(true); setContentView(webview);

} }

请注意,我使用https,使这个拯救。如果使用普通的http协议,你可以阅读和窃取用户的令牌。

Note, I use https to make this save. If you use plain http, one could read and steal the token of a user.

[1]:http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, INT)

[1]: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)