• angular请求头部加XSRFTOKEN


    1、创建拦截器

    import {
        HttpEvent,
        HttpInterceptor,
        HttpHandler,
        HttpRequest,
      } from '@angular/common/http';
    import { Observable } from 'rxjs';
      
      export class AddHeaderInterceptor implements HttpInterceptor {
        intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
          // Clone the request to add the new header
            let xsrftoken = ''
            document.cookie.split(';').forEach(it => {
                if (it.split('=')[0]=='XSRF-TOKEN'||it.split('=')[0]==' XSRF-TOKEN') {
                xsrftoken = it.split('=')[1]
                }
            })
          const clonedRequest = req.clone({ headers: req.headers.set('X-XSRF-TOKEN', xsrftoken) });
      
          // Pass the cloned request instead of the original request to the next handle
          return next.handle(clonedRequest);
        }
      }
    

    2、用HTTP_INTERCEPTORS提供的注册

    import { HTTP_INTERCEPTORS } from '@angular/common/http';
    
    @NgModule({
      providers: [{
        provide: HTTP_INTERCEPTORS,
        useClass: AddHeaderInterceptor,
        multi: true,
      }],
    })
    export class AppModule {}
    

    参照:https://www.cnblogs.com/sugartang/p/14338731.html

  • 相关阅读:
    隔离级别
    分析Hello2代码
    正则表达式
    Filter and servlet
    部署描述符
    Annotation
    LDAP and Implementation
    Restful levels and Hateoas
    servlet injection analysis
    隔离级别
  • 原文地址:https://www.cnblogs.com/boreguo/p/15927429.html
Copyright © 2020-2023  润新知