在我的例子中,我正在尝试扩展TS Window接口以包含polyfill fetch
.为什么无所谓.问题是" 我怎么告诉TS这window.fetch
是一个有效的函数? "
我在运行TS v.1.5(IIRC)的VS Code,v.0.3.0中这样做.
在我想要使用它的TS类文件中声明接口不起作用:
///<reference path="typings/tsd.d.ts"/>
interface Window {
fetch:(url: string, options?: {}) => Promise<any>
}
...
window.fetch('/blah').then(...); // TS objects that window doesn't have fetch
Run Code Online (Sandbox Code Playgroud)
但是如果我在一个单独的".d.ts"文件中声明这个相同的接口并在我的TS类文件中引用它,那就没关系.
这是"typings/window.extend.d.ts"
///<reference path="es6-promise/es6-promise"/>
interface Window {
fetch:(url: string, options?: {}) => Promise<any>
}
Run Code Online (Sandbox Code Playgroud)
现在我可以在我的TS类文件中使用它:
///<reference path="typings/window.extend.d.ts"/>
...
window.fetch('/blah').then(...); // OK
Run Code Online (Sandbox Code Playgroud)
或者,我可以在我的TS类文件中使用另一个名称编写扩展接口,然后在强制转换中使用它:
interface WindowX extends Window {
fetch:(url: string, options?: {}) => Promise<any>
}
...
(<WindowX> window).fetch('/blah').then(...); // OK
Run Code Online (Sandbox Code Playgroud)
为什么扩展接口工作在"d.ts"而不是就地? …
我正在开发一个web应用程序,包括UI-Angular,Server-Java,RestEasy 3.0.9.Final for rest api calls
当我试图从另一个域访问其余服务时,我得到以下错误
无法加载对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.
我配置我的服务器端以响应跨域调用,这是使用GET调用但POST调用正在创建ERROR
web.xml中
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.test.sample.app.CorsFeature</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.sample.app.Application</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
服务类
@GET
@Path("/getnameAtt")
@Produces(MediaType.APPLICATION_JSON)
public Response getHostnameAttributes() {
return Response
.status(200)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Headers",
"origin, content-type, accept, authorization")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS, HEAD")
.header("Access-Control-Max-Age", "1209600")
.entity(new TestImpl().getHostNameAttributes())
.build();
}
@POST
@Path("/getSeq") …
Run Code Online (Sandbox Code Playgroud) 我从brew安装了GPG.
brew install gpg
Run Code Online (Sandbox Code Playgroud)
这是gnupg2-2.0.30_2.
当我提交时,我收到一条错误消息:
You need a passphrase to unlock the secret key for
user: "Max Mustermann (mycomment) <mm@test.de>"
2048-bit RSA key, ID 1111AAAA, created 2017-01-05
error: gpg failed to sign the data
fatal: failed to write commit object
Run Code Online (Sandbox Code Playgroud)
我使用了命令:
gpg --list-secret-keys | grep ^sec
Run Code Online (Sandbox Code Playgroud)
它让我回来:
sec 2048R/1111AAAA 2017-01-05
Run Code Online (Sandbox Code Playgroud)
然后我用这个命令:
git config --global user.signingkey 1111AAAA
Run Code Online (Sandbox Code Playgroud)
commit给了我相同的错误消息.
我怎么解决这个问题?
我正在尝试创建一个html文件,它将数据从pouchDb同步到couchDb ..但是我在chrome控制台中遇到以下错误.
未捕获的TypeError:无法调用null的方法'addEventListener'
选项http:// localhost:5984/todos / 405(方法不允许)
选项http:// localhost:5984/todos /否请求资源上存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.XMLHttpRequest无法加载http:// localhost:5984/todos /.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.选项http:// localhost:5984/todos /否请求资源上存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.pouchdb-nightly.js:3496选项http:// localhost:5984/todos /否请求资源上存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.pouchdb-nightly.js:3496 XMLHttpRequest无法加载http:// localhost:5984/todos /.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.
我找到了避免这个问题的方法.这是我必须使用以下命令打开chrome:cd C:\ Program Files(x86)\ Google\Chrome\Application Chrome.exe --disable-web-security.But这并没有解决问题.我在这里看到了很多类似的问题.但我不明白任何答案,因为我在这个领域相对较新
我正在尝试通过nginx 1.6提供静态文件,并使用socket.io代理来自Node.js Web服务器的套接字流量.
这是nginx.conf的相关部分:
location /socket.io/ {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
Run Code Online (Sandbox Code Playgroud)
它在浏览器和Node.js之间直接完美地工作,但是当使用nginx 1.6代理时,socket.io需要太长时间.握手协议需要花费太多时间,但如果保持不间断,它最终会在几分钟后开始工作.
nginx提供的静态文件完美运行.
可能是什么问题呢?
更新:
我分析了一下网络流量,并确定以下请求大约持续一分钟(这正是请求升级时):
Sec-WebSocket-Key: LhZ1frRdl+myuwyR/T03lQ==
Cookie: io=1-A7tpvwmoGbrSvTAAA5
Connection: keep-alive, Upgrade
Upgrade: websocket
....
Run Code Online (Sandbox Code Playgroud)
预期的响应是代码101和:
Connection: upgrade
Sec-WebSocket-Accept: HXx3KKJadQYjDa11lpK5y1nENMM=
Upgrade: websocket
...
Run Code Online (Sandbox Code Playgroud)
相反,浏览器接收400并且:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:8888
Connection: keep-alive
Content-Type: application/json
Server: nginx/1.6.2
Transfer-Encoding: chunked
Run Code Online (Sandbox Code Playgroud)
更新2:
我确定相同的配置在我的办公室计算机上完美运行,这意味着它是我的家用电脑问题.无论如何,确定到底出了什么问题会非常好.
我有两个VS项目:一个暴露MVC5控制器,另一个是角客户端.我希望angular客户端能够查询控制器.我阅读了很多线程并尝试了以下内容:
我在服务器的web配置中添加了这个:
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<system.webServer>
Run Code Online (Sandbox Code Playgroud)我在控制器的动作上创建并使用了以下过滤器:
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)在angular客户端中,我创建了以下拦截器:
app.factory("CORSInterceptor", [
function()
{
return {
request: function(config)
{
config.headers["Access-Control-Allow-Origin"] = "*";
config.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
config.headers["Access-Control-Allow-Headers"] = "Content-Type";
config.headers["Access-Control-Request-Headers"] = "X-Requested-With, accept, content-type";
return config;
}
};
}
]);
app.config(["$httpProvider", function ($httpProvider) {
$httpProvider.interceptors.push("CORSInterceptor");
}]);
Run Code Online (Sandbox Code Playgroud)根据Firebug,这导致以下请求:
OPTIONS //Login/Connect HTTP/1.1
Host: localhost:49815
User-Agent: Mozilla/5.0 (Windows NT …
Run Code Online (Sandbox Code Playgroud) 我在chrome版本52.0.2743.82(64位)上使用fetch.我想获得响应中的所有标题.以下片段仅返回,content-type
但如果您查看chrome dev工具,则会显示许多其他响应标头.如何从fetch获取其他标头.
fetch('https://httpbin.org/get')
.then(response => {
const headers = response.headers.entries();
let header = headers.next();
while (!header.done){
console.log(headers.value);
header = header.next();
}
})
Run Code Online (Sandbox Code Playgroud)
我尝试了polyfilling(手动覆盖)github实现.仍然没有运气.
是否有任何实现或规范在<script>标记的属性中包含哈希或签名,以便浏览器可以在执行之前验证是否检索到了正确的文件?就像是:
<script
src="http://cdn.example.com/jquery-2001.js"
signature="sha-256/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
></script>
Run Code Online (Sandbox Code Playgroud)
其动机是这样的:通常,您为网站使用的每个额外CDN或主机都会增加您的漏洞,方法是添加可能被黑客攻击以破坏您网站的新目标.允许主要前端服务器断言这些文件的哈希值或签名可以完全消除这种风险,使您在设计架构时更加灵活.您甚至可以从不受信任的对等网络请求丢失的文件.
我以为我记得有关此的规范,但一直未能找到它.
我对CORS POST请求的安全性方面有点困惑.我知道网上有关于这个话题的信息丢失了,但我找不到我的问题的明确答案.
如果我理解正确,同源策略的目标是防止CSRF攻击,并且CORS的目标是在(并且仅当)服务器同意与其他站点上托管的应用程序共享其数据时启用资源共享(起源) ).
HTTP指定POST请求不是"安全"的,即它们可能会更改服务器的状态,例如通过添加新注释.当使用HTTP方法POST发起CORS请求时,如果请求的内容类型是非标准的(或者如果存在非标准的http标头),则浏览器仅执行"安全"预检请求.因此,执行带有标准内容类型和标准标头的POST请求,并且可能对服务器产生负面影响(尽管请求脚本可能无法访问响应.)
存在这种向每个表单添加随机令牌的技术,然后服务器需要将其作为每个非安全'请求的一部分.如果脚本试图伪造请求,它也可以
我的结论是,对标准内容类型和标题的伪造POST请求的唯一保护是上述技术(或类似的技术).对于任何其他非安全'请求,例如PUT或DELETE,或具有json内容的POST,使用该技术不是必需的,因为CORS执行'安全'OPTIONS请求.
为什么CORS的作者将这些POST排除在预检请求之外,因此有必要采用上述技术?
要使用SharedArrayBuffer
,我们必须添加两个响应标头:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使用 GitHub Pages 添加这些标头吗SharedArrayBuffer
?
github-pages webassembly sharedarraybuffer cross-origin-embedder-policy cross-origin-opener-policy
cors ×7
javascript ×3
fetch-api ×2
preflight ×2
angularjs ×1
asp.net-mvc ×1
couchdb ×1
cross-origin-embedder-policy ×1
cryptography ×1
fetch ×1
git ×1
github-pages ×1
gnupg ×1
html ×1
html5 ×1
macos ×1
nginx ×1
post ×1
pouchdb ×1
rest ×1
resteasy ×1
security ×1
socket.io ×1
typescript ×1
webassembly ×1