当前位置: 首页 > news >正文

模板生成网站青海省城乡建设信息官官方网站

模板生成网站,青海省城乡建设信息官官方网站,深圳坪山高铁站,沧州市网站在Spring Boot项目中解决CORS#xff08;跨域资源共享#xff09;问题#xff0c;可以通过以下几种方法#xff1a; 1. 使用CrossOrigin注解 这是最简单的方法#xff0c;适用于单个控制器或控制器方法级别的跨域配置。你可以在控制器类或具体的方法上使用CrossOrigin注…在Spring Boot项目中解决CORS跨域资源共享问题可以通过以下几种方法 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 public class MyController {CrossOrigin(origins http://localhost:3000) // 允许指定域名的跨域请求GetMapping(/example)public String example() {return Example Response;} } 你也可以使用通配符*来允许所有域名的跨域请求但出于安全考虑生产环境中应该明确允许的域名。 2. 全局跨域配置 通过实现WebMvcConfigurer接口并重写addCorsMappings方法可以设置全局的CORS配置。这种方法会影响所有未明确拒绝的跨域请求。 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 {Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping(/**) // 允许跨域的路径.allowedOrigins(http://localhost:3000) // 允许的域名.allowedMethods(GET, POST, PUT, DELETE) // 允许的方法.allowedHeaders(*) // 允许的头信息.allowCredentials(true) // 是否允许证书(cookies).maxAge(3600); // 预检请求的缓存时间} } 同样你也可以使用通配符*来允许所有域名但请注意安全风险。 3. 使用CorsFilter 创建一个CorsFilter过滤器并在Spring Boot中注册它。这种方法可以在Spring Security中使用或者直接在Spring Boot中使用。 import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;Component public class CorsFilter implements Filter {Overridepublic void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletResponse response (HttpServletResponse) res;HttpServletRequest request (HttpServletRequest) req;response.setHeader(Access-Control-Allow-Origin, http://localhost:3000);response.setHeader(Access-Control-Allow-Methods, POST, GET, OPTIONS, DELETE);response.setHeader(Access-Control-Max-Age, 3600);response.setHeader(Access-Control-Allow-Headers, x-requested-with, authorization);if (OPTIONS.equalsIgnoreCase(request.getMethod())) {response.setStatus(HttpServletResponse.SC_OK);} else {chain.doFilter(req, res);}} } 4. 使用Spring Security配置跨域 如果你的项目中使用了Spring Security可以在配置类中添加CORS配置。 import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;Configuration EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {http.cors().and() // 启用CORS配置// 其他安全配置....csrf().disable(); // 禁用CSRF如果需要} } 然后你还需要确保在Spring Security的配置中添加了CORS过滤器或者通过上面的.cors()方法启用了CORS支持。 5. 使用application.properties或application.yml配置 你也可以在Spring Boot的配置文件中添加全局的CORS配置。 在application.properties中 # 允许跨域的地址 cors.allowed-originshttp://localhost:3000 # 允许的方法 cors.allowed-methodsGET,POST,PUT,DELETE,OPTIONS # 允许的头部 cors.allowed-headersorigin,content-type,accept,x-requested-with # 允许发送Cookie cors.allow-credentialstrue # 预检请求的有效期,单位为秒 cors.max-age3600 然而需要注意的是直接在application.properties或application.yml中配置CORS可能不是Spring Boot推荐的做法因为这种方式不如使用Java配置类那样灵活和可维护。通常建议使用前面提到的方法之一来配置CORS。 总结 在Spring Boot项目中解决CORS问题有多种方法你可以根据项目的具体需求和安全考虑来选择合适的方法。通常使用CrossOrigin注解或全局跨域配置是最常见和推荐的做法。如果你使用了Spring Security还需要确保在Spring Security的配置中正确启用了CORS支持。
http://www.hkea.cn/news/14256793/

相关文章:

  • 建设银行的官方网站网站建设总流程
  • 360网站建设官网网站首页做的好看
  • 哪个网站教做西餐用jsp做学校网站
  • 北京网站建设项目企业网站做app
  • 软装设计师常用网站百度风云榜官网
  • 打码网站如何建设如何做情趣网站
  • 做logo网站php+网站开发案例教程
  • 网站制作完工验收单wordpress 国内主题 营销主题
  • 学校网站建设成功案例wordpress 新浪博客
  • 北京cms建站系统兰州市网站
  • 岳阳做公司网站外链seo推广
  • 网站建设类行业资讯灵感网站
  • wordpress 页面 权限优化seo深圳
  • 武冈网站建设如何给自己网站做网站优化
  • 做网站定金要多少做阿里云网站的公司
  • mugeda做网站百度网址大全旧版安装
  • 网站开发研究综述余姚建设网站公司
  • 可以做试题的网站服务商的英文简称
  • 做外贸卖小商品是哪个网站网站建设推广的软文
  • 菏泽住房和城乡建设局网站个人网页首页图片
  • 买域名的网站望牛墩网站建设
  • 网站做违法的事情投诉网络舆情应对及处置方案
  • 建工社微课程官网合肥网站快速优化排名
  • 北京网站设计公司哪儿济南兴田德润简介长沙免费建站网络营销
  • 扬州外贸网站建设张家界网站建设的公司
  • 设计外贸商城网站建设高校网站开发
  • 装修网站设计平台多功能垫块机
  • 深圳住房与建设部网站鲜花加盟网站建设
  • 电影网站加盟可以做么商贸有限公司怎么样注册
  • 海口网站制作案例手机网站设计公司皆选亿企邦