【问题标题】:How to Pass the encrypted response from nodejs to client side through ajax?如何通过ajax将nodejs的加密响应传递到客户端?
【发布时间】:2018-01-26 22:54:53
【问题描述】:

我正在尝试加密通过 ajax 以 json 格式发送的密码。加密逻辑在我的 server.js 中

如何传递来自 server.js 的加密响应。我可以加密,但我一直在传递这个加密的响应

server.js:

    app.post('/mylink',function(request,reply){ 
    var data = JSON.stringify(request.body.jsonblob);
    var pwd = request.body.jsonblob.Password;
    var cipher = crypto.createCipher(algorithm,password)
    var crypted = cipher.update(pan,'utf8','hex')
    crypted += cipher.final('hex');
    console.log("crypted"+crypted);
    reply.send(crypted);
     });

在我的 html 页面中调用 ajax:

     var json_data = JSON.stringify({
                    "jsonblob" : {    
                         "Password": password   
                    }
                    });


          $.ajax({ 

            url:"/mylink",
            type: "post",               
            dataType: "json",      
            contentType: "application/json",
            data: json_data,                       
            success:function(response){
                if(response.status === "success")
                {
                    console.log(crypted);
                }

            },
            error: function(jqXHR, textStatus, errorThtrown) {
                console.log("error " +textStatus);
                }


          });

【问题讨论】:

  • 使用 HTTPS,这将完全安全透明地发生吗?
  • 如果你请求json,你需要用json响应。向客户端返回密码没有意义
  • 感谢您的澄清

标签: javascript jquery html node.js ajax


【解决方案1】:

我想不出任何您希望将加密密码返回给客户端的用例。您在服务器上加密和解密您的密码,以防止您的密码以明文形式存储在数据库中。

我认为您可能会混淆密码的加密和生成 JSON Web 令牌,您以后可以使用该令牌对用户进行身份验证。在这种情况下,您可能希望返回要保存在客户端的令牌。有许多关于如何构建安全节点后端的精彩文章。这是我最喜欢的:

https://medium.freecodecamp.org/securing-node-js-restful-apis-with-json-web-tokens-9f811a92bb52

此外,如果您想保护您的密码在您的请求内容中清晰,请确保您使用 HTTPS。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 2016-03-19
    • 2019-10-15
    相关资源
    最近更新 更多