横向扩展Json-Web-Token nodejs
Horizontal scaling Json-Web-Token nodejs
在 nodejs 中横向扩展 JWT 的正确方法是什么。我正在使用 RSA
生成令牌。因此每个服务器都能够解码自己生成的令牌。所有负载平衡都是无状态的,因此无法知道哪个服务器生成了令牌。我目前使用的代码是
helper['generateToken'] = (user)=>{
return new Promise((fullfill,reject)=>{
try{
var cert = fs.readFileSync('pvt.key');
var token = jwt.sign(user,process.env.SECRET);
fullfill(token);
}catch(ex){
reject(new Error("Your token could not be generated"));
}
});
}
generateToken
函数可以运行任意一张图片,而且所有图片都有不同的私钥。什么是最好的扩展方式。
Just a side note i am running there instances on docker swarm
so each server would be able to decode tokens that were generated by itself. All the load balancing is stateless so there is no way to knowing which server generated the token.
...and all of them have different private key.
由于一个服务器可以接收另一个服务器颁发的令牌,而您无法区分发起者,您需要使用相同的签名密钥。
备选方案
在使用共享文件夹或数据库的实例之间共享密钥(从而保护对其的访问)
使用所有实例共享的中央身份验证微服务来签署令牌。如果使用密钥对,则可以在每个实例本地完成签名验证。问题:同样需要负载均衡,但可以降低密钥共享的复杂度
测试所有可能的密钥(不是很好):使用非对称密钥对 (RSA) 并使用所有可用的 public 键检查是否正确
在 nodejs 中横向扩展 JWT 的正确方法是什么。我正在使用 RSA
生成令牌。因此每个服务器都能够解码自己生成的令牌。所有负载平衡都是无状态的,因此无法知道哪个服务器生成了令牌。我目前使用的代码是
helper['generateToken'] = (user)=>{
return new Promise((fullfill,reject)=>{
try{
var cert = fs.readFileSync('pvt.key');
var token = jwt.sign(user,process.env.SECRET);
fullfill(token);
}catch(ex){
reject(new Error("Your token could not be generated"));
}
});
}
generateToken
函数可以运行任意一张图片,而且所有图片都有不同的私钥。什么是最好的扩展方式。
Just a side note i am running there instances on docker swarm
so each server would be able to decode tokens that were generated by itself. All the load balancing is stateless so there is no way to knowing which server generated the token.
...and all of them have different private key.
由于一个服务器可以接收另一个服务器颁发的令牌,而您无法区分发起者,您需要使用相同的签名密钥。
备选方案
在使用共享文件夹或数据库的实例之间共享密钥(从而保护对其的访问)
使用所有实例共享的中央身份验证微服务来签署令牌。如果使用密钥对,则可以在每个实例本地完成签名验证。问题:同样需要负载均衡,但可以降低密钥共享的复杂度
测试所有可能的密钥(不是很好):使用非对称密钥对 (RSA) 并使用所有可用的 public 键检查是否正确