简体   繁体   English

如何在 WKWebView 中支持授权令牌?

[英]How to support authorization token in WKWebView?

I have native iOS app and one of the flows of the app should be done with WebView.我有本机 iOS 应用程序,应用程序的流程之一应该使用 WebView 完成。 From native part of the app, user can navigate to WebView part.从应用程序的本机部分,用户可以导航到 WebView 部分。 And, somehow, web page should identify user.而且,不知何故,web 页面应该识别用户。 I have authorization token stored in the app and of course I can pass that token in the headers of the WKWebView.我在应用程序中存储了授权令牌,当然我可以在 WKWebView 的标头中传递该令牌。 And other stuff will be handle in the web (routing and etc).其他内容将在 web(路由等)中处理。 But is it a good and secure way of doing this?但这是一种很好且安全的方法吗? How can I easily integrate WebView in the app caring about token?如何在关心令牌的应用程序中轻松集成WebView?

There are a few options here:这里有几个选项:

  1. Using headers seems problematic according to this thread but hopefully you can get it to work.根据这个线程,使用标题似乎有问题,但希望你能让它工作。 It feels like this will have reliability problems if the token ever expires in the web view, so you'll need to manage that.如果令牌在 web 视图中过期,感觉这将存在可靠性问题,因此您需要对其进行管理。

  2. Simple option: open a system browser - either Safari or a Safari View Controller.简单选项:打开系统浏览器 - Safari 或 Safari 查看 Controller。 The user may have to sign in again though, which your stakeholders may not like.但是,用户可能必须再次登录,您的利益相关者可能不喜欢。

  3. More complex option: use the Javascript API to pass the token from the mobile UI to the web UI.更复杂的选项:使用 Javascript API 将令牌从移动 UI 传递到 web UI。 This will give you full control, and the web app can call back the mobile app to refresh its token.这将为您提供完全控制权,并且 web 应用程序可以回调移动应用程序以刷新其令牌。 It can be the best usability option if used sparingly.如果谨慎使用,它可能是最好的可用性选项。 It requires tricky foundational work in both the web and mobile UIs though.不过,它需要在 web 和移动 UI 中进行棘手的基础工作。

SECURITY安全

Passing the token from the Mobile UI to the Web UI is natural if both are part of the same logical application and access the same level of data.如果两者都是同一个逻辑应用程序的一部分并且访问相同级别的数据,那么将令牌从 Mobile UI 传递到 Web UI 是很自然的。 In this case option 1 or 3 would work.在这种情况下,选项 1 或 3 会起作用。

If the apps have very different security levels (eg the web app is now getting a much higher privilege token than it usually gets), then I would not pass the token and would use option 2 instead.如果应用程序具有非常不同的安全级别(例如,web 应用程序现在获得的特权令牌比它通常获得的要高得多),那么我不会传递令牌,而是使用选项 2。

FURTHER DETAILS更多详细信息

I wrote a quite detailed blog post on considerations a while back, and there is also a code sample you can run:不久前,我写了一篇关于注意事项的非常详细的博客文章,还有一个您可以运行的代码示例:

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

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