Spring boot中解决跨域问题
2017-06-21 17:37
736 查看
1、使用过滤器形式。配合Spring 自带Filter(CorsFilter )
优雅起见在项目根目录新建 BeanConfiguration用于定义bean信息,如下:
2、使用注解方式:@CrossOrigin 该注解可加到方法及类上 其中参数 origins 为可调用域名 如:
当然注解中还可以设置其他参数 可以自行百度或查看源码
3、使用jsonp 如下:
$.ajax({
type: "post",
dataType: "jsonp",
jsonp: "callback",
url: "http://192.168.21.165:8080/prairie/manage/weather/getWeatherInfo",
data: {weatherType: 'frostless', areaCode: '15070000'},
success: function (result) {
console.log(result);
},
error: function (err) {
console.log("失败");
}
});
其中后台需要将 callback 做下处理 即将返回信息用callback包裹起来:比如返回字符串为
优雅起见在项目根目录新建 BeanConfiguration用于定义bean信息,如下:
@Configuration public class BeanConfiguration { private static final Logger LOG = Logger.getLogger(BeanConfiguration.class); /** * 解决跨域问题 * @return */ @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); urlBasedCorsConfigurationSource.registerCorsConfiguration("/rpc/**", corsConfiguration); return new CorsFilter(urlBasedCorsConfigurationSource); } /** * 开启方法参数校验 * @return */ @Bean public MethodValidationPostProcessor parameterVerify(){ return new MethodValidationPostProcessor(); } }
2、使用注解方式:@CrossOrigin 该注解可加到方法及类上 其中参数 origins 为可调用域名 如:
@RequestMapping(value = "getLandInfo") @CrossOrigin(origins = "www.domain.com") public RpcResponseEntity<VerifyAuthorityInfo> getLandInfo(String areaCode){
当然注解中还可以设置其他参数 可以自行百度或查看源码
3、使用jsonp 如下:
$.ajax({
type: "post",
dataType: "jsonp",
jsonp: "callback",
url: "http://192.168.21.165:8080/prairie/manage/weather/getWeatherInfo",
data: {weatherType: 'frostless', areaCode: '15070000'},
success: function (result) {
console.log(result);
},
error: function (err) {
console.log("失败");
}
});
其中后台需要将 callback 做下处理 即将返回信息用callback包裹起来:比如返回字符串为
{"invoke_result_code":"INVOKE_SUCCESS","invoke_result_message":"调用成功"} 则需处理为
callback(
{"invoke_result_code":"INVOKE_SUCCESS","invoke_result_message":"调用成功"})
注意callback 需要当做参数 在后台接取 即:
@RequestMapping(value = "getWeatherInfo",produces="application/javascript") public String getWeatherInfo(@RequestParam("weatherType") String weatherType, @RequestParam("areaCode") String areaCode, String callback){注意 chrome 调用需要将contentType设置为 application/javascript 否则会保错
相关文章推荐
- springboot -- 通过cors协议解决跨域问题
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- Spring boot + shiro 解决跨域登陆问题
- springboot中通过cors协议解决跨域问题
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- ajax跨域问题解决(spring boot)
- Spring Boot学习总结(6)——SpringBoot解决ajax跨域请求问题的配置
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- SpringBoot(六):SpringBoot使用CROS解决跨域问题
- 后端(springboot)解决跨域问题
- [置顶] spring boot项目实战:跨域问题解决
- Spring boot 完美解决ajax跨域请求问题
- [置顶] spring boot项目实战:跨域问题解决