【问题标题】:Using Passport JS Bearer Authenticate Globally使用 Passport JS Bearer 进行全局身份验证
【发布时间】: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


    【解决方案1】:

    回答我的问题:我设法通过使用此处提到的通配符路由解决了这个问题: app.use()

    app.put("/api/*",passport.authenticate('bearer', {session:false}));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 2019-01-31
      • 2021-05-09
      • 2018-08-29
      相关资源
      最近更新 更多