Angular 4 app on S3 denied access after redirect

懵懂的女人 提交于 2021-02-10 20:18:34

问题


I've built a simple angular 4 app that uses firebase as my authentication. And I use loginWithRedirect because loginWithPopup didn't work really on my cell phone.

But the problem I'm running into is the redirect leaves the page, obviously, to authenticate, and then comes back to mysite.com/login but because its a SPA /login doesn't exist to the bucket I'm guessing.

I've added this redirection rule, but it doesn't seem to be doing anything

<RoutingRules>
  <RoutingRule>
    <Condition>
      <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
    </Condition>
    <Redirect>
      <HostName>mysite.cloudfront.net</HostName>
      <ReplaceKeyPrefixWith>#!/</ReplaceKeyPrefixWith>
    </Redirect>
  </RoutingRule>
  <RoutingRule>
    <Condition>
      <HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals>
    </Condition>
    <Redirect>
      <HostName>mysite.cloudfront.net</HostName>
      <ReplaceKeyPrefixWith>#!/</ReplaceKeyPrefixWith>
    </Redirect>
  </RoutingRule>
</RoutingRules>

I'm redirecting on 403 also because thats the error I keep getting. Then I've added <script> history.pushState({}, "entry page", location.hash.substring(1)); </script> to the top of my index.html file (solution from another stackoverflow question).

But still getting the 403. I don't have the app set up to use hashbangs to begin with and would like to not have to.

The error message I recieve:

<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>0D98B2536B28C06D</RequestId>
    <HostId>8nhnAtzaLxPvWcV3f8zqc=</HostId>
</Error>

Edit: A dirty fix I have is to just right before sending the user to google to authenticate, navigate to '/' so thats where google redirects back to. It'd be nice if there was a way to tell google where to redirect back to. Is there?


回答1:


We need add the read access policy in the permissions > bucket policy as mentioned in this documentation. Give it a try

http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteAccessPermissionsReqd.html

{ "Version":"2012-10-17", "Statement":[{ "Sid":"PublicReadGetObject", "Effect":"Allow", "Principal": "*", "Action":["s3:GetObject"], "Resource":["arn:aws:s3:::YOUR-BUCKET-NAME/*" ] } ] }



来源:https://stackoverflow.com/questions/48118958/angular-4-app-on-s3-denied-access-after-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!