代码之家  ›  专栏  ›  技术社区  ›  MichaelS

JHipster:对微服务进行API调用时接收401未经授权

  •  6
  • MichaelS  · 技术社区  · 7 年前

    我目前正在使用Jhipster生成以下组件:

    • UAA-身份验证服务器
    • API网关
    • 微服务-产品1

    其他组件:

    还需要注意的是,自定义前端使用Jhipster angular 4代码,可以在vanilla Jhipster Api网关中找到。 这包括customHttpProvider。

    enter image description here

    目前,我能够使用此设置成功登录并在UAA上调用API,然而,当我尝试在产品上调用任何API时,我得到了一个 ,例如,发布到 产品1/api/zcd .

    这些服务在Consor中都是可见的和绿色的,Gateway还将UAA和Product1作为注册和可用的路由。

    enter image description here

    到目前为止,我发现在对产品进行api调用时,似乎没有调用AuthInterceptor。 我尝试手动将jwt令牌附加到方法中,这解决了问题,但我无法理解为什么customHttpProvider没有被用来拦截请求并附加令牌。

    当我手动插入令牌时,下面的ProductService可以工作,如图所示,但这显然不是正确的方法。

    @Injectable()
    export class ProductService {
    
        private options = new Headers();
    
        constructor(private http: Http) {
            this.options.append('Authorization', 'Bearer ' + 'token is inserted here');
        }
    
        priceProduct(productPriceRequest: productPriceRequest): Observable<IdResponse> {
            return this.http.post('Product1/api/zcd', productPriceRequest, { headers: this.options })
                .map(response => response.json());
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   MichaelS    7 年前

    解决了的:

    问题:

    1. 轴突3
    2. 具有延迟加载模块的自定义Angular 4前端。

    解决:

      • axons配置,它初始化令牌存储(没有任何 与安全有关)。
      • InMemoryTokenStore。
      • 我有许多延迟加载的模块。根据这种情况下的文档,有一个SharedServiceModule使用forRoot()并导入到AppModule中。
      • 然而,当我有一个服务。e、 在ProductModule中导入的g ProductService中,它覆盖了在SharedServiceModule中导入的Http工厂(与在AppModule中导入Http工厂时的行为相同)。
      • SharedServiceModule HttpProviderService