【发布时间】:2014-09-21 18:06:13
【问题描述】:
我有以下用 NodeJS 编写的代码,在路由中间件中使用 Passport JS Bearer 身份验证。
app.put('/api/customers/:id', passport.authenticate('bearer', {session : false}), handlers.putCustomers);
app.put('/api/products/:id', passport.authenticate('bearer', {session:false}), handlers.putProducts);
我不想对每个端点使用passport.authenticate,而是想对每个端点全局应用它。
我尝试了以下中间件顺序,但没有成功
var app = express();
app.use(bodyParser());
app.use(session({ store: new RedisStore({client:client}),secret: 'keyboard cat' }));
app.use(passport.initialize());
passport.use(new BearerStrategy(){....
app.use(passport.authenticate('bearer', {session:false}));
app.put('/api/customers/:id', handlers.putCustomers);
app.put('/api/products/:id', handlers.putProducts);
当我到达端点时,我得到一个 401。如果这有帮助或有意义,我会看到它是“OPTIONS”类型而不是“PUT”。(使用 chrome 从我的应用程序的 UI 测试我的端点)。
【问题讨论】:
标签: node.js express passport.js restful-authentication bearer-token