原创

Spring-直接通过@CrossOrigin注解和withCredentials处理跨域

方式1:
在Controller的类名上添加
@CrossOrigin(allowCredential="true",allowdHeaders=“???”)

前端使用ajax请求时添加 xhrFields:{withCredentials:true}

方式2:
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class WebMvcConfig2 implements WebMvcConfigurer {

@Bean
public LoginInterceptor paasLoginInterceptor() {
return new LoginInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(paasLoginInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/v3/api-docs")
.excludePathPatterns("/manual/*")
.excludePathPatterns("/jh/user/sendRegisterSms/*")
.excludePathPatterns("/jh/user/register");
}

@Override
public void addCorsMappings(CorsRegistry registry) {
//跨域配置
registry.addMapping("/**").allowedOrigins("*")
.allowedHeaders("*")
正文到此结束
本文目录