[英]Amazon Cognito: How to stop getting "redirect_mismatch" error when redirecting from browser to Android app
我正在嘗試創建一個 Android 項目,在該項目中,我通過讓用戶在瀏覽器中登錄 Amazon Cognito 來授權用戶,然后瀏覽器應該重定向回我的應用程序。 不幸的是,當瀏覽器打開時,我沒有到達正確的登錄頁面,而是不斷收到此錯誤:
在我的 AuthenticatorActivity.java 中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authenticator);
Uri authzUrl = Uri.parse("https://<myDomain>.auth.us-west-2.amazoncognito.com/login?response_type=token&client_id=<myClientId>&redirect_uri=myapp://mainAct");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, authzUrl);
startActivity(launchBrowser);
}
在安卓清單中:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="mainAct" android:scheme="myapp"></data>
</intent-filter>
</activity>
我不知道我在這里做錯了什么。 我錯過了一步嗎?
好的,我要在這里為下一個可能會找到它的人留下一條小道消息。 我遇到了完全相同的問題,但作為 Cognito 和 IdP/SSO 的新手,我不知道如何解決這個問題。 這是我為最終解決此問題所做的工作。 我們正在與外部服務集成,我們收到了這個錯誤。 在 Chrome Developer Tools -> Network 下,我開始記錄訪問過的 URL,然后我再次嘗試了 SSO 集成。 列表中顯示了一個 URL,它通過重定向到 URL 訪問了 Cognito。 該 URL 必須與 Cognito 的回調 URL 下列出的 URL 相同。
希望這可以在將來為某人節省一些時間。
擴展Dimitris的回答https://stackoverflow.com/a/60456018/6883773
如果您為負載均衡器指定了 DNS route53。 您可以在回調 URL 中指定相同的內容。
https://www.example.com/oauth2/idpresponse
參考: https : //aws.amazon.com/premiumsupport/knowledge-center/elb-configure-alb-authentication-idp/
我正在使用 cognito 放大並遇到此錯誤。 通過以下修復。 在 aws-export.ts 中,有一個 redirectSingIn url,它必須與應用程序運行的 cognito/app Integration/app 客戶端設置/回調 url 中的 url 完全相同。
更新:我在 AWS cognito、用戶池、App 客戶端、客戶端 Web 中再次遇到此問題。 更新回調 URL 后,事情開始中斷,即使回調 URL 是有效的。 后來我發現更改同步需要一些時間。需要走開大約 10 分鍾,然后再試一次。
我犯的另一個愚蠢的錯誤是我花了幾個小時才弄明白的事實是aws-exports.js
的redirectSignIn
值完全錯誤。 當您通過 Amplify CLI 多次修改此配置的值時,它會附加一個逗號,將該值視為一個列表,為您提供如下內容
"redirectSignIn": "http://localhost:3000/,http://localhost:3000/,http://localhost:3000/,http://localhost:3000/",
不幸的是,當使用 HostedUI 時,該值被視為字符串。
在我的情況下,錯誤是由於 CloudFront 提供舊文件。
解決它; 您可以通過 AWS 控制台使 CloudFront 文件無效。 ps 可以使用/*
使所有文件無效https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html
我通過記住在前端的 callbackUrl 中包含http://
來解決這個問題。
const redirect_url=`${window.location.origin}`;
這可能不是一個常見的原因,但這就是我的壞了的原因。
這是因為redirectSignIn 或redirectSignOut 的url 不匹配。 請檢查 aws 控制台中的設置和代碼 aws_config,並使它們保持一致。
在我的情況下是因為在控制台中我重定向到https://localhost:4200
但在 URL 中我有http://localhost:4200
。 注意SSL/TLS
版本與non-SSL/TLS
假設您的網站位於應用程序負載均衡器 (ALB) 之后,並且您有一個使用 Cognito 用戶池的偵聽器規則,並且 IF 規則語句中的路徑為 * ,您應該配置您的 0Auth 客戶端應用程序回調 url,例如:
https://<your-ALB-DNS>/oauth2/idpresponse
這至少對我有用,沒有其他花哨的配置。
請記住,這只會在 ALB 后面的任何內容之上提供一個層。 如果你有一些額外的身份驗證方法,你也必須配置它。
如果您請求范圍,您絕對需要確保檢查這些項目,否則您會收到redirect_mismatch
(無用的錯誤名稱)。
使用教程here中的以下配置
Auth.configure({
oauth: {
domain: aws.idpDomain,
scope: ['email', 'openid'],
// we need the /autologin step in between to set the cookies properly,
// we don't need that when signing out though
redirectSignIn: aws.redirectSignIn,
redirectSignOut: aws.redirectSignOut,
responseType: 'token',
},
})
就我而言,我像馮章一樣使用放大。 更改回調 url 后,我不得不等待一個多小時才能使更改生效。
在 Amplify + 多個重定向 URL 的上下文中(受@Oscar Nevarez
啟發)我查看了src/aws-export.js
:
"redirectSignIn": "https://example.com/,http://localhost:5173/",
"redirectSignOut": "https://example.com/,http://localhost:5173/",
當作為 URL redirect_uri
參數傳遞時,Cognito 不會消化它。
我的修復是在src/main.js
中覆蓋這些值,如下所示
awsconfig.oauth.redirectSignIn = `${window.location.origin}/`
awsconfig.oauth.redirectSignOut = `${window.location.origin}/`
為本地和部署工作
我關注了這個視頻“使用 AWS Amplify 為 Web 應用程序添加 Facebook 登錄”: https : //dev.to/aws/adding-facebook-sign-in-for-web-applications-with-aws-amplify-2fc8
它部署到本地主機,所以我然后將它部署到 Amplify URL ......我有同樣的重定向錯誤廣告,結果我沒有更新 src 目錄中的 aws-exports.js。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.