配置跨域时,要考虑当前环境,只是代码层次,还是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) {
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
}
@Override
public void addFormatters(FormatterRegistry registry) {
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("POST","GET","PUT", "DELETE")
.allowedOrigins("*");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
}
@Override
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
}
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
}
@Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
}
@Override
public Validator getValidator() {
return null;
}
@Override
public MessageCodesResolver getMessageCodesResolver() {
return null;
}
- tomcat
https:
- vue
proxy: {
'/api': {
target: 'http://IP:端口/',
changOrigin: true,
pathRewrite: {
'^/api': ''
}
},
}
- nginx
location/ {
proxy_passhttp:
#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前端一般不需要解决跨域问题 ,开发环境层次上可以加一下