博客
关于我
Vue+Spring Security前后端交互如何处理跨域请求
阅读量:294 次
发布时间:2019-03-01

本文共 2337 字,大约阅读时间需要 7 分钟。

问题复现

Access to XMLHttpRequest at 'http://localhost:8081/login' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

在这里插入图片描述

问题原因

出现这个问题的原因就是我的后端没有写好跨域配置,或者说我刚开始不知道跨域如何配置。后端我是用的是Spring boot+Spring Security+Jwt实现的。刚开始我进行了如下配置:

public class AccessControlAllowOriginFilter implements WebMvcConfigurer {       @Override    public void addCorsMappings(CorsRegistry registry) {           registry.addMapping("/**")                .allowedHeaders("Content-Type","X-Requested-With","accept,Origin","Access-Control-Request-Method","Access-Control-Request-Headers","token")                .allowedMethods("PUT", "DELETE", "GET", "POST", "OPTIONS")                .allowedOrigins("*")                .exposedHeaders("access-control-allow-headers",                        "access-control-allow-methods",                        "access-control-allow-origin",                        "access-control-max-age",                        "X-Frame-Options")                .allowCredentials(true);    }}
//解决跨域问题。cors 预检请求放行,让Spring security 放行所有preflight request(cors 预检请求)      http.authorizeRequests().requestMatchers(CorsUtils::isPreFlightRequest).permitAll();

但是请求还是报错,这两种配置确认无效。

解决办法

在前后端交互的过程中,跨域是最基本的问题,后端可以同意进行跨域处理,前端也可以通过代理进行跨域处理。

我这里采用的是在后端配置跨域请求。
在查看了Spring Security官方文档以后找到了正确的跨域请求方法。

查看完官方文档以后我进行了如下配置:

@Bean    CorsConfigurationSource corsConfigurationSource() {           CorsConfiguration configuration = new CorsConfiguration();        configuration.addAllowedOrigin("*");//修改为添加而不是设置,* 最好改为实际的需要,我这是非生产配置,所以粗暴了一点        configuration.addAllowedMethod("*");//修改为添加而不是设置        configuration.addAllowedHeader("*");//这里很重要,起码需要允许 Access-Control-Allow-Origin        configuration.setAllowCredentials(true);        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();        source.registerCorsConfiguration("/**", configuration);        return source;    }

在websecurityconfigure的configure(HttpSecurity http)方法里面还要加上:

http.cors(Customizer.withDefaults());

至此,跨域请求完美解决。

其实跨域请求在第一次请求时浏览器都会先发起一个preflight request,这是要进行如下配置才可以进行跨域访问:

//解决跨域问题。cors 预检请求放行,让Spring security 放行所有preflight request(cors 预检请求)        http.authorizeRequests().requestMatchers(CorsUtils::isPreFlightRequest).permitAll();

有了这些配置前端就可以进行跨域请求了。

转载地址:http://iteo.baihongyu.com/

你可能感兴趣的文章
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>
mysql 的GROUP_CONCAT函数的使用(group_by 如何显示分组之前的数据)
查看>>
MySQL 的instr函数
查看>>
MySQL 的mysql_secure_installation安全脚本执行过程介绍
查看>>