我目前正在使用 Jhipster 生成以下组件:
- UAA - 身份验证服务器
- API 网关
- 微服务 - 产品1
- 服务发现 - 领事
其他组件:
- 自定义前端 (Angular 4) - 在一个单独的项目中
还需要注意的是,自定义前端使用了 Jhipster angular 4 代码,该代码可以在 vanilla Jhipster Api Gateway 中找到。这包括 customHttpProvider。
目前,我可以使用此设置成功登录并调用 UAA 上的 API,但是,当我尝试调用 Product 上的任何 API 时,我得到401 Unauthorized,例如 Post to Product1/api/zcd。
这些服务在 Consul 中都是可见的和绿色的,网关也有 UAA 和 Product1 作为注册和可用的路由。
到目前为止,我发现当我对 Product 进行 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());
}
}