例如以下代码:
对应的html页面中是这样:<iframe class=“filling” [src] = “iframe”>
在这里直接对src进行动态绑定变量就会出现:unsafe value used in a resource URL context (see http://g.co/ng/security#xss)问题。
查看官方文档会发现angular在这方面做了关于防止xss攻击的安全机制,以上的做法是不符合要求的。
解决办法:
1.在对应ts文件中,导入DomSanitizer,SafeResourceUrl
import {DomSanitizer, SafeResourceUrl} from ‘@angular/platform-browser’;
2、引用DomSanitizer,SafeResourceUr
pageUrl:string;
iframe: SafeResourceUrl;
constructor(public domSanitizer:DomSanitizer,public commonConfig: CommonConfig ) {
this.pageUrl = this.commonConfig.activitUrl sessionStorage.getItem(‘property’);
this.iframe = this.domSanitizer.bypassSecurityTrustResourceUrl(
this.pageUrl);
}