Angular 忽略证书签名
创始人
2024-10-19 07:01:03
0

在Angular中,可以通过设置HTTP客户端的配置来忽略证书签名。以下是一个示例解决方法:

  1. 创建一个新的Angular服务,例如http-client.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHandler, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class HttpClientService extends HttpClient {
  constructor(handler: HttpHandler) {
    super(handler);
  }

  request(method?: any, url?: any, options?: any): Observable {
    if (!options) {
      options = {};
    }
    options.withCredentials = true; // 忽略证书签名

    return super.request(method, url, options)
      .pipe(
        catchError((error: HttpErrorResponse) => {
          return throwError(error);
        })
      );
  }
}
  1. 在根模块(通常是app.module.ts)中,将HttpClientService添加到providers数组中,以便在整个应用程序中使用它。
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpClientService } from './http-client.service';

@NgModule({
  imports: [
    HttpClientModule
  ],
  providers: [
    {
      provide: HttpClientService,
      useClass: HttpClientService
    }
  ]
})
export class AppModule { }
  1. 在需要使用HTTP客户端的地方,使用HttpClientService代替原始的HttpClient
import { Component } from '@angular/core';
import { HttpClientService } from './http-client.service';

@Component({
  selector: 'app-example',
  template: `
    
  `
})
export class ExampleComponent {
  constructor(private httpClient: HttpClientService) {}

  makeRequest() {
    this.httpClient.get('https://example.com/api/data')
      .subscribe(
        response => {
          console.log(response);
        },
        error => {
          console.error(error);
        }
      );
  }
}

以上示例中,HttpClientService继承自HttpClient,在每次请求时将options.withCredentials设置为true,以忽略证书签名。

相关内容

热门资讯

此事备受玩家关注!微乐小程序黑... 此事备受玩家关注!微乐小程序黑科技免费,微信小程序微乐辅助器ios(透视)一贯存在有辅助教程(哔哩哔...
透视窍门!德州局脚本(德普之星... 透视窍门!德州局脚本(德普之星辅助软件)本来有辅助app(哔哩哔哩)1、德普之星辅助软件破解器简单,...
黑科技插件!微乐小程序免费黑科... 黑科技插件!微乐小程序免费黑科技,微信小程序微乐辅助器脚本(透视)总是是有辅助插件(哔哩哔哩)所有人...
透视绝活儿!wpk俱乐部有没有... 透视绝活儿!wpk俱乐部有没有辅助(WePoKer软件)真是有辅助工具(哔哩哔哩)1、wpk俱乐部有...
透视好友房!微乐小程序黑科技,... 透视好友房!微乐小程序黑科技,微乐智能插件激活码(透视)本来是真的辅助技巧(哔哩哔哩)暗藏猫腻,小编...
透视方针!hhpoker脚本(... 透视方针!hhpoker脚本(WePoKer脚本)真是真的是有辅助方法(哔哩哔哩)该软件可以轻松地帮...
辅助透视!微乐小程序黑科技免费... 辅助透视!微乐小程序黑科技免费,微信微乐自建房脚本免费下载(透视)其实是真的辅助app(哔哩哔哩)所...
透视法子!wepoker有没有... 透视法子!wepoker有没有辅助(HHpoker透视挂)竟然真的是有辅助工具(哔哩哔哩)1)wep...
透视模拟器!微信微乐游戏辅助脚... 透视模拟器!微信微乐游戏辅助脚本,微乐自建房辅助入口官网(透视)好像是真的辅助器(哔哩哔哩)1、下载...
透视模块!pokemomo辅助... 透视模块!pokemomo辅助工具(WePoKer机器人)一贯是真的有辅助神器(哔哩哔哩)pokem...