关于跨域问题

自己折腾了一段时间的js,总是卡在跨域问题。

比如,自己用Java写了个后端。某一天,需要测试angular js的post请求是否正确,便想到了这个服务端,当请求的时候提示“跨域”。

今天,需要得到iframe页面对应的url,也是因为跨域,导致失败。

刚刚看到了一篇文章,说跨域问题是为了用户安全,讲的很有道理,看来以为遇到这类问题,只能想别的办法了。

原文链接:http://stackoverflow.com/questions/26055152/how-to-get-current-page-loaded-url-from-iframe

For a matter of security you are allowed to retrieve the URL as long as the contents of the iframe, and the referencing javascript, are hosted in the same domain.

Should it be the case, you can do something like:

document.getElementById("frameid").contentWindow.location.href

If the two domains are different then you'll have all the restrictions that apply to the cross-site reference scripting domain. Example:

document.getElementById("frameid").src ='/';
alert(document.getElementById("frameid").documentWindow.location.href);

Error: Permission denied to get property Location.href

For sure (except if you find some huge security flaw in your browser) you simply cannot achievewhat you need using javascript in the parent document. Let's see with a simple example why. If the browser allowed what you need, you could easily:

  1. Create a page, with a hidden iframe (e.g. http://malicous.com/dont-trust)
  2. In that iframe, open a child page with the login process of some website (e.g. http://insecure-web-site.com/redirectlogin)
  3. If cookies for child are present and under certain circumstances, the page inside the frame will redirect to the real website, proceeding with user login.
  4. From the parent page now you will be able to read all the sensitive informations gone through the login process contained inside the URL, e.g. access tokens, session IDs, ...
  5. At this point the victim website and its users are in front of a wide new set of possible security threats...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot在处理跨域请求时,通常是为了支持前端与后端分离的应用架构中,当客户端(如浏览器)发起 AJAX 请求到不同的域名或端口时,服务器默认会因为同源策略(Same-Origin Policy)而拒绝这些请求。为了解决这个问题,Spring Boot提供了几种方法来处理跨域。 1. **全局启用CORS**: 你可以通过`@EnableWebMvc`注解加上`spring.mvc.cross-origin.enabled=true`配置,然后在`application.properties`或`application.yml`文件中添加CORS相关配置,比如允许特定来源、方法和头信息: ```yaml spring: mvc: cors: enabled: true origins: '*' 或 'http://localhost:8080' // 允许特定或所有来源 allowedMethods: '*' // 允许的所有HTTP方法 allowedHeaders: '*' // 允许的所有请求头 ``` 2. **全局注册CORS Filter**: 使用`@CrossOrigin`注解可以全局注册一个CORS Filter,例如: ```java @Configuration @WebFilter(urlPatterns = "/*") public class CorsConfig implements WebFilterConfigurer { @Override public void configureWebFilter(WebFilterRegistry registry) throws Exception { registry.addFilter(CorsFilter.class).addMappingForAllUrls().applyPermitDefaultValues(); } } ``` 3. **控制器级别处理**: 如果只需要某个或部分Controller响应跨域,可以在方法上使用`@CrossOrigin`: ```java @RestController @CrossOrigin(origins = "*", methods = RequestMethod.GET) public class MyController { @GetMapping("/api") public String crossDomainApi() { // ... } } ``` 4. **自定义CORS策略**: 如果需要更灵活的控制,可以创建`CorsConfiguration`实例并动态配置: ```java @Bean public CorsConfigurationSource corsConfigurationSource() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); // ... 设置配置项 source.registerCorsConfiguration("/**", config); return source; } @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*"); } }; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值