Answers:
更新v8
以下答案有效,但会使您的应用程序面临XSS安全风险!。this.sanitizer.bypassSecurityTrustResourceUrl(url)
建议不要使用,而建议使用this.sanitizer.sanitize(SecurityContext.URL, url)
更新资料
对于RC.6 ^版本,请使用DomSanitizer
一个好的选择是使用纯管道:
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer} from '@angular/platform-browser';
@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}
transform(url) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
记住将新的添加SafePipe
到declarations
AppModule 的数组中。(如文档所示)
@NgModule({
declarations : [
...
SafePipe
],
})
html
<iframe width="100%" height="300" [src]="url | safe"></iframe>
如果您使用embed
标签,这可能对您很有趣:
旧版本RC.5
您可以DomSanitizationService
像这样利用:
export class YourComponent {
url: SafeResourceUrl;
constructor(sanitizer: DomSanitizationService) {
this.url = sanitizer.bypassSecurityTrustResourceUrl('your url');
}
}
然后绑定到url
您的模板中:
<iframe width="100%" height="300" [src]="url"></iframe>
不要忘记添加以下导入:
import { SafeResourceUrl, DomSanitizationService } from '@angular/platform-browser';
Pipe({ name: 'safe' }) export class SafePipe implements PipeTransform { constructor(private sanitizer: DomSanitizer) {} transform(url): any { return this.sanitizer.bypassSecurityTrustResourceUrl(url); } }
并在模板中调用<iframe width="100%" height="315" src="{{url}} | safe" frameborder="0" allowfullscreen></iframe>
。但这不适用于错误“不安全值”。请大家帮忙
[src]="url | safe"
只是去掉括号
this.sanitizer.sanitize(SecurityContext.URL, url)
,出现错误“错误错误:在资源URL上下文中使用了不安全的值”,II将其更改为this.sanitizer.bypassSecurityTrustResourceUrl(url)
可以正常工作。知道有什么问题吗?
this.sanitizer.sanitize(SecurityContext.URL, url)
无法正常工作,this.sanitizer.bypassSecurityTrustResourceUrl(url)
但在静态代码分析中提出了高度安全性漏洞问题,这使我无法将其移至生产环境。需要一种解决此问题的方法
这对我有用。
import { Component,Input,OnInit} from '@angular/core';
import {DomSanitizer,SafeResourceUrl,} from '@angular/platform-browser';
@Component({
moduleId: module.id,
selector: 'player',
templateUrl: './player.component.html',
styleUrls:['./player.component.scss'],
})
export class PlayerComponent implements OnInit{
@Input()
id:string;
url: SafeResourceUrl;
constructor (public sanitizer:DomSanitizer) {
}
ngOnInit() {
this.url = this.sanitizer.bypassSecurityTrustResourceUrl(this.id);
}
}
这适用于Angular 5.2.0
sarasa.Component.ts
import { Component, OnInit, Input } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
@Component({
selector: 'app-sarasa',
templateUrl: './sarasa.component.html',
styleUrls: ['./sarasa.component.scss']
})
export class Sarasa implements OnInit {
@Input()
url: string = "https://www.mmlpqtpkasjdashdjahd.com";
urlSafe: SafeResourceUrl;
constructor(public sanitizer: DomSanitizer) { }
ngOnInit() {
this.urlSafe= this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
}
}
sarasa.Component.html
<iframe width="100%" height="100%" frameBorder="0" [src]="urlSafe"></iframe>
多数民众赞成!
constructor(
public sanitizer: DomSanitizer, ) {
}
我已经挣扎了四个小时。问题出在img标签中。当使用方括号来表示“ src”时,例如:[src]。您不能使用此角度表达式{{}}。您只需直接从下面的对象示例中给出即可。如果您给出角度表达式{{}}。您将获得插值错误。
首先,我使用ngFor来遍历各个国家
*ngFor="let country of countries"
第二,您将其放入img标签。就是这个。
<img [src]="sanitizer.bypassSecurityTrustResourceUrl(country.flag)"
height="20" width="20" alt=""/>
我也遇到了这个问题,但是为了在我的角度模块中使用安全管道,我安装了安全管道npm软件包,您可以找到它 此处。仅供参考,这在Angular 9.1.3中有效,我没有在任何其他Angular版本中尝试过。逐步添加的方法如下:
通过npm install safe-pipe或yarn add safe-pipe安装软件包。这将在您的依赖项中将对它的引用存储在package.json文件中,从开始新的Angular项目开始应该已经具有该引用。
将SafePipeModule模块添加到Angular模块文件中的NgModule.imports中,如下所示:
import { SafePipeModule } from 'safe-pipe';
@NgModule({
imports: [ SafePipeModule ]
})
export class AppModule { }
通过以下方式将安全管道添加到要导入到NgModule中的Angular组件的模板中的元素中:
<element [property]="value | safe: sanitizationType"></element>
<div [style.background-image]="'url(' + pictureUrl + ')' | safe: 'style'" class="pic bg-pic"></div>
<img [src]="pictureUrl | safe: 'url'" class="pic" alt="Logo">
<iframe [src]="catVideoEmbed | safe: 'resourceUrl'" width="640" height="390"></iframe>
<pre [innerHTML]="htmlContent | safe: 'html'"></pre>
我通常如下添加单独的安全管道可重用组件
# Add Safe Pipe
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({name: 'mySafe'})
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {
}
public transform(url) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
# then create shared pipe module as following
import { NgModule } from '@angular/core';
import { SafePipe } from './safe.pipe';
@NgModule({
declarations: [
SafePipe
],
exports: [
SafePipe
]
})
export class SharedPipesModule {
}
# import shared pipe module in your native module
@NgModule({
declarations: [],
imports: [
SharedPipesModule,
],
})
export class SupportModule {
}
<!-------------------
call your url (`trustedUrl` for me) and add `mySafe` as defined in Safe Pipe
---------------->
<div class="container-fluid" *ngIf="trustedUrl">
<iframe [src]="trustedUrl | mySafe" align="middle" width="100%" height="800" frameborder="0"></iframe>
</div>
恭喜你!¨^^我为您提供了一种简单高效的解决方案,是的!
<iframe width="100%" height="300" [attr.src]="video.url"></iframe
[attr.src]而不是src“ video.url”,而不是{{video.url}}
太好了
unsafe value used in a resource URL context
<video> <source [src]=video.url type="video/mp4" /> Browser not supported </video>
实际上就像这样 ,也可以使用它来显示文档。.如果在使用视频标签时遇到任何麻烦,我在这里;)