目錄
1.通過(guò)SpringSecurity方式配置
2.使用Spring提供的CorsFilter注入Bean(推薦)
3.使用注解@CrossOrigin注解(繁瑣)
4.通過(guò)ResponseBodyAdvice 實(shí)現(xiàn)跨域
5.通過(guò)HttpServletResponse設(shè)置跨域
6.通過(guò)WebMvcConfigurer 實(shí)現(xiàn)跨域文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-621907.html
1.通過(guò)SpringSecurity方式配置
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 允許跨域
http.cors().configurationSource(corsConfigurationSource());
// 省略其他代碼...
}
// 跨域配置
private CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedMethods(Arrays.asList("GET", "POST"));// 支持請(qǐng)求方式
config.addAllowedOriginPattern("*");// 支持跨域
config.setAllowCredentials(true);// cookie
config.addAllowedHeader("*");// 允許請(qǐng)求頭信息
config.addExposedHeader("*");// 暴露的頭部信息
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);// 添加地址映射
return source;
}
}
2.使用Spring提供的CorsFilter注入Bean(推薦)
//....省略其他代碼
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));// 支持請(qǐng)求方式
config.addAllowedOriginPattern("*");// 支持跨域
config.setAllowCredentials(true);// cookie
config.addAllowedHeader("*");// 允許請(qǐng)求頭信息
config.addExposedHeader("*");// 暴露的頭部信息
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);// 添加地址映射
return new CorsFilter(source);
}
//....省略其他代碼
3.使用注解@CrossOrigin注解(繁瑣)
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(origins = "*")
public class DemoController {
@RequestMapping("/test")
public Object test() {
return "hello world";
}
}
4.通過(guò)ResponseBodyAdvice 實(shí)現(xiàn)跨域
與第5類似文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-621907.html
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {
/**
* 內(nèi)容是否需要重寫(通過(guò)此方法可以選擇性部分控制器和方法進(jìn)行重寫)
* 返回 true 表示重寫
*/
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
/**
* 方法返回之前調(diào)用此方法
*/
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
// 設(shè)置跨域
response.getHeaders().set("Access-Control-Allow-Origin", "*");
return body;
}
}
5.通過(guò)HttpServletResponse設(shè)置跨域
HttpServletResponse#setHeader("Access-Control-Allow-Origin", "*");
6.通過(guò)WebMvcConfigurer 實(shí)現(xiàn)跨域
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 CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 所有接口
.allowCredentials(true) // 是否發(fā)送 Cookie
.allowedOriginPatterns("*") // 支持域
.allowedMethods(new String[]{"GET", "POST"}) // 支持方法
.allowedHeaders("*")// 允許請(qǐng)求頭
.exposedHeaders("*");// 暴露出去的響應(yīng)頭
}
}
到了這里,關(guān)于SpringBoot 實(shí)現(xiàn)跨域的六種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!