0

当我从承诺中的数据库中传递选定的路线时,授权不起作用。这意味着对通过路由的请求总是被授权的。

protected applyRoutes(consumer: MiddlewaresConsumer) {
    let paths = this.authPathService.findAll();

    paths.then((resultPaths) => {
        let result: {}[] = [];
        for (let path of resultPaths) {
            result.push({
                path: path.path,
                method: RequestMethod.ALL
            })
        }
        consumer
            .apply(passport.authenticate('jwt', { session: false }))
            .forRoutes(...result);

        return result;
    }, (error) => {
        console.log('error', error);
    });
}

当我在对象数组中传递路由时效果很好

protected applyRoutes(consumer: MiddlewaresConsumer) {
    consumer
        .apply(passport.authenticate('jwt', { session: false }))
        .forRoutes(...[
            { path: '/auth/authorized', method: RequestMethod.ALL },
            { path: '/auth/test', method: RequestMethod.ALL }]);

}
4

1 回答 1

1

使用MiddlewaresConsumer. 相反,注册一个异步组件(https://docs.nestjs.com/fundamentals/async-components),它将获取所有路径,例如 as AUTH_PATHS,然后将其注入您的模块类,假设并在方法中AuthModule使用此数组。configure()

于 2018-02-04T15:01:21.023 回答