简体   繁体   English

Amazon Cognito:如何在从浏览器重定向到 Android 应用程序时停止出现“redirect_mismatch”错误

[英]Amazon Cognito: How to stop getting "redirect_mismatch" error when redirecting from browser to Android app

I am trying to create a Android project where I authorize a user by having him log into Amazon Cognito in a browser, which should then redirect back to my app .我正在尝试创建一个 Android 项目,在该项目中,我通过让用户在浏览器中登录 Amazon Cognito 来授权用户,然后浏览器应该重定向回我的应用程序 Unfortunately, when the browser opens, instead of reaching the proper sign-in page, I keep getting this error:不幸的是,当浏览器打开时,我没有到达正确的登录页面,而是不断收到此错误:

在此处输入图像描述

In my AuthenticatorActivity.java:在我的 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);
}

In AndroidManifest:在安卓清单中:

<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>

I can't figure out what I am doing wrong here.我不知道我在这里做错了什么。 Am I missing a step?我错过了一步吗?

Ok, I'm leaving a tidbit here for whoever might find it next.好的,我要在这里为下一个可能会找到它的人留下一条小道消息。 I hit this issue exact same issue, but as a newbie to Cognito and IdP/SSO I had no idea how to fix this.我遇到了完全相同的问题,但作为 Cognito 和 IdP/SSO 的新手,我不知道如何解决这个问题。 Here is what I did to eventually fix this.这是我为最终解决此问题所做的工作。 We were integrating with an external service, and we were getting this error.我们正在与外部服务集成,我们收到了这个错误。 Under Chrome Developer Tools -> Network, I started to record the URL's visited, then I tried the SSO integration again.在 Chrome Developer Tools -> Network 下,我开始记录访问过的 URL,然后我再次尝试了 SSO 集成。 There was a URL that showed up in the list which visited Cognito with a redirect to URL.列表中显示了一个 URL,它通过重定向到 URL 访问了 Cognito。 That URL must be the same URL as listed under the Callback URL for Cognito.该 URL 必须与 Cognito 的回调 URL 下列出的 URL 相同。

Hopefully, this saves someone some time in the future.希望这可以在将来为某人节省一些时间。

Do check your callback url and sign out url.请检查您的回调网址并注销网址。 The corect format is :正确的格式是:

app_client_name:https://www.myapp.com/

认知

Extending on the answer by Dimitris https://stackoverflow.com/a/60456018/6883773扩展Dimitris的回答https://stackoverflow.com/a/60456018/6883773

If you have a DNS route53 specified for your load balancer.如果您为负载均衡器指定了 DNS route53。 You can specify the same in the callback URL.您可以在回调 URL 中指定相同的内容。

https://www.example.com/oauth2/idpresponse

Ref: https://aws.amazon.com/premiumsupport/knowledge-center/elb-configure-alb-authentication-idp/参考: https : //aws.amazon.com/premiumsupport/knowledge-center/elb-configure-alb-authentication-idp/

I am using amplify with cognito and encountered this error.我正在使用 cognito 放大并遇到此错误。 Fixed by following.通过以下修复。 In aws-export.ts, there is a redirectSingIn url, it must be the exact same url as in cognito/app Integration/app client setting/ callback url which is where the application runs.在 aws-export.ts 中,有一个 redirectSingIn url,它必须与应用程序运行的 cognito/app Integration/app 客户端设置/回调 url 中的 url 完全相同。

Update: I encountered this problem again in AWS cognito, user pool, App client, client web.更新:我在 AWS cognito、用户池、App 客户端、客户端 Web 中再次遇到此问题。 After updating the Callback URL(s), things starts to break, even the Callback URL(s) is valid.更新回调 URL 后,事情开始中断,即使回调 URL 是有效的。 Later I figured out that it will take some time for the change to sync in. Need to walk away about 10 minutes, then try again.后来我发现更改同步需要一些时间。需要走开大约 10 分钟,然后再试一次。

Another silly mistake I did and took me hours to figure it out was the fact that the value of redirectSignIn in aws-exports.js was completely wrong.我犯的另一个愚蠢的错误是我花了几个小时才弄明白的事实是aws-exports.jsredirectSignIn值完全错误。 When you modify the value of this configuration multiple times through Amplify CLI, it appends a comma treating the value as a List giving you something like this当您通过 Amplify CLI 多次修改此配置的值时,它会附加一个逗号,将该值视为一个列表,为您提供如下内容

 "redirectSignIn": "http://localhost:3000/,http://localhost:3000/,http://localhost:3000/,http://localhost:3000/",

Unfortunately, the value is treated as a string when used using HostedUI.不幸的是,当使用 HostedUI 时,该值被视为字符串。

