spring boot——请求与参数校验——重要概念——配置CORS实现跨域——华章
CorsConfig
package org.example.cors.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 CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**")//指定可以被跨域的路径 .allowedHeaders("*") //服务器允许的请求头 .allowedMethods("POST", "PUT", "GET", "OPTIONS", "DELETE") //服务器允许的请求方法 .allowCredentials(true) //允许带cookie的跨域请求 Access-Control-Allow-Credentials .allowedOrigins("*") //服务端允许哪些域请求来源 .maxAge(3600); //预检请求的客户端缓存时间 单位秒,默认为1800秒 } }
CorsFilterConfig
package org.example.cors.config; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import java.util.Arrays; @Configuration public class CorsFilterConfig { @Bean public FilterRegistrationBean<CorsFilter> corsFilter() { FilterRegistrationBean<CorsFilter> corsFilterFilterRegistrationBean = new FilterRegistrationBean<>(); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new CorsConfiguration(); //服务器允许的请求头 corsConfiguration.addAllowedHeader("*"); //服务端允许哪些域请求来源 corsConfiguration.addAllowedOrigin("*"); //服务器允许的请求方法 corsConfiguration.setAllowedMethods(Arrays.asList("POST", "PUT", "GET", "OPTIONS", "DELETE")); //允许带cookie的跨域请求 Access-Control-Allow-Credentials corsConfiguration.setAllowCredentials(true); //预检请求的客户端缓存时间 单位秒,默认为1800秒 corsConfiguration.setMaxAge(3600L); //指定可以被跨域的路径 source.registerCorsConfiguration("/**", corsConfiguration); //注册CorsFilter corsFilterFilterRegistrationBean.setFilter(new CorsFilter(source)); //设置加载顺序为-1,该值越小优先级越高 corsFilterFilterRegistrationBean.setOrder(-1); return corsFilterFilterRegistrationBean; } }
@CrossOrigin
package org.example.cors.web; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class IndexController { @CrossOrigin @RequestMapping("/index") public String index(){ return "hello man"; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
2022-01-23 JAVA面向对象学习——java面向对象概念———多态——多态:指子类对象可以直接赋给父类变量,但运行时依然表现出子类的行为特征,这意味着同一个类型的对象在执行同一个方法时,可能表现出多种行为特征。
2022-01-23 JAVA面向对象学习——java面向对象概念———重载
2022-01-23 JAVA面向对象学习——java面向对象概念———重写——Super 关键字的使用
2022-01-23 JAVA面向对象学习——java面向对象概念———重写