[英]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.