当前位置:网站首页>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; } }
边栏推荐
- 【Leetcode】470. Implement Rand10() Using Rand7()
- 【MySQL系列】MySQL索引事务
- asyncawait和promise的区别
- 在MySQL中使用MD5加密【入门体验】
- @WebServlet注解(Servlet注解)
- WEB安全基础 - - - XRAY使用
- Use Jenkins for continuous integration, this knowledge point must be mastered
- Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程
- Quartus 使用 tcl 文件快速配置管脚
- C language - branch statement and loop statement
猜你喜欢
The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
Use Jenkins for continuous integration, this knowledge point must be mastered
C language - branch statement and loop statement
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
【Leetcode】1206. Design Skiplist
CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
【C语言进阶】文件操作(二)
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
在MySQL中使用MD5加密【入门体验】
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
随机推荐
CDH6 Hue to open a "ASCII" codec can 't encode characters
【MySQL篇】初识数据库
Docker实践经验:Docker 上部署 mysql8 主从复制
分享一份接口测试项目(非常值得练手)
架构基本概念和架构本质
伸展树的特性及实现
中职网络安全竞赛B7比赛部署流程
The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
TexturePacker使用文档
经典文献阅读之--DLO
Excel表格数据导入MySQL数据库
Flink学习第三天——一文带你了解什么是Flink流?
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
Loading configuration of Nacos configuration center
[Camp Experience Post] 2022 Cybersecurity Summer Camp
数据机构---第五章树与二叉树---二叉树的概念---应用题
【Leetcode】475. Heaters
斜堆、、、
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections