gpt4 book ai didi

angular - JHipster : Receive 401 Unauthorized when making API call to microservice

转载 作者:太空狗 更新时间:2023-10-29 17:37:58 25 4
gpt4 key购买 nike

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

  • UAA - 授权服务器
  • API 网关
  • 微服务 - 产品 1
  • 服务发现 - 领事

其他组件:

  • 自定义前端(Angular 4)- 在一个单独的项目中

另外需要注意的是,自定义前端使用的是 Jhipster angular 4 代码,可以在普通的 Jhipster Api Gateway 中找到。这包括 customHttpProvider。

包含的类如下图所示: enter image description here

目前,我可以使用此设置成功登录并调用 UAA 上的 API,但是,当我尝试调用产品上的任何 API 时,我会收到 401 Unauthorised,因为例如发布到 Product1/api/zcd

这些服务在 Consul 中都是可见的和绿色的,网关也有 UAA 和 Product1 作为已注册和可用的路由。

enter image description here

到目前为止,我发现当我对 Product 进行 api 调用时,似乎没有调用 AuthInterceptor。我尝试手动将 jwt token 附加到方法,这解决了问题,但我不明白为什么 customHttpProvider 没有被用来拦截请求并附加 token 。

当我如图所示手动插入 token 时,我的 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());
}
}

最佳答案

已解决:

有两件事给我带来了麻烦。需要注意的重要一点是,它们没有直接链接到 JHipster,而是将 Jhipster 与以下内容集成时发生的问题:

问题:

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

解决方案:

    • 我已将 axon 3 包含到产品微服务中,并作为轴突配置,它初始化一个 token 存储(与与安全有关)。
    • MicroserviceSecurityConfiguration 中本应属于 JwtTokenStore 类型的 tokenStore bean 被覆盖为InMemoryTokenStore。
    • 解决方案是将 MicroserviceSecurityConfiguration 中的 tokenStore bean 重命名为 jwtTokenStore。
    • 我有很多延迟加载的模块。根据这种情况下的文档,SharedServiceModule 使用 forRoot() 并导入到 AppModule 中。
    • 但是,当我有一个服务时,例如在 ProductModule 中导入的 ProductService,它会覆盖在 SharedServiceModule 中导入的 Http Factory(在 AppModule 中导入 Http Factory 时的行为相同)。
    • 解决方案是创建一个与 customHttpProvider 函数(在 SharedServiceModule 中)在同一级别提供的 HttpProviderService。然后管理应用程序中较低级别的所有其他服务的 Http。 SharedServiceModule HttpProviderService

关于angular - JHipster : Receive 401 Unauthorized when making API call to microservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45862731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com