配置跨域时,要考虑当前环境,只是代码层次,还是tomcat部署后的,再或者是nginx转发后的
-
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; }
-
tomcat
https://blog.csdn.net/weixin_44100514/article/details/109781103?spm=1001.2014.3001.5501
-
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’: ‘’
}
},
} -
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; } }
跨域问题,主要需要知道,当前的环境。
- 只有java层面上,java里加上跨域就可以
- 部署环境,tomcat上部署,那么需要tomcat配置跨域
- nginx + tomcat 那么不要同时配置跨域,会反复出现跨域了两次,你可以只在java层配置,tomcat不需要了,如果都配置,那么你还是跨域
- vue前端一般不需要解决跨域问题 ,开发环境层次上可以加一下