【问题标题】:Unsafe value used in a resource URL context (iframe)资源 URL 上下文 (iframe) 中使用的不安全值
【发布时间】:2020-05-28 11:23:28
【问题描述】:

尝试将值作为 iframe url 从本地数据库传递,并且我收到错误消息: 资源 URL 上下文中使用的不安全值。 我正在尝试显示一系列打印机 ip,这样我就可以通过网站检查它们的状态,无论如何在没有 iframe 的情况下可以做到这一点? 我会很高兴听到一些建议。

我是 Angular 的新手,每一个帮助都更受欢迎 提前致谢。

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DomSanitizer } from '@angular/platform-browser';



@Component({
  selector: 'app-value',
  templateUrl: './value.component.html',
  styleUrls: ['./value.component.css']
})


export class ValueComponent implements OnInit {
  values: any;



  constructor(private http: HttpClient, private sanitizer: DomSanitizer) { }

  

ngOnInit() {
  this.getValues();


}

getValues() {
  this.http.get('http://localhost:5000/api/values/').subscribe(response => {
    this.values = response;
  }, error => {
    console.log(error);
  })
}

}
<H2>Print Manager</H2>


<div id="outerdiv">
  <iframe src="http://192.168.1.6/general/status.html" id="inneriframe" scrolling="no"  ></iframe>
</div>



<div *ngFor="let value of values">
<p>{{value.id}},{{value.hostName}},{{value.location}},{{value.manufacturer}},{{value.ip}}</p>

<span>Hostname: {{value.hostName}}</span>
<br>
<span>Location: {{value.location}}</span>
<br>
<span>Manufacturer: {{value.manufacturer}}</span>
<br>
<span>IP: {{value.ip}}</span>

</div>

【问题讨论】:

    标签: javascript html asp.net angular typescript


    【解决方案1】:

    使用管道更好。

    创建一个Pipeapp.component.ts(首先加载的主要组件)*

    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);
      }
    }
    

    声明管道: 创建管道后

    @NgModule({
      imports: [ ... ],
      declarations: [ ..., SafePipe ],
      bootstrap: [ App ]
    })
    

    用法:您可以将它与管道运算符和管道名称一起使用。 示例:

     <iframe [src]="your_url_here | safe" id="inneriframe" scrolling="no"  ></iframe>
    

    使用此方法,您将获得一个经过净化的 URL,而无需每次在其他一些组件中需要它时都重写代码..

    【讨论】:

      【解决方案2】:

      [src]="sanitizer.bypassSecurityTrustResourceUrl('http://'+value.ip+'/general/status.html')" 帮我解决了

      【讨论】:

        【解决方案3】:

        你可以使用来自@angular/platform-browser的Angular DomSanitizer

        更多信息请参考Angular Guide

        import {DomSanitizationService} from '@angular/platform-browser';
        
        @Component({
          templateUrl: 'temp.html'
        })
        export class AppComponent {
        yourUrl : '';
          constructor(private sanitizer : DomSanitizationService) {  
          }
        
          getSanitizedURL() {
            return this.sanitizer.bypassSecurityTrustUrl(yourUrl);
          }
        }
        

        HTML:

           <div id="outerdiv">
              <iframe src="getSanitizedURL()" id="inneriframe" scrolling="no"></iframe>
            </div>
        

        【讨论】:

        • 应用此解决方案后仍显示错误
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多