vue和springboot分页开发实现(解决Vue+SpringBoot+Shiro跨域问题)
vue和springboot分页开发实现
解决Vue+SpringBoot+Shiro跨域问题目录
- 一、配置Vue前端
- 1、开发跨域配置
- 2、生产跨域配置
- 二、配置spring boot
相信大家刚开始做都会遇到这个问题,在网上找了好多也不管用,都写的不全,
在这里记录一下,希望对大家有所帮助
一、配置Vue前端在config下index.js中配置代理信息
注意:这里的跨域配置只在开发环境中有效,打包部署后,这个跨域就不起作用了,本人也是这里卡了好久,Vue前端打包后,最好部署到nginx上,用nginx可以直接解决跨域问题
1、开发跨域配置
proxyTable: { '/api': { target: 'http://xxxx.com', //地址 changeOrigin: true, pathRewrite: { '^/api': '' }, } },
在main.js中配置Ajax代理请求
var axios = require('axios') axios.defaults.baseURL = '/api' //环境
然后就是我们写请求方法的时候在方法前加上“/api”,这个是根据你的配置名,配的啥名就写啥
这样我们前端Vue开发跨域就配置完了
2、生产跨域配置
首先我们看一下代码配置
在网上看了大量的文章资料,说是修改这个,修改那个,事实却是然并卵。。。。
其实我们只需要在config下的index.js中配置好代理信息
proxyTable: { '/api/*': { target: 'http://域名', //生产地址一定要加http changeOrigin: true, pathRewrite: { '^/api': '/api' }, } },
上面我们在配置本地跨域的时候设置了axios默认的请求路径,生产打包不需要配置
这样我们代码这里就配置完了,其他的都不要动,然后npm run build 打包就可以了
剩下的事情就交给nginx就可以了,我是在windows服务上部署的nginx,这个安装步骤网上一大堆,这里就不说了
我们安装好nginx后,需要进行一些配置
1、删除nginx下html目录里的内容
2、将我们Vue打好的包dist复制到nginx的html目录下,
3、配置nginx下config目录下nginx.conf,配置内容如下:
这里说明一下:nginx应用的文件目录名改一下,我们直接安装完都是nginx-1.xx,类似这样的目录,我们在配置上图中的root路径时,/n可能会有编译问题,我这里是改成了ProNginx,大家可以改为自己喜欢的名
这是我nginx的所有配置
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name 前台服务域名/IP; root D:/HWKJ/ProNginx/ProNginx/html/dist/; location / { index index.php index.html index.htm; try_files $uri $uri/ /index.html; } location /api/ { #rewrite ^.+api/?(.*)$ /$1 break; #include uwsgi_params; proxy_pass http://xxx后台xxxx/api/; # 解决springboot中获取远程ip的问题 } } }
配置完后我们启动nginx,以下是nginx一些操作命令
start nginx //启动 nginx -s stop // stop是快速停止nginx,可能并不保存相关信息 nginx -s quit // quit是完整有序的停止nginx,并保存相关信息 nginx -s reload // 当配置信息修改,需要重新载入这些配置时使用此命令 nginx -s reopen // 重新打开日志文件 nginx -v // 查看Nginx版本
这样我们前端Vue生产跨域就配置完了
下面我们配置spring boot后台
二、配置spring boot如果说你是单只有spring boot那么你配置一下信息即可
import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.*; /** */ @Configuration public class MyWebConfigurer implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 允许跨域访问的路径 .allowCredentials(true) // 是否发送cookie .allowedOriginPatterns("*") // 允许跨域访问的源 .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") // 允许请求方法 .allowedHeaders("*") // 允许头部设置 .maxAge(168000) ; // 预检间隔时间 } }
如果你的spring boot后台整合了shiro,那上面的配置对走shiro的请求不会生效,浏览器还是会提示跨域,因此我们用下列方法设置允许跨域访问
import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.*; /** */ @Configuration public class MyWebConfigurer implements WebMvcConfigurer { @Bean public FilterRegistrationBean corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration config = new CorsConfiguration(); // 允许cookies跨域 config.setAllowCredentials(true); // #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin config.addAllowedOriginPattern("*"); // #允许访问的头信息,*表示全部 config.addAllowedHeader("*"); // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了 config.setMaxAge(18000L); // 允许提交请求的方法,*表示全部允许 config.addAllowedMethod("OPTIONS"); config.addAllowedMethod("HEAD"); config.addAllowedMethod("GET"); config.addAllowedMethod("PUT"); config.addAllowedMethod("POST"); config.addAllowedMethod("DELETE"); config.addAllowedMethod("PATCH"); source.registerCorsConfiguration("/**", config); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); // 设置监听器的优先级 bean.setOrder(0); return bean; } }
到此这篇关于解决Vue+SpringBoot+Shiro跨域问题的文章就介绍到这了,更多相关Vue SpringBoot Shiro跨域内容请搜索开心学习网以前的文章或继续浏览下面的相关文章希望大家以后多多支持开心学习网!
- vue基础语法对象(浅析从面向对象思维理解Vue组件)
- vue组件开发步骤(解析如何自动化生成vue组件文档)
- vue统计代码行数(vue实现计数器简单制作)
- vue是怎样进行页面跳转(Vue实现登陆跳转)
- vue插槽实例(Vue中插槽slot的使用方法与应用场景详析)
- vue原样表格导出excel(Vue导出Excel功能的全过程记录)
- vue过滤器filters怎么用(如何使用vue过滤器filter)
- vue实现商品详情讲解(京东 Vue3 组件库支持小程序开发的详细流程)
- vue的watch用法(Vue3中watch的用法与最佳实践指南)
- vuefor指令使用教程(Vue必学知识点之forEach的使用)
- vue-router底层实现原理(vue-router history模式服务器端配置过程记录)
- vue插槽的分类(vue具名插槽的基本使用实例)
- vueelementui三级菜单(vue+element ui实现锚点定位)
- vuekeep-alive源码(vue中keepAlive组件的作用和使用方法详解)
- vue 路由的两种模式(Vue3使用路由VueRouter4的简单示例)
- vue移动端页面不能上下滑动(vue移动端实现左滑编辑与删除的全过程)
- 越南新娘(越南新娘婚介网站)
- 越南新娘(越南新娘骗婚套路流程)
- 菲律宾游学(菲律宾游学中介机构)
- 菲律宾美食(菲律宾美食排行榜前十名)
- 菲律宾特产(菲律宾特产最值得买回国)
- 越南特产(越南特产首饰)
热门推荐
- docker映射端口(Docker初级网络端口映射的配置)
- 终于有人将mysql 索引讲清楚了(MySQL 索引的一些细节分享)
- mysql流式查询(MySQL全面瓦解之查询的正则匹配详解)
- mysql必知必会索引(MySQL索引知识的一些小妙招总结)
- 怎么查看mysql异常链接(MySQL 查看链接及杀掉异常链接的方法)
- SQL SERVER存储过程中使用事务与try catch
- pandas 比较两个表数据是否相同(浅谈pandas筛选出表中满足另一个表所有条件的数据方法)
- 判断手机浏览器是否支持position:fixed
- php教程微信支付技术(php微信扫码支付 php公众号支付)
- 如何去阿里云解析域名(利用Python+阿里云实现DDNS动态域名解析的方法)