Error: Umi project development environment localhost:8000 cannot be opened after sso in Chrome. Edge and Firefox are normal

Reason: third-party cookies are disabled after chrome84. In localhost development environment request site.example.com/api/current… The interface does not carry the cookie after authorization. Authentication failed and redirected to SSO.. example.com

Login Flowchart

Open graph TD localhost: 8000 - - > B Request [https://site.example.com/api/currentUserInfo] - the Request Headers carry cookies, Set -cookie <br> domain=example.com, path= API --> A{ Verify login} A -- logged in --> Successfully entered the development environment A -- not logged in --> Redirect to https://sso.. Example.com authorization login - > C/login successful C - > | set new cookie | redirected to localhost - > B

There is a loop that, in Chrome, jumps back and forth between localhost and site.example.com. The reason is that localhost is not authorized to redirect to site, but site is authorized to redirect to Localhost

The solution

The idea is to solve the problem of crossing cookies

  1. Umi sets proxy to resolve the cross-domain cookie problem
  2. Change hosts. Create a new domain name dev.example.com as the development environment domain name
  3. set with SameSite=None and SecureEnable third-party cookies
  4. Chrome was downgraded to 83 and below
  5. Use the Nginx proxy

Proxy Problems

– Fixed cross-domain problem, but set-cookie set domain, localhost still does not carry cookies

Change the hosts

Configure 127.0.0.1 dev.example.com for the hosts file

Open dev.example.com:8000 as the development environment

Problem: the calling interface was rejected by the server. It may be a firewall or interface configuration problem

SameSite

A future release of Chrome will only deliver cookies with cross-site requests if they are set with SameSite=None and Secure. A cookie with the Secure attribute is sent to the server only with an encrypted request over the HTTPS protocol,

SameSite and Secure configurations for set-cookie cannot be turned on without background code

Use the Nginx proxy

Nginx configuration

server { listen 443 ssl; server_name dev.nfapp.southcn.com; ssl_certificate server.crt; ssl_certificate_key server.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:! aNULL:! MD5; ssl_prefer_server_ciphers on; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; Proxy_pass http://127.0.0.1:8000; }}Copy the code

Server certificate server.crt

-----BEGIN CERTIFICATE-----
MIIDHjCCAqOgAwIBAgIUTH2RYIRu2VXopx7+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server key server.key

-----BEGIN EC PARAMETERS-----
BgUrgQQAIg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDB44vFFtwQupGCHb1Ow1sboZVgkYB+nW6QobqmKShIXLKTCPYXrYpHE
6WLzYPUEgLygBwYFK4EEACKhZANiAATta0xZgXNJq/p37x7pShiQiCt0fuIB0ZWo
CHAb+fnCiqrsz8HAg/J3jEBZFTK8LpXG3hOj04OFUd2l5GMeXM4LUrLs4A0i8JI8
13PoYWkFutJnHdFjuSMNU8POkCjy8j8=
-----END EC PRIVATE KEY-----

Copy the code

Root domain name certificate root. CRT, import the PC trusted certificate

-----BEGIN CERTIFICATE-----
MIICGzCCAaICFGwJFzu0l8D2ZCcws4spyOC85XAIMAoGCCqGSM49BAMDMHIxCzAJ
BgNVBAYTAkNOMQswCQYDVQQHDAJHWjENMAsGA1UECgwEcm9vdDENMAsGA1UECwwE
cm9vdDEZMBcGA1UEAwwQcm9vdCBjZXJ0aWZpY2F0ZTEdMBsGCSqGSIb3DQEJARYO
cm9vdEBlbWFpbC5jb20wHhcNMjEwOTIyMDczMTUxWhcNMzEwOTIwMDczMTUxWjBy
MQswCQYDVQQGEwJDTjELMAkGA1UEBwwCR1oxDTALBgNVBAoMBHJvb3QxDTALBgNV
BAsMBHJvb3QxGTAXBgNVBAMMEHJvb3QgY2VydGlmaWNhdGUxHTAbBgkqhkiG9w0B
CQEWDnJvb3RAZW1haWwuY29tMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEvz8L7zsA
cWsOlJ02QfHRBO1iQQyjPd76nZqyo9rUsuvaxai6mV0CYTgoPNtT4Zoti/iaucLt
SHigGhzeUae8H2ehyJavFLNEyQU8t5tbHEpmscqXE+wu8UUaR0eed5GiMAoGCCqG
SM49BAMDA2cAMGQCMGFbi0XXo2O8Ou3r5oUA8wYnyPPuRYT36AjjJGp9Rf2pdqUb
LpMGHDZFFGu3Ey5T0gIwEBFtvGmpr6YagL4hGyI+wYwj/VKoYRmdClUpeTyURkFk
Y/kWQSaPMIQfOIgUaQAr
-----END CERTIFICATE-----

Copy the code

Dev.nfapp.southcn.com: 8000 as a development environment, sso.. Example.com redirect to locahost after authorization, need to open dev.nfapp.southcn.com: 8000

WebSocket is used in the project and WSS is required for the protocol

document.location.protocol === "https:"
      ? `wss://${webSocketIP}`
      : `ws://${webSocketIP}`.Copy the code

Login flowchart after proxy

Open graph TD D [https://dev.nfapp.southcn.com:8000] -- > B Request [https://site.example.com/api/currentUserInfo] - the Request Set -cookie <br> domain=example.com, path= API --> A{ Verify login} A -- logged in --> Successfully entered the development environment A -- not logged in --> Redirect to https://sso.. Example.com authorization login - > C/login successful C - > | set new cookie | redirected to localhost - > manual jump | | D

amendments

  1. https://sso. After login authorization, place the token after the URL.
  2. After authorization, reset the URL opened by the user
// koa
ctx.redirect(ctx.request.origin + '? token=xxxx');
Copy the code

Umi 2 x pit

  1. Webpack DevServer configuration is not supportedhttps: trueconfiguration

Umi 3.x HTTPS localhost can be enabled

reference

Medium.com/swlh/7-keys…

Self-signed HTTPS certificate