Springboot后端应用
跨域实现:全局跨域类
在应用根目录下,建立config包,在下面建立跨域配置类CrosConfig
该类实现WebMvcConfigurer接口
package com.jb.sbdemo.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CrosConfig implements { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOriginPatterns("*") //allowedOrigins("*") .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") .allowCredentials(true) .maxAge(3600) .allowedHeaders("*"); } } 注:
直接复制网上可能出错:allowedOrigins("*"),修改为:allowedOriginPatterns("*")
保存、重启服务端,OK!
测试:
(1)建立静态页面,在页面中放置<a href=‘服务端网址与接口’ >跨域访问</> 访问这个接口。
(2)启动客户端应用与页面,访问该服务端接口、