Springboot怎么实现跨域访问无需使用jsonp的代码
小编给大家分享一下Springboot怎么实现跨域访问无需使用jsonp的代码,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
Springboot 实现跨域访问 无需使用jsonp
在springboot的拦截器中添加respone的头信息即可
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //String origin = (String) request.getRemoteHost()+":"+request.getRemotePort(); response.addHeader("Access-Control-Allow-Origin", "*"); //System.out.println("Access-Control-Allow-Origin"); response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); //System.out.println("Access-Control-Allow-Methods"); response.addHeader("Access-Control-Max-Age", "3600"); //System.out.println("Access-Control-Max-Age"); response.addHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization"); //System.out.println("Access-Control-Allow-Headers"); response.addHeader("Access-Control-Allow-Credentials","true"); //System.out.println("Access-Control-Allow-Credentials"); String api_key = request.getParameter("api_key"); String api_secret = request.getParameter("api_secret"); ; if (check(api_key,api_secret)){ return true; } response.sendError(400,"api_key or api_secret are error"); return false; } @Configuration public class MyWebMvcConfigurer implements WebMvcConfigurer { @Bean APIIntercepter apiIntercepter() { return new APIIntercepter(); } @Override public void addInterceptors(InterceptorRegistry registry) { // 添加一个拦截器,连接以/v1为前缀的 url路径 registry.addInterceptor(loginIntercepter()).addPathPatterns("/admin/**"); registry.addInterceptor(apiIntercepter()).addPathPatterns("/v1/**"); } }
springboot是什么
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
看完了这篇文章,相信你对“Springboot怎么实现跨域访问无需使用jsonp的代码”有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!