第一种方法

在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跨域的更多相关文章

  1. 前后端分离框架前端react,后端springboot跨域问题分析

    前后端分离框架前端react,后端springboot跨域问题分析 为啥跨域了 前端react的设置 springboot后端设置 为啥跨域了 由于前后端不在一个端口上,也是属于跨域问题的一种,所以必 ...

  2. springmvc springboot 跨域问题(CORS)

    官方文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html springmvc s ...

  3. 两种解决springboot 跨域问题的方法示例

    两种解决springboot 跨域问题的方法示例,哪种方法看情况而定,自己选择.社会Boolean哥,人狠话不多,直接上代码. 第一种实现方式:       此种方式做全局配置,用起来更方便,但是无法 ...

  4. SpringBoot跨域问题

    1.先来说说跨域原理: 跨域原理简单来说就是发起跨域请求的时候,浏览器会对请求域返回的响应信息检查HTTP头,如果Access-Control-Allow-Origin包含了自身域,则允许访问,否则报 ...

  5. SpringBoot跨域小结

    前言:公司的SpringBoot项目出于某种原因,经常样处理一些跨域请求. 一.以前通过查阅相关资料自己写的一个处理跨域的类,如下. 1.1首先定义一个filter(拦截所有请求,包括跨域请求) pu ...

  6. springboot跨域请求

      首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源 SpringBoot | 番外:使用小技巧合集 2018/09/17 | 分类: 基础技术 | 0 条评论 | 标 ...

  7. 关于解决Springboot跨域请求的方法

    前言 最近在项目中,由于前后分离,前台项目和后台项目部署的不在一台服务器,就产生了跨域的问题,特此记录下 正文 正常情况下,如果提示: 就可以判断是没有解决跨域的问题了. 在SSM中,我曾经这样解决过 ...

  8. springboot跨域请求设置

    当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时,一个资源会发起一个跨域HTTP请求(Cross-site HTTP request).比如说,域名A ( http://domaina ...

  9. springboot 跨域

    参考: https://blog.csdn.net/qq779446849/article/details/53102925 https://blog.csdn.net/wo541075754/art ...

  10. Springboot跨域 ajax jsonp请求

    SpringBoot配置: <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

随机推荐

  1. ICMP、ARP协议介绍和ping命令

    交换机工作原理和常用的简单命令    一.ICMP协议      1)ICMP协议的封装    二.ARP协议      1)什么是ARP协议      2)ARP相关命令    三.Ping命令的使 ...

  2. Java基础00-Debug11

    1. Debug 1.1 Debug概述 1.2 Debug操作流程 1.2.1 如何加断点 1.2.2 如何运行加了断点的程序 1.2.3 看哪里 1.2.4 点哪里 1.2.5 如何删除断点 多个 ...

  3. 详解Lombok中的@Builder用法

    Builder 使用创建者模式又叫建造者模式.简单来说,就是一步步创建一个对象,它对用户屏蔽了里面构建的细节,但却可以精细地控制对象的构造过程. 基础使用 @Builder注释为你的类生成相对略微复杂 ...

  4. RPC远程协议之原理分析

    在近几年工作中发现,功能服务化或微服务化越来越流行,逐渐成为实现中大型分布式系统架构的主要方式,而在分布式系统中的不同节点应用间的通信中,RPC远程协议扮演关键作用.实际上,在日常工作中,我们也多多少 ...

  5. [001] - JavaSE面试题(一):面向对象

    第一期:Java面试 - 100题,梳理各大网站优秀面试题.大家可以跟着我一起来刷刷Java理论知识 [001] - JavaSE面试题(一):面向对象 第1问:面向对象和面向过程的区别? 面向过程 ...

  6. [刘阳Java]_Spring相关配置介绍_第5讲

    这一节我们介绍一下Spring框架的相关常用配置 Spring依赖注入的两种方式(构造方法注入和setter方式注入) p-namespace方式配置 properties属性文件配置方式 集合对象配 ...

  7. IDEA上搭建spark开发

    IDEA上搭建spark开发环境 我本地系统是windows10,首先IDEA上要安装了scala插件. 1.下载winutils.exe文件 winutils.exe是在Windows系统上需要的h ...

  8. 流畅的python学习1

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Apr 20 21:08:08 202 ...

  9. C++第三十八篇 -- 研究一下Windows驱动开发(二)--WDM式驱动的加载

    基于Windows驱动开发技术详解这本书 一.简单的INF文件剖析 INF文件是一个文本文件,由若干个节(Section)组成.每个节的名称用一个方括号指示,紧接着方括号后面的就是节内容.每一行就是一 ...

  10. pip批量安装库

    将需要安装的库名和版本号都写在一个txt文档中,每个库名占一行,例如requests==2.24.0. 然后在用pip install -r命令去找到这个txt文档批量安装里面填写的库,如果嫌速度太慢 ...