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

禁止国内ip访问 网站石家庄百度快照优化

禁止国内ip访问 网站,石家庄百度快照优化,怎么建立一个网站能够与讯飞云对话,jsp网站开发实例实验报告在Spring Boot项目中解决CORS(跨域资源共享)问题,可以通过以下几种方法: 1. 使用CrossOrigin注解 这是最简单的方法,适用于单个控制器或控制器方法级别的跨域配置。你可以在控制器类或具体的方法上使用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.propertiesapplication.yml配置

你也可以在Spring Boot的配置文件中添加全局的CORS配置。

application.properties中:

# 允许跨域的地址
cors.allowed-origins=http://localhost:3000
# 允许的方法
cors.allowed-methods=GET,POST,PUT,DELETE,OPTIONS
# 允许的头部
cors.allowed-headers=origin,content-type,accept,x-requested-with
# 允许发送Cookie
cors.allow-credentials=true
# 预检请求的有效期,单位为秒
cors.max-age=3600


然而,需要注意的是,直接在application.propertiesapplication.yml中配置CORS可能不是Spring Boot推荐的做法,因为这种方式不如使用Java配置类那样灵活和可维护。通常,建议使用前面提到的方法之一来配置CORS。

总结

在Spring Boot项目中解决CORS问题有多种方法,你可以根据项目的具体需求和安全考虑来选择合适的方法。通常,使用@CrossOrigin注解或全局跨域配置是最常见和推荐的做法。如果你使用了Spring Security,还需要确保在Spring Security的配置中正确启用了CORS支持。

http://www.mmbaike.com/news/49006.html

相关文章:

  • 自己做热图的网站在线bt种子
  • 网站首页布局的设计个人网站设计内容
  • 电商网站开发商简述网站建设的基本流程
  • 天猫网站做链接怎么做国外网站推广
  • 小说网站怎么做流量吗sem是什么显微镜
  • 高端网站建设推来客网络网站流量数据分析
  • 视频网站的嵌入代码是什么今日热点事件
  • 中英文切换网站网络宣传平台有哪些
  • TP5.1做的网站首页被挂马原因淄博seo网站推广
  • 怎么做免费的网站空间谷歌的推广是怎么样的推广
  • 做外贸哪些b2b网站比较有效青岛seo青岛黑八网络最强
  • 苏州建设网站微信公众号百度广告平台电话
  • 上海城乡建设与管理委员会网站批量查询神马关键词排名
  • 网络营销相关的资源网站下载手机百度最新版
  • 做的网站手机打不开怎么回事手机网站建设案例
  • 网站建设课程论文江西优化中心
  • 视频教学网站cms手机网站智能建站
  • 公司网站一般用什么软件做如何自己做网络推广
  • 公司网站设计网络公司抖音账号权重查询
  • 网站url建设如何自己做引流推广
  • 茶叶网站建设公司seo推广思路
  • 做家电维修网站能接到单吗关键词优化系统
  • 2023最新装修效果图开封搜索引擎优化
  • 郑州 网站建设百度推广优化是什么?
  • 17网站一起做网店普宁池尾免费自建网站有哪些
  • 个人网站建设与实现毕业设计seo推广的方法
  • 珠海网站制作价格品牌推广服务
  • 自己做网站导航页搜索引擎优化与推广技术
  • 厦门app网站建设搜狗友链交换
  • 无线网站应建设在什么地方自己接单的平台