在spring boot项目中配置跨域(cors,cross-origin resource sharing)主要是为了允许来自不同源(不同的协议、域名或端口)的前端应用能够访问后端api。
spring boot提供了多种方式来配置跨域支持。
1. 使用@crossorigin注解
最简单的方式是在控制器或者具体的方法上使用@crossorigin注解。
例如:
import org.springframework.web.bind.annotation.crossorigin;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
@crossorigin(origins = "http://example.com") // 允许来自 http://example.com 的跨域请求
public class mycontroller {
@getmapping("/myendpoint")
public string mymethod() {
return "hello, world!";
}
}这将允许来自http://example.com的跨域请求访问/myendpoint这个接口。
2. 全局跨域配置
如果你想要为整个应用配置跨域支持,可以在配置类中添加一个webmvcconfigurer的实现:
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 webconfig implements webmvcconfigurer {
@override
public void addcorsmappings(corsregistry registry) {
registry.addmapping("/**") // 为所有请求添加跨域支持
.allowedorigins("*") // 允许任何源
.allowedmethods("get", "post", "put", "delete") // 允许的http方法
.allowcredentials(true); // 是否允许发送cookie信息
}
}这个配置将允许任何源的跨域请求,并且允许get、post、put和delete方法。
allowcredentials设置为true表示允许客户端发送cookie信息。
3. 使用corsfilter
如果需要更细致的控制,可以创建一个corsfilter并注册为bean:
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.web.cors.corsconfiguration;
import org.springframework.web.cors.corsconfigurationsource;
import org.springframework.web.cors.urlbasedcorsconfigurationsource;
import org.springframework.web.filter.corsfilter;
@configuration
public class corsconfig {
@bean
public corsfilter corsfilter() {
corsconfiguration configuration = new corsconfiguration();
configuration.setallowedorigins("*");
configuration.setallowedmethods("get", "post", "put", "delete");
configuration.setallowcredentials(true);
configuration.setallowedheaders("*");
urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource();
source.registercorsconfiguration("/**", configuration);
return new corsfilter(source);
}
}这个配置与上面的全局跨域配置类似,但是通过corsfilter提供了更多的灵活性和控制。
注意事项:
allowedorigins可以是一个具体的域名,如"http://example.com",或者使用"*"来允许任何源。allowedmethods定义了允许的http方法。allowcredentials设置为true时,服务器将接受包含敏感信息(如cookies和http认证信息)的跨域请求。allowedheaders定义了允许的http请求头。
根据你的项目需求和安全考虑,合理配置跨域支持是非常重要的。
在生产环境中,通常不建议允许任何源("*"),而是应该明确指定可信的源。
当然,除了上述提到的使用@crossorigin注解、全局跨域配置和corsfilter之外,还有其他一些方法可以在spring boot项目中配置跨域支持。
4. 配置webmvcconfigurerproperties
在spring boot中,可以通过扩展webmvcconfigurerproperties类来配置跨域。
首先,需要创建一个配置类并继承webmvcconfigurerproperties:
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.annotation.configuration;
import org.springframework.web.servlet.config.annotation.webmvcconfigurer;
@configuration
@configurationproperties(prefix = "cors")
public class corsconfig implements webmvcconfigurer {
private string[] allowedorigins;
private string[] allowedmethods;
private string[] allowedheaders;
private boolean allowcredentials;
// getters and setters ...
@override
public void addcorsmappings(corsregistry registry) {
registry.addmapping("/**")
.allowedorigins(allowedorigins)
.allowedmethods(allowedmethods)
.allowedheaders(allowedheaders)
.allowcredentials(allowcredentials);
}
}然后,在application.properties或application.yml中添加相应的配置:
# application.properties cors.allowedorigins[0]=http://example.com cors.allowedmethods[0]=get cors.allowedmethods[1]=post cors.allowedheaders[0]=content-type cors.allowcredentials=true
5. 使用spring security
如果你的项目中使用了spring security,可以通过配置httpsecurity来实现跨域支持。
首先,需要创建一个配置类并重写configure(httpsecurity http)方法:
import org.springframework.security.config.annotation.web.builders.httpsecurity;
import org.springframework.security.config.annotation.web.configuration.websecurityconfigureradapter;
@configuration
public class securityconfig extends websecurityconfigureradapter {
@override
protected void configure(httpsecurity http) throws exception {
http
// ...其他配置...
.cors().and() // 启用默认的跨域配置
// ...其他配置...
}
}如果需要自定义跨域配置,可以使用.cors()方法并传递一个corsconfigurationsource实例:
corsconfiguration configuration = new corsconfiguration();
configuration.setallowedorigins(arrays.aslist("http://example.com"));
configuration.setallowedmethods(arrays.aslist("get","post", "put", "delete"));
configuration.setallowedheaders(arrays.aslist("content-type", "authorization"));
configuration.setallowcredentials(true);
urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource();
source.registercorsconfiguration("/**", configuration);
http.cors(source).and();6. 使用第三方库
还可以使用第三方库如cors-filter来实现跨域支持。
这通常需要在项目的pom.xml中添加依赖,并在web.xml中配置过滤器。
以上是一些在spring boot项目中配置跨域支持的方法。
选择最适合项目需求和架构的方法,并确保考虑到安全性和性能的影响。
在实施跨域策略时,应当避免过度宽松的配置,以免引入安全风险。
这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论