javascript - PassportJS - 在将负载数据作为请求参数传递给 passport.authenticate 之前获取负载数据

标签 javascript angularjs node.js passport.js cryptojs

有一个passport.js用于 LDAP-auth 的实现。现在下一步是使用 Crypto-js 在客户端加密密码如下:

客户端 angular-js Controller

$scope.authenticate = function () {      
  var auth = new login();
  auth.username = $scope.username;
  auth.password = CryptoJS.AES.encrypt($scope.password); //// HERE  

  auth.$save(function (response){
    console.log(response);
  },function(err){
    console.log(err);
  });
}

服务端服务

.....
.....
app.post('/login', passport.authenticate('ldapauth'), (req, res) => {

    console.log("req.user: ",req.user);
    req.session.username = req.user[ldap.username];
    req.session.userModel = req.user;
    res.status(200).send({"success": 'success'});
});
.....

在使用请求“req”调用passport.authenticate 之前的服务器端服务上,aes 加密密码需要解密。如何在这里实现? (问题不在于加密,而在于如何在将数据传递给 passport.authenticate 之前获取数据作为请求)

最佳答案

@Abhijay Ghildyal 我认为他们不理解您的问题。在将请求传递给 passport.authenticate() 之前拦截请求确实是可能的。你想要做的是将这段代码添加到你的 express.js 或你在其中执行快速服务器实现的任何文件。此外,我在这里解密 request.body 而不是 req.user,因为那时用户尚未登录,但是如果您的情况有所不同,那很好,您可以用相同的方式解密 req.user。 (此处的变量 app 是您的服务器的名称,即 var app = express();)

app.use(function(req, res, next) {
    if(req.url === '/login'){
        //CryptoJS.AES.decrypt() is Assumed to be the decrypter function here.
        req.body = CryptoJS.AES.decrypt(req.body);
        console.log(req.body); //To view decrypted body
    }
    next();
});

就是这样。此中间件函数将在 passport.authenticate() 函数之前首先到达。只要确保如果你将它应用到 req.body,你首先添加这些代码行,在上面的段落之前导入 bodyParser (bodyParser = require('body-parser');) 之后。

app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());

关于javascript - PassportJS - 在将负载数据作为请求参数传递给 passport.authenticate 之前获取负载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42698423/

相关文章:

javascript - 在 DataTable 中找不到匹配的记录

javascript - 警告 : Failed prop type: Invalid prop `error` of type `string` expected `boolean`

mysql - 如何使用/Angular 从 MySQL 服务器查看 RowDataPacket?

node.js - 登录验证后快速重定向

node.js - 在 Node.js 中映射 Azure 表存储数据模型的最简单方法

javascript - jQuery Mobile 从两个自动完成 UL 中获取值(value)

JavaScript Promise 与 FileReader

angularjs - $on 只在一个 Controller 上被调用

angularjs - 在 ControllerAs AngularJS 中调用子函数

javascript - 手动加载node.js模块