我试图了解如何使用CORS,并对Access-Control-Allow-Credentials标题的作用感到困惑.
文件说
指示凭证标志为true时是否可以公开对请求的响应.
但我不明白"暴露"的反应意味着什么.
任何人都可以解释这个标题被设置为true(与设置为true的凭证标志一起)实际上是什么?
我正试图让我的Passport本地策略正常运行.
我已经设置了这个中间件:
passport.use(new LocalStrategy(function(username, password, done) {
//return done(null, user);
if (username=='ben' && password=='benny'){
console.log("Password correct");
return done(null, true);
}
else
return done(null, false, {message: "Incorrect Login"});
}));
Run Code Online (Sandbox Code Playgroud)
但是在这里
app.use('/admin', adminIsLoggedIn, admin);
function adminIsLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}
Run Code Online (Sandbox Code Playgroud)
它总是失败并重定向到主页.
我无法弄清楚为什么会这样?为什么不进行身份验证?
在我的控制台中,我可以看到Password Correct正在打印.为什么它不起作用?
我无法跟踪此情况,但对于我的设置,isAuthenticated即使成功登录后也始终返回false.这是护照代码:
req.isAuthenticated = function() {
var property = 'user';
if (this._passport && this._passport.instance._userProperty) {
property = this._passport.instance._userProperty;
}
return (this[property]) ? true : false;
};
Run Code Online (Sandbox Code Playgroud)
但是快速浏览一下,我看不到_userProperty本地策略中的任何地方(对不起,如果我看起来不够努力),所以我想这可能就是为什么它总是返回错误?
我留下了我的应用程序代码的代码示例,但我觉得可能更容易快速查看我正在进行的工作的repo: passport api token sessionless
最终,我的目标是让logout在该样板项目中正常工作(目前它没有).
出于某种原因,在我的NodeJS Express应用程序上,当使用Facebook通过PassportJS库进行身份验证时,无论身份验证成功并返回配置文件数据,调用request.isAuthenticated()始终返回false.
我已经声明我的身份验证回调将成功重定向到/profile路由,它成功完成:
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/profile', failureRedirect: '/' }));
Run Code Online (Sandbox Code Playgroud)
使用在确定是否继续处理请求之前验证身份验证的函数声明此路由:
app.get('/profile', ensureAuthenticated, user.list);
该功能的定义如下:
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
return res.redirect('/')
}
Run Code Online (Sandbox Code Playgroud)
你可以看到一切都很简单.我这里没有做任何特别特别的事.
逐步通过PassportJS的代码显示它不会在请求中存储用户数据,除非您assignProperty在声明身份验证中间件时指定选项dict中.这是它在调用时尝试访问的同一属性isAuthenticated(),因为它从不存储这些数据,它总是声称我没有经过身份验证.
不幸的是,指定此密钥会搞砸Express的路由匹配,这会导致404错误处理回调URL,因为检查的代码会assignProperty立即转移到处理下一个可用路由.
我已经将代码全部添加到了pastie中.我很感激任何人都能提供的任何帮助.
我在使用Passport时遇到了一个问题:在调用我的自定义端点时,我无法检查用户是否经过身份验证.
我已按以下方式配置了Express4应用程序:
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// required for passport
app.use(session({ secret: 'secretphrase' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(checkAuth); // CHECK SESSION
app.use(flash()); // use connect-flash for flash messages stored in session
app.use(prepareRequests);
Run Code Online (Sandbox Code Playgroud)
checkAuth()中间件具有以下代码:
var checkAuth = function(request, response, next) {
console.log("------------");
console.log("checkAuth user: " + request.session.passport.user);
console.log("checkAuth isAuthenticated: " + request.isAuthenticated());
next();
}
Run Code Online (Sandbox Code Playgroud)
我第一次尝试使用护照登录时,isAuthenticated是假的.一旦我登录,每次打电话给我的服务器,当我的中间件通过时,被认证是假的!!! 但是,奇怪的是,如果我再次尝试登录,则isAuthenticated是真的.
这意味着只有我的AJAX调用返回isAuthenticated = false,但是当我maka表单帖子或点击指向API的链接时,它返回true!然后存储会话,但不存储AJAX请求. …
passport.js ×4
node.js ×3
express ×2
javascript ×2
access-token ×1
ajax ×1
cors ×1
http-headers ×1