首页 > 解决方案 > 在 Angular 8 中使用 JWT 和 SwaggerUI Bundle 进行预身份验证

问题描述

我正在尝试将 SwaggerUiBundle 集成到角度应用程序中。从创建的 url 获取请求时,我不必再次授权它并使用存储在 localStorage 中的当前 JWT 令牌发送以在 swagger 中使用 CRUD 请求。我无法将令牌存储在任何地方。我需要一些关于这个问题的帮助。谢谢大家。

ngOnInit() {

const ui = SwaggerUIBundle({
  dom_id: '#swagger-ui',
  layout: 'BaseLayout',
  presets: [
    SwaggerUIBundle.presets.apis,
    SwaggerUIBundle.SwaggerUIStandalonePreset
  ],
  url: url,
  docExpansion: 'none',
  operationsSorter: 'alpha',
  securityDefinitions: {
    JWT: {
      type: "apiKey",
      description: "",
      name: "Authorization",
      in: "header"
    }
  },
  // requestInterceptor: function(request){
  //   console.log('[Swagger] intercept try-it-out request');
  //   let currentUser = hInterceptor.userService.currentUserValue;
  //   var token = `Bearer ${currentUser.token}`
  //   request.headers.Authorization = token;
  //   return request;
  // }
  // requestInterceptor: function(request){
  //   var rq = new HttpRequest(
  //     "GET",
  //     request.url,
  //     request.body, {
  //     headers: new HttpHeaders(request.headers),
  //     responseType: request.responseType,
  //   })
  //   hInterceptor.intercept(rq, new HttpXhrBackend({ build: () => new XMLHttpRequest }));
  // }
  }

标签: angularswagger-uijwt-auth

解决方案


推荐阅读