我有以下用 NodeJS 编写的代码,它在路由中间件中使用 Passport JS Bearer Authentication。
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 测试我的端点)。