SpringBoot跨域
第一种方法
在Controller类或方法上加上@CrossOrigin元注解
package com.wzq.test.action;
import com.wzq.utils.BatchDownFilesUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
/**
* @description: 测试Controller
* @author: Wzq
* @create: 2019-11-25 10:19
*/
@CrossOrigin(origins = "*")
@RestController
@RequestMapping(value = "test")
public class TestController {
@RequestMapping(value = "/test.do")
public String test(){
return "test";
}
}
这种方式对于整个项目都要跨域,那么每个类上都要加@CrossOrigin元注解,比较麻烦,下面讲解下全局跨域配置。
第二种 拦截器的方式
package com.wzq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@SpringBootApplication
public class MyprojectApplication {
public static void main(String[] args) {
SpringApplication.run(MyprojectApplication.class, args);
}
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config); // CORS 配置对所有接口都有效
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
}
第三种 集成WebMvcConfigurerAdapter类重写 addCorsMappings方法
package com.meeno.wzq;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @Auther: Wzq
* @Date: 2019/4/11 18:30
* @Description: 天青色等烟雨,而我在等你.. -- CORSConfiguration
*/
@Configuration
public class CORSConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("*")
.allowedOrigins("*")
.allowedHeaders("*");
super.addCorsMappings(registry);
}
}
第三种有个问题,session无法共享!
第四种 使用nginx代理 把前端项目和服务端api接口地址,保持在同一个ip和端口中
完整的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 20684;
server_name inner.meeno.net;
#charset koi8-r;
#access_log logs/host.access.log main;
location /trainsys {
#root html;
#index index.html index.htm;
client_max_body_size 100000m;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://inner.meeno.net:20681/trainsys/;
}
location /dist {
root E:\svnResourse\program\webTwo\sysBankAdmin-Vue;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
主要看这段:
listen 8080; #端口
server_name 127.0.0.1;#ip或域名
#服务端api基地值
location /trainsys {
#root html;
#index index.html index.htm;
client_max_body_size 100000m;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://inner.meeno.net:20681/trainsys/;
}
#web端项目映射地址
location /dist {
root E:\svnResourse\program\webTwo\sysBankAdmin-Vue;
index index.html index.htm;
}
SpringBoot跨域的更多相关文章
- 前后端分离框架前端react,后端springboot跨域问题分析
前后端分离框架前端react,后端springboot跨域问题分析 为啥跨域了 前端react的设置 springboot后端设置 为啥跨域了 由于前后端不在一个端口上,也是属于跨域问题的一种,所以必 ...
- springmvc springboot 跨域问题(CORS)
官方文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html springmvc s ...
- 两种解决springboot 跨域问题的方法示例
两种解决springboot 跨域问题的方法示例,哪种方法看情况而定,自己选择.社会Boolean哥,人狠话不多,直接上代码. 第一种实现方式: 此种方式做全局配置,用起来更方便,但是无法 ...
- SpringBoot跨域问题
1.先来说说跨域原理: 跨域原理简单来说就是发起跨域请求的时候,浏览器会对请求域返回的响应信息检查HTTP头,如果Access-Control-Allow-Origin包含了自身域,则允许访问,否则报 ...
- SpringBoot跨域小结
前言:公司的SpringBoot项目出于某种原因,经常样处理一些跨域请求. 一.以前通过查阅相关资料自己写的一个处理跨域的类,如下. 1.1首先定义一个filter(拦截所有请求,包括跨域请求) pu ...
- springboot跨域请求
首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源 SpringBoot | 番外:使用小技巧合集 2018/09/17 | 分类: 基础技术 | 0 条评论 | 标 ...
- 关于解决Springboot跨域请求的方法
前言 最近在项目中,由于前后分离,前台项目和后台项目部署的不在一台服务器,就产生了跨域的问题,特此记录下 正文 正常情况下,如果提示: 就可以判断是没有解决跨域的问题了. 在SSM中,我曾经这样解决过 ...
- springboot跨域请求设置
当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时,一个资源会发起一个跨域HTTP请求(Cross-site HTTP request).比如说,域名A ( http://domaina ...
- springboot 跨域
参考: https://blog.csdn.net/qq779446849/article/details/53102925 https://blog.csdn.net/wo541075754/art ...
- Springboot跨域 ajax jsonp请求
SpringBoot配置: <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
随机推荐
- EXCEL中的多个条件同时成立写法
=IF(AND($B2>0,$C2>0,$D2>0,$E2>0),(($B2*1000/$C2/60/$D2)*$E2),0)点击F2,粘贴上边的公式选择F2到f200ctrl ...
- PYTHON 利用ImagePipeline专门爬取图片
自定义file_path()函数,即可以原有图像文件名为名来保存,并分类保存 def file_path(self, request, response=None, info=None): image ...
- React 模块与组件
React 模块与组件 几个重要概念理解 1). 模块与组件 1. 模块: 理解: 向外提供特定功能的js程序, 一般就是一个js文件 为什么: js代码更多更复杂 作用: 复用js, 简化js的编写 ...
- robotframework - selenium Api介绍
一.介绍下selenium常用的api *** Settings ***Library SeleniumLibraryResource baidu业务.txtResource UI分层.txt *** ...
- informix常见问题
1.中文乱码 https://www.cnblogs.com/equation/p/5545967.html 2.informix创建数据库和用户 https://wenku.baidu.com/vi ...
- Skywalking-03:Skywalking本地调试
live-demo 与 skywalking 源码联调 构建项目 找一个目录执行如下命令 git clone https://github.com/apache/skywalking.git # cl ...
- odoo14里面的用户登录log记录
一.继承userlog,添加字段 # -*- coding: utf-8 -*- from odoo import models, fields, api from odoo.http import ...
- xhell、xftp、putty使用教程
作为远程登陆工具,上传代码登陆服务器工具 1.XSHELL Xshell是远程连接Linux服务器的工具,基于SSH协议,使用它可以更加方便的操作Linux操作系统,在刚使用时可能需要提前简单的设置下 ...
- Scrapy入门到放弃04:下载器中间件,让爬虫更完美
前言 MiddleWare,顾名思义,中间件.主要处理请求(例如添加代理IP.添加请求头等)和处理响应 本篇文章主要讲述下载器中间件的概念,以及如何使用中间件和自定义中间件. MiddleWare分类 ...
- 八数码难题之 A* 算法
人生第一个A*算法-好激动-- 八数码难题--又称八数码水题,首先要理解一些东西: 1.状态可以转化成整数,比如状态: 1 2 3 4 5 6 7 8 0 可以转化成:123456780这个整数 2. ...