当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot解决跨域请求拦截问题代码实例

SpringBoot解决跨域请求拦截问题代码实例

2019年07月19日  | 移动技术网IT编程  | 我要评论

火星情报局第一季,于心何忍是什么意思,早婚依依

前言

同源策略:判断是否是同源的,主要看这三点,协议,ip,端口。

同源策略就是浏览器出于网站安全性的考虑,限制不同源之间的资源相互访问的一种政策。

比如在域名https://www.baidu.com下,脚本不能够访问https://www.sina.com源下的资源,否则将会被浏览器拦截。

注意两点:

1.必须是脚本请求,比如ajax请求。

但是如下情况不会产生跨域拦截

<img src="xxx"/>
<a href='xxx"> </a>

2.跨域拦截是前端请求已经发出,并且在后端返回响应时检查相关参数,是否允许接收后端请求。

在微服务开发中,一个系统包含多个微服务,会存在跨域请求的场景。

本文主要讲解springboot解决跨域请求拦截的问题。

搭建项目

这里创建两个web项目,web1 和 web2.

web2项目请求web1项目的资源。

这里只贴关键代码,完整代码参考github

web2

创建一个controller返回html页面

@slf4j
@controller
public class homecontroller {
@requestmapping("/index")
public string home(){
log.info("/index");
return "/home";
}
}

html页面 home.html

这里创建了一个按钮,按钮按下则请求资源:http://localhost:8301/hello

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>web2</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(function () {
$("#testbtn").click(function () {
console.log("testbtn ...");
$.get("http://localhost:8301/hello",function(data,status){
alert("数据: " + data + "\n状态: " + status);
});
})
})
</script>
</head>
<body>
web2
<button id="testbtn">测试</button>
</body>
</html>

web1

@slf4j
@restcontroller
public class web1controller {
@requestmapping("/hello")
public string hello(){
log.info("hello ");
return "hello," + new date().tostring();
}
}

这里配置两个项目为不同的端口。

web1为8301

web2为8302

因此是不同源的。

测试

在web1还没有配置允许跨域访问的情况下

按下按钮,将会出现错误。显示header中没有access-control-allow-origin

access to xmlhttprequest at 'http://localhost:8301/hello' from origin 'http://localhost:8300' has been blocked by cors policy: no 'access-control-allow-origin' header is present on the requested resource.

web1添加允许跨域请求,通过实现webmvcconfigurer

@configuration
public class webmvcconfig implements webmvcconfigurer {
@override
public void addcorsmappings(corsregistry registry) {
registry.addmapping("/hello");
}
}

再次访问将会返回正常数据。

除了以上的配置外,还可以做更细致的限制

比如对请求的headers,请求的方法post/get...。请求的源进行限制。

同时还可以使用注解 @crossorigin来替换上面的配置。

@slf4j
@restcontroller
public class web1controller {
@crossorigin
@requestmapping("/hello")
public string hello(){
log.info("hello ");
return "hello," + new date().tostring();
}
}

注解可以用在类上,也可以用在方法上,但必须是控制器类

配置和上面一样,也是可以对方法,header,源进行个性化限制。

@target({elementtype.method, elementtype.type})
@retention(retentionpolicy.runtime)
@documented
public @interface crossorigin {
/** @deprecated */
@deprecated
string[] default_origins = new string[]{"*"};
/** @deprecated */
@deprecated
string[] default_allowed_headers = new string[]{"*"};
/** @deprecated */
@deprecated
boolean default_allow_credentials = false;
/** @deprecated */
@deprecated
long default_max_age = 1800l;
@aliasfor("origins")
string[] value() default {};
@aliasfor("value")
string[] origins() default {};
string[] allowedheaders() default {};
string[] exposedheaders() default {};
requestmethod[] methods() default {};
string allowcredentials() default "";
long maxage() default -1l;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网