当前位置:网站首页>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; } }
边栏推荐
- LocalDateTime转为Date类型
- 在MySQL中使用MD5加密【入门体验】
- @WebServlet注解(Servlet注解)
- How to better understand and do a good job?
- 6132. 使数组中所有元素都等于零-快速排序法
- Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
- color transparency parameter
- Oracle database is set to read-only and read-write
- 一道golang中关于iota的面试题
- 【Leetcode】479. Largest Palindrome Product
猜你喜欢
随机推荐
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
asyncawait和promise的区别
thinkphp漏洞总结
Flink学习第四天——完成第一个Flink 流批一体案例
Making a Simple 3D Renderer
ELK日志采集
6134. Find the closest node to the given two nodes - force double hundred code
类型“FC<Props>”的参数不能赋给类型“ForwardRefRenderFunction<unknown, Props>”的参数。 属性“defaultProps”的类型不兼容。 不
问题解决方式了
检查 Oracle 版本的 7 种方法
如何进行数据库备份
Chapter 12 End-User Task As Shell Scripts
如何用Redis实现分布式锁?
字节跳动面试官:请你实现一个大文件上传和断点续传
Work for 5 years, test case design is bad?To look at the big case design summary
【ACWing】406. 放置机器人
Use Jenkins for continuous integration, this knowledge point must be mastered
oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
@Resource和@Autowired的区别