【问题标题】:Ionic 4 oauth redirect connection refusedIonic 4 oauth 重定向连接被拒绝
【发布时间】:2019-08-02 09:06:26
【问题描述】:

我正在使用 ionic/cordova 构建 Android 和 iOS 应用程序:离子版本是:4.11.0 & Cordova 版本是:8.1.2

我创建了应用程序并在 chrome 上的浏览​​器中进行了测试,整个应用程序运行良好,包括授权、获取数据等。

我的问题始于我创建要测试的 android 应用程序。无论我做什么,我都无法让授权工作流重定向到应用程序。

这是我尝试过的事情:

1- 无论我在 config.xml、cordova web 视图插件和 cordova 自定义 url 方案插件中尝试什么,回调“http/localhost”都会拒绝连接。

2- 无论我尝试修改 config.xml 并使用上面相同的 cordova 插件,回调“myapp://callback”都会给出 err_unkown_url_schema”。

我正在 LGG6 Android 版本 8.0.0 上进行测试。尚未在 iOS 上进行测试。

代码sn-p:

var uniqueWindowId = 'authorization-' + this.client_id
var params = 'titlebar=no, location=yes'
    var loginWindow = window.open(this.authCodeURL, uniqueWindowId, params);

    loginWindow.addEventListener(
      'message',
      e => {
        var oauthMessage = e.data;
        alert(oauthMessage);
        loginWindow.close();
        loginWindow.opener.postMessage(oauthMessage, 'https://localhost/');
      },
      false
    );

这是通过 Android Studio 从设备获取的日志

Connecting to io.ionic.starter
Connected to the target VM, address: 'localhost:8600', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote64: Late-enabling -Xcheck:jni
W/ActivityThread: Application io.ionic.starter is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/zygote64: Debugger is active
I/System.out: Debugger has connected
    waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1419)
I/InstantRun: starting instant run server: is main process
I/CordovaLog: Changing log level to DEBUG(3)
I/CordovaActivity: Apache Cordova native platform version 7.1.4 is starting
D/CordovaActivity: CordovaActivity.onCreate()
I/WebViewFactory: Loading com.android.chrome version 72.0.3626.121 (code 362612152)
I/cr_LibraryLoader: Time to load native libraries: 7 ms (timestamps 8989-8996)
I/chromium: [INFO:library_loader_hooks.cc(42)] Chromium logging enabled: level = 0, default verbosity = 0
I/cr_LibraryLoader: Expected native library version number "72.0.3626.121", actual native library version number "72.0.3626.121"
W/cr_ChildProcLH: Create a new ChildConnectionAllocator with package name = com.android.chrome, sandboxed = true
I/cr_BrowserStartup: Initializing chromium process, singleProcess=false
W/ResourceType: Failure getting entry for 0x7f120538 (t=17 e=1336) (error -2147483647)
D/IonicWebViewEngine: Ionic Web View Engine Starting Right Up 1...
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/SystemWebViewEngine: CordovaWebView is running on device made by: LGE
D/PluginManager: init()
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/OpenGLRenderer: HWUI GL Pipeline
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
I/zygote64: Do partial code cache collection, code=28KB, data=25KB
I/zygote64: After code cache collection, code=27KB, data=25KB
    Increasing code cache capacity to 128KB
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
D/CordovaWebViewImpl: >>> loadUrl(file:///android_asset/www/index.html)
I/AudioManagerEx: AudioManagerEx created
W/cr_media: Requires BLUETOOTH permission
D/CordovaActivity: Started the activity.
I/Adreno: QUALCOMM build                   : 14c0496, Ida6448821d
    Build Date                       : 01/08/18
    OpenGL ES Shader Compiler Version: EV031.22.00.01
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.6.5.R1.08.00.00.312.043
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
D/CordovaActivity: Resumed the activity.
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
W/AudioCapabilities: Unsupported mime audio/x-lg-alac
W/AudioCapabilities: Unsupported mime audio/x-lg-flac
W/AudioCapabilities: Unsupported mime audio/adpcm
W/AudioCapabilities: Unsupported mime audio/g726
W/AudioCapabilities: Unsupported mime audio/lg-wma-voice
W/AudioCapabilities: Unsupported mime audio/x-lg-ms-wma
W/VideoCapabilities: Unsupported mime video/mjpg
W/VideoCapabilities: Unsupported mime video/theora
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
    Unrecognized profile 2130706434 for video/avc
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
    Unrecognized profile 2130706434 for video/avc
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
W/VideoCapabilities: Unrecognized profile 2130706434 for video/avc
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@fdd775d time:801221046
W/VideoCapabilities: Unsupported mime video/dolby-vision
W/VideoCapabilities: Unsupported mime video/dolby-vision
W/VideoCapabilities: Unsupported mime video/mp4v-esdp
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
W/cr_CrashFileManager: /data/user/0/io.ionic.starter/cache/WebView/Crash Reports does not exist or is not a directory
D/CordovaWebViewImpl: onPageDidNavigate(file:///android_asset/www/index.html)
D/CordovaWebViewImpl: onPageFinished(file:///android_asset/www/index.html)
D/SERVER: Handling local request: http://localhost/
D/CordovaWebViewImpl: onPageDidNavigate(http://localhost/)
D/SERVER: Handling local request: http://localhost/cordova.js
D/SERVER: Handling local request: http://localhost/build/main.css
D/SERVER: Handling local request: http://localhost/build/polyfills.js
D/SERVER: Handling local request: http://localhost/build/vendor.js
D/SERVER: Handling local request: http://localhost/build/main.js
D/JsMessageQueue: Set native->JS mode to EvalBridgeMode
D/SERVER: Handling local request: http://localhost/cordova_plugins.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-device/www/device.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-inappbrowser/www/inappbrowser.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-ionic-keyboard/www/android/keyboard.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-ionic-webview/src/www/util.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-splashscreen/www/splashscreen.js
I/zygote64: Do partial code cache collection, code=61KB, data=56KB
    After code cache collection, code=61KB, data=56KB
    Increasing code cache capacity to 256KB
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-statusbar/www/statusbar.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-sqlite-storage/www/SQLitePlugin.js
D/SystemWebChromeClient: http://localhost/build/vendor.js: Line 4325 : Angular is running in the development mode. Call enableProdMode() to enable the production mode.
I/chromium: [INFO:CONSOLE(4325)] "Angular is running in the development mode. Call enableProdMode() to enable the production mode.", source: http://localhost/build/vendor.js (4325)
D/SystemWebChromeClient: http://localhost/build/main.js: Line 998 : ################################################################################ home.ts constructor
I/chromium: [INFO:CONSOLE(998)] "################################################################################ home.ts constructor", source: http://localhost/build/main.js (998)
D/SERVER: Handling local request: http://localhost/assets/icon/icon.png
D/SERVER: Handling local request: http://localhost/assets/fonts/ionicons.woff2?v=3.0.0-alpha.3
D/SERVER: Handling local request: http://localhost/assets/fonts/roboto-medium.woff2
D/SERVER: Handling local request: http://localhost/assets/fonts/roboto-regular.woff2
D/SystemWebChromeClient: http://localhost/build/vendor.js: Line 140284 : Ionic Native: deviceready event fired after 1716 ms
I/chromium: [INFO:CONSOLE(140284)] "Ionic Native: deviceready event fired after 1716 ms", source: http://localhost/build/vendor.js (140284)
I/App: WARNING: Back Button Default Behavior will be overridden.  The backbutton event will be fired!
D/CordovaWebViewImpl: onPageFinished(http://localhost/)
D/SERVER: Handling local request: http://localhost/assets/icon/favicon.ico
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/zygote64: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
I/zygote64: Do full code cache collection, code=123KB, data=107KB
I/zygote64: After code cache collection, code=94KB, data=64KB
D/InAppBrowser: target = _self
D/InAppBrowser: in self
D/InAppBrowser: isUrlWhiteListed [class java.lang.String]
D/InAppBrowser: loading in InAppBrowser
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
I/zygote64: Do partial code cache collection, code=118KB, data=94KB
    After code cache collection, code=118KB, data=94KB
    Increasing code cache capacity to 512KB
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/AssistStructure: Flattened final assist data: 520 bytes, containing 1 windows, 2 views
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP

我在屏幕上得到的信息是:

the webpage at http://localhost/?code=......@state=..... could nopt be loaded becuase:

net::ERR_CONNECTION_REFUSED

【问题讨论】:

  • 您能否提供一些代码 sn-ps 以及可能的完整错误堆栈跟踪?
  • 你是绝对正确的,我应该从一开始就提供代码和日志。道歉。过去 4 天一直在努力解决这个问题,精神疲惫。
  • 那么您是否尝试通过应用程序外部的 url 进行身份验证?
  • 是的。正如 oauth2 的惯例一样。正如我们对 google 和其他 oauth2 服务所做的那样。

标签: cordova redirect oauth-2.0 cordova-plugins ionic4


【解决方案1】:

如果您通过应用程序外部的 url 进行 oauth,我建议您使用 inappbrowser 离子原生插件。

https://ionicframework.com/docs/native/in-app-browser/

你试过吗?一般来说,这种使用 inapp 浏览器作为 oauth 的方法正在成为一种反模式,随着 Ionic Capacitor 的发展,他们没有使用 inappbrowser,而是围绕 iOS 的 SafariViewController 和 Android 的 Chrome 自定义选项卡实现了一个解决方案,但这仍然非常在我看来,大部分都处于测试阶段。

https://capacitor.ionicframework.com/docs/apis/browser/

这里有一篇关于使用 inappbrowser 的 OAuth 的文章有些过时,但它很好地涵盖了插件的基本使用:

https://www.thepolyglotdeveloper.com/2016/01/using-an-oauth-2-0-service-within-an-ionic-2-mobile-app/

关于它为何成为反模式的文章: https://medium.com/@jlchereau/stop-using-inappbrowser-for-your-cordova-phonegap-oauth-flow-a806b61a2dc5

就您的具体问题而言,我还会检查控制台日志,并确保这不是 CORS 问题,还要检查您在 config.xml 中的导航白名单。

【讨论】:

  • inappbrowser 已在 ios 上更新以使用 WKWebview,并且我已成功完成 iOS 上的 OAuth 流程。 Android 在重定向方面很不稳定,试图调试它。
猜你喜欢
  • 2023-03-21
  • 2017-08-08
  • 2018-08-13
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 2018-08-20
相关资源
最近更新 更多