strict-origin-when-cross-origin nginx、tomcat、java、vue 跨域问题

本文详细介绍了在不同环境中处理跨域问题的方法,包括Java中使用Spring MVC配置CORS,Tomcat服务器上的跨域设置,以及在Nginx反向代理下的配置。同时提到了Vue前端如何通过proxy解决开发环境的跨域问题。请注意,在实际部署中,避免在多个层次重复配置以防止不必要的跨域问题。
摘要由CSDN通过智能技术生成
配置跨域时,要考虑当前环境,只是代码层次,还是tomcat部署后的,再或者是nginx转发后的
  1. java
import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CommonInterceptor2 implements WebMvcConfigurer {

	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addFormatters(FormatterRegistry registry) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		// TODO Auto-generated method stub
	    registry.addMapping("/**")
        .allowedHeaders("*")
        .allowedMethods("POST","GET","PUT", "DELETE")
        .allowedOrigins("*");

	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		// TODO Auto-generated method stub

	}

	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
		// TODO Auto-generated method stub

	}

	@Override
	public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
		// TODO Auto-generated method stub

	}

	@Override
	public Validator getValidator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MessageCodesResolver getMessageCodesResolver() {
		// TODO Auto-generated method stub
		return null;
	}
  1. tomcat
https://blog.csdn.net/weixin_44100514/article/details/109781103?spm=1001.2014.3001.5501
  1. vue
proxy: {  //配置跨域
      '/api': {
        target: 'http://IP:端口/',  //这里后台的地址模拟的
        changOrigin: true,  //允许跨域
        pathRewrite: {
          /* 重写路径,当我们在浏览器中看到请求的地址为:http://localhost:8080/api/core/getData/userInfo 时
            实际上访问的地址是:http://121.121.67.254:8185/core/getData/userInfo,因为重写了 /api
           */
          '^/api': '' 
        }
      },
    }
  1. nginx
location/ {
              proxy_passhttp://localhost:8081/;

              #proxy_set_headerHost $host:80;

              #proxy_set_headerX-Real-IP $remote_addr;

              #proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;

 

              add_headerAccess-Control-Allow-Origin $http_origin;

              add_headerAccess-Control-Allow-Methods *;

              add_headerAccess-Control-Allow-Headers $http_access_control_request_headers;

              add_headerAccess-Control-Max-Age 60000;

              add_headerAccess-Control-Allow-Credentials true;

              if($request_method = OPTIONS){
                     return200;

              }

    }

跨域问题,主要需要知道,当前的环境。

  1. 只有java层面上,java里加上跨域就可以
  2. 部署环境,tomcat上部署,那么需要tomcat配置跨域
  3. nginx + tomcat 那么不要同时配置跨域,会反复出现跨域了两次,你可以只在java层配置,tomcat不需要了,如果都配置,那么你还是跨域
  4. vue前端一般不需要解决跨域问题 ,开发环境层次上可以加一下
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值