简体   繁体   English

在2个网站上进行身份验证和登录

[英]Authentication and Login on 2 website

I have 2 site: example.com and exampletwo.com 我有2个网站:example.com和exampletwo.com

I want that when a user login on example.com then he is automatically authenticated also on exampletwo.com 我希望当用户在example.com上登录时,他也会在exampletwo.com上自动通过身份验证

How can I do that ? 我怎样才能做到这一点 ? I use Django + Nginx on first website and Tornado framework + Tornado server on second website. 我在第一个网站上使用Django + Nginx,在第二个网站上使用Tornado框架+ Tornado服务器。

Thanks ;) 谢谢 ;)

.

PS If you don't know this platforms ( Django or Tornado or Nginx ), I accept also a solution for a generic PHP+Apache platform and then I will do some research :) PS:如果您不知道此平台(Django,Tornado或Nginx),那么我也接受通用PHP + Apache平台的解决方案,然后我将进行一些研究:)

I would have the code handling user registration on example.com immediately send an https request to exampletwo.com (which authenticates it based on certificates, of course) meaning "add this user with these credentials". 我要在example.com上处理用户注册的代码立即将https请求发送到exampletwo.com (当然,它是根据证书进行身份验证的),意思是“使用这些凭据添加此用户”。 This approach seems to be workable for any two web servers / frameworks / languages as long as they're able to send and receive HTTPS requests and authenticate certificates. 只要任何两种Web服务器/框架/语言都能够发送和接收HTTPS请求以及对证书进行身份验证,此方法就似乎可行。

If you can't authenticate certificates, you could send the "add this user" message encrypted (as long as the two sites can share a secret to use for the encryption). 如果您不能对证书进行身份验证,则可以发送加密的“添加此用户”消息(只要两个站点可以共享用于加密的秘密)。 This may be vulnerable to replay attacks, but if you make a timestamp part of the "add this user" message, you can highly restrict the time window of vulnerability for the replay attacks, probably enough to make this approach viable. 这可能容易受到重播攻击的影响,但是如果将时间戳记作为“添加此用户”消息的一部分,则可以高度限制重播攻击的漏洞时间窗口,可能足以使此方法可行。

If you can't safely share secrets between the two sites, not everything is lost: you can still use public key encription. 如果您不能安全地在两个站点之间共享机密,那么不会丢失所有内容:您仍然可以使用公共密钥加密。 The sender encrypts the "add this user message" (including the timestamp of course) with its own private key, then with the receiver's public key; 发送者使用自己的私钥,然后使用接收者的公钥对“添加此用户消息”(当然包括时间戳)进行加密; the receiver decrypts what it receives with its own private key, then with the sender's public key. 接收者先用自己的私钥解密接收到的内容,然后再用发送者的公钥解密。 A bit messy and perhaps a bit slow, but under such difficult constraints it's surprising that it can still be done at all;-). 有点混乱,也许有点慢,但是在如此困难的约束下,令人惊奇的是它仍然可以完成;-)。

Assuming both the websites can access a single shared database this can be done with both the webpages accessing a table and checking whether a user has logged in or not. 假设两个网站都可以访问一个共享数据库,则这两个网页都可以访问表并检查用户是否已登录。 I am not familiar with Djano or Tornado, but if the above solution is dubious, you may wish to expose a Web Method (Web Services) of one website and then use (call) the web method from the other. 我不熟悉Djano或Tornado,但是如果上述解决方案令人怀疑,则您可能希望公开一个网站的Web方法(Web服务),然后从另一个网站使用(调用)该Web方法。 Cookies are a no brainer as they are site dependent. Cookies毫无疑问,因为它们取决于站点。 And another approach is tracking the user through the IP, but dynamic IP's can pose a great problem and may be used to breach the security. 另一种方法是通过IP跟踪用户,但是动态IP可能会带来很大的问题,并且可能会用来破坏安全性。

The problem is that one site cannot set cookies for another. 问题是一个站点无法为另一个站点设置cookie。 Even if your login code on example.com were to do a server-to-server request to tell exampletwo.com that the user's validated, there's no way to set a cookie to carry this message over when the user actually visits exampletwo.com. 即使您在example.com上的登录代码发出了服务器到服务器的请求以告知exampletwo.com用户已通过验证,也无法设置cookie来在用户实际访问exampletwo.com时传递此消息。

Client certificates are way around this, but if this dual-auth system is for the general public, not a particularly good one. 客户端证书可以解决此问题,但是如果此双重身份验证系统是面向大众的,则不是一个特别好的系统。 Most people will see some weird request ('Do you want to add this cert?') pop up and run off to the hills screaming. 大多数人会看到一些奇怪的请求(“您要添加此证书吗?”)弹出并尖叫到山上。

The workaround is do have example.com spit out SOMETHING that'll cause the user to load something from exampletwo.com. 解决方法是让example.com吐出一些东西,这会导致用户从exampletwo.com加载内容。 An image, a chunk of javascript, etc... and put an encrypted token in the request ( <img src="http://exampletwo.com/login.php?remoteauth=ENCRYPTEDTOKEN"> ). 图片,大量的javascript等...,然后在请求中放入加密令牌( <img src="http://exampletwo.com/login.php?remoteauth=ENCRYPTEDTOKEN"> )。 Then exampletwo can decrypt the token, and if its contents are ok, and send out the image (or whatever you're transferring) with the appropriate cookie set. 然后exampletwo可以解密令牌,如果令牌的内容还可以,并发送带有适当cookie集的图像(或您要传输的任何内容)。

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

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