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("*")
.allowedMethods("*");
正文到此结束
- 本文标签: Spring Spring Boot
- 本文链接: https://code.jiangjiesheng.cn/article/25
- 版权声明: 本文由小江同学原创发布,转载请先联系本站长,谢谢。