2
我有一个使用react-router的通用NodeJS,Express,React/Redux应用程序。它在应用程序的初始请求和客户端上对来自react-router的后续请求从服务器端渲染。在服务器端使用passport-jwt呈现Express/React应用程序
我routes.js文件:
<Route path="/" component={App}>
<Route path="/main" component={Main}/>
<Route path="/login" component={Login}/>
</Route>
我有匹配该反应的路由和发送组件标记回模板通配符快递路线:
import routes from '../routes';
app.get('*', (req, res) => {
match(
{ routes, location: req.url },
(err, redirectLocation, renderProps) => {
let markup;
if (renderProps) {
markup = renderToString(
<Provider store={store}>
<RouterContext {...renderProps}/>
</Provider>
);
} else {
markup = renderToString(<NotFoundPage/>);
res.status(404);
}
return res.render('index', { markup });
}
);
});
现在我想使用passport-jwt保护一些被通配符路由拦截的路由,例如:
app.get("*", passport.authenticate('jwt', { session: false }),
(req, res) => {
match(
{ routes, location: req.url },
(err, redirectLocation, renderProps) => {
res.json("Success! You can not see this without a token");
}
)
});
如何在通配符路由上只保护routes.js中的给定路由?
其实我要让我的通配符的路线,而服务器端呈现选择性保护。你知道我怎么能在这个案例中使用中间件? –
让我们假设(只是一个例子),你想用你的令牌认证中间件保护'/ admin'背后的任何东西。为此,在声明通配符处理函数之前,您应该使用'app.use('admin',passport.authenticate('jwt',{session:false})'。中间件函数按顺序执行,因此中间件包含的顺序是重要的。 –
这里是一个更深入的例子 - https://github.com/scotch-io/easy-node-authentication/blob/local/server.js –