In my case the error was due to CloudFront serving the old files.在我的情况下,错误是由于 CloudFront 提供旧文件。

To solve it;解决它; you can invalidate CloudFront files via AWS console.您可以通过 AWS 控制台使 CloudFront 文件无效。 ps can use /* to invalidate all of the files https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html ps 可以使用/*使所有文件无效https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html

I solved this by remembering to include http:// in the callbackUrl on the frontend.我通过记住在前端的 callbackUrl 中包含http://来解决这个问题。

const redirect_url=`${window.location.origin}`;

It probably wont be a common reason, but was why mine broke.这可能不是一个常见的原因,但这就是我的坏了的原因。

This is because of the mismatched url for either redirectSignIn or redirectSignOut.这是因为redirectSignIn 或redirectSignOut 的url 不匹配。 Please check both setup in aws console and code aws_config, and make them consistent.请检查 aws 控制台中的设置和代码 aws_config,并使它们保持一致。

aws console redirect uri configuration aws 控制台重定向 uri 配置

In my case is was because in the console I was redirecting to https://localhost:4200 but in the URL I had http://localhost:4200 .在我的情况下是因为在控制台中我重定向到https://localhost:4200但在 URL 中我有http://localhost:4200 Note the SSL/TLS version vs. non-SSL/TLS注意SSL/TLS版本与non-SSL/TLS

Assuming your website is behind an application load balancer (ALB), and you have a listener rule that uses a Cognito user pool and Path is * in the IF rule statement, you should configure your 0Auth client app callback url, like:假设您的网站位于应用程序负载均衡器 (ALB) 之后,并且您有一个使用 Cognito 用户池的侦听器规则,并且 IF 规则语句中的路径为 * ,您应该配置您的 0Auth 客户端应用程序回调 url,例如:

https://<your-ALB-DNS>/oauth2/idpresponse

This make it work for me at least, with no other fancy config.这至少对我有用,没有其他花哨的配置。

Keep in mind though that this will just provide a layer on top of whatever you have behind the ALB.请记住,这只会在 ALB 后面的任何内容之上提供一个层。 If you have some additional authentication method in it, you have to configure that as well.如果你有一些额外的身份验证方法,你也必须配置它。

You absolutely need to make sure these items are checked if you're requesting a scope, otherwise you get redirect_mismatch (unhelpful error name).如果您请求范围,您绝对需要确保检查这些项目,否则您会收到redirect_mismatch (无用的错误名称)。

在此处输入图片说明

With a config of below from the tutorial here使用教程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',
  },
})

In my case I use amplify just like Feng Zhang.就我而言,我像冯章一样使用放大。 After changing the callback url, I had to wait over an hour for the changes to take effect.更改回调 url 后,我不得不等待一个多小时才能使更改生效。

redirect_uri (1st img) must be the same as in the Callback URL(s) field (2nd img). redirect_uri(第一个 img)必须与回调 URL 字段(第二个 img)中的相同。

1st img第一张图片

在此处输入图片说明

2nd img (App integration -> App client settings under AWS)第二个 img(应用程序集成 -> AWS 下的应用程序客户端设置)

在此处输入图片说明

In the context of Amplify + multiple redirection URLs (inspired by @Oscar Nevarez ) I looked at src/aws-export.js :在 Amplify + 多个重定向 URL 的上下文中(受@Oscar Nevarez启发)我查看了src/aws-export.js

        "redirectSignIn": "https://example.com/,http://localhost:5173/",
        "redirectSignOut": "https://example.com/,http://localhost:5173/",

Which is NOT digested by Cognito when passed as URL redirect_uri parameter.当作为 URL redirect_uri参数传递时,Cognito 不会消化它。

My fix was to override these values in src/main.js as follows我的修复是在src/main.js中覆盖这些值,如下所示

awsconfig.oauth.redirectSignIn = `${window.location.origin}/`
awsconfig.oauth.redirectSignOut = `${window.location.origin}/`

Worked both for local and deployed为本地和部署工作

I followed this video "Adding Facebook Sign In for Web Applications with AWS Amplify": https://dev.to/aws/adding-facebook-sign-in-for-web-applications-with-aws-amplify-2fc8我关注了这个视频“使用 AWS Amplify 为 Web 应用程序添加 Facebook 登录”: https : //dev.to/aws/adding-facebook-sign-in-for-web-applications-with-aws-amplify-2fc8

It deploys to localhost, so I then deployed it to a Amplify URL ... I had the same redirect error ad it turned out that I hadn't updated aws-exports.js in the src directory.它部署到本地主机,所以我然后将它部署到 Amplify URL ......我有同样的重定向错误广告,结果我没有更新 src 目录中的 aws-exports.js。

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

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