当前位置:网站首页>security跨域配置
security跨域配置
2022-08-01 23:52:00 【程序三两行】
一、概述
1、什么是跨域
CORS是w3c指定的一种跨域资源共享的请求资源方式,体现就是协议+ip+端口三个相同才是同源,否则就是跨域,早期javaEE解决跨域方案是JSONP,Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获取资料,即跨域读取数据。但是jsonp只支持get方式,而CORS支持多种请求方式,是目前主流的跨域解决方案
2、CORS解决跨域过程
cors新增了一组http请求头字段,通过这些字段,服务器告诉浏览器,哪些网络通过浏览器有访问权限,同时规定,对那些可能修改服务器数据的http请求方法(如get以为的http请求等),浏览器首先必须使用options请求发起一个预检请求,预检请求的目的就是查看服务器是否支持即将发起的跨域请求,如果服务端允许才发实际的http请求,在预检请求的返回中,服务端也可以通知客户端,是否需要携带身份凭证,如cookies、http认证信息等
3、简单请求

4、复杂请求

二、跨域解决方案
1、springmvc中的注解方式@CrossOrigin
该注解可以加在方法上也可以加在controller类上,加在类上所有方法支持跨域,@CrossOrigin支持的属性如下
- alowCredentials:浏览器是否应当发送凭证信息入Cookie
- allowedHeaders:请求被允许的请求头字段 * 标识所有字段
- exposedHeaders:哪些响应头可以作为响应的一部分暴露出来
- maxAge:预检请求的有效期 有效期内不必再次发送预检请求 默认是1800秒
- methods:允许的请求方法 * 标识允许所有的方法
- origins:允许的域 可以多个,* 标识允许所有的域
@RequestMapping("/touser")
@CrossOrigin(origins = {"localhost:8081","localhost:8082"})
public String toUser(){
return "user";
}2、springmvc中的配置方式
自定义springmvc配置WebMvcConfigurer类中的addCorsMappings方法 全局处理
@Configuration
public class ConfigMy implements WebMvcConfigurer {
/**
* 跨域
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")//对哪些请求进行跨域
.allowedOrigins("http://localhost:8082","http://localhost:8081","http://localhost:8080")
.allowCredentials(false)
.allowedMethods("GET", "POST")
.allowedHeaders("*")
.maxAge(3600);
}
}3、spring web过滤器CrosFilter方式
@Configuration
public class ConfigMy {
@Bean
FilterRegistrationBean<CorsFilter> corsFilter(){
FilterRegistrationBean<CorsFilter> registrationBean = new FilterRegistrationBean<CorsFilter>();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowedHeaders(Arrays.asList("*"));
corsConfiguration.setAllowedMethods(Arrays.asList("*"));
corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:8080"));
corsConfiguration.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",corsConfiguration);
registrationBean.setFilter(new CorsFilter(source));
//指定filter顺序 -1标识在所有内置filter之前执行
registrationBean.setOrder(-1);
return registrationBean;
}
}4、springsecurity跨域解决方案
引入security之后上面的@ CrossOrigin和配置方式都会失效,crosfilter是否失效取决于过滤器和security自带过滤器顺序
filter 、dispatchserServlet以及intercepter执行顺序
client->web filter(sercurity filter)->dispatchserServlet->intercepter->controller
对于非简单请求都会发起一个预检请求,预检请求不会携带认证信息,所以会被security拦截,因此通过@ CrossOrigin和配置处理跨域都是失效的,如果crosfilter执行顺序高于security,那有效的,security解决跨域方式
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and().formLogin()
//增加跨域以及相关配置
.and().cors().configurationSource(configurationSource())
.and().csrf().disable();
}
//跨域配置
CorsConfigurationSource configurationSource(){
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowedHeaders(Arrays.asList("*"));
corsConfiguration.setAllowedMethods(Arrays.asList("*"));
corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:8080"));
corsConfiguration.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",corsConfiguration);
return source;
}
}
边栏推荐
- cmd command
- 1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?
- [Camp Experience Post] 2022 Cybersecurity Summer Camp
- numpy.isclose
- 20220725资料更新
- [C language advanced] file operation (2)
- Programmer is still short of objects? A new one is enough
- 【Leetcode】473. Matchsticks to Square
- 【MySQL系列】 MySQL表的增删改查(进阶)
- 数据机构---第五章树与二叉树---二叉树的概念---应用题
猜你喜欢

深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练

thinkphp漏洞总结

A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing

在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错

Chrome书签插件,让你实现高效整理

Excel表格数据导入MySQL数据库

Various Joins of Sql

Flink Yarn Per Job - Yarn应用

UI自动化测试框架搭建-标记性能较差用例

很多人喜欢用多御安全浏览器,竟是因为这些原因
随机推荐
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
numpy.isclose
Department project source code sharing
经典文献阅读之--DLO
Get piggy homestay (short-term rental) data
Loading configuration of Nacos configuration center
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
nodejs--process
GIF制作-灰常简单的一键动图工具
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed
Flink学习第三天——一文带你了解什么是Flink流?
Excel文件读写(创建与解析)
ES中SQL查询详解
邻接表与邻接矩阵
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
Flink Yarn Per Job - 提交流程一
多御安全浏览器android版更新至1.7,改进加密协议