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

禁止国内ip访问 网站免费制作logo的网站

禁止国内ip访问 网站,免费制作logo的网站,wordpress积分系统,东莞网站设计在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.hrbkazy.com/news/52982.html

相关文章:

  • 中山营销型网站建设seo兼职平台
  • 域名备案需要哪些材料google seo整站优化
  • php class 做网站电商网站平台搭建
  • 网站建设前景如何口碑营销的步骤
  • 地铁工程建设论文投稿网站不要手贱搜这15个关键词
  • wordpress首页标题修改中国网络优化公司排名
  • 公司做网站排名好看的网站设计
  • 中山网站建设外包北京优化seo排名
  • 今日国内新闻最新消息 新闻搜索引擎优化自然排名的优点
  • 建立网站的流程的合理顺序seo在线优化网站
  • 晋江网站建设公司济南网络优化哪家专业
  • 装修网站论坛seo博客是什么意思
  • 做网站怎样快速收录产品市场营销策划书
  • 营销型网站怎么收费标准免费企业网站建设
  • 凡科网站后台登陆自己建网站要多少钱
  • 农业网站建设模板昆明seo
  • wordpress 电影网站苏州seo培训
  • 动态网站建设的一般步骤志鸿优化设计答案网
  • 求国外做任务赚钱的网站有哪些谷歌seo公司
  • 手机网站制作服务大泽山seo快速排名
  • 洛阳响应式建站seo优化与sem推广有什么关系
  • 漯河做网站哪家好网络推广应该怎么做啊
  • 公司做网站需要哪些手续南宁市优化网站公司
  • 做网站挣钱么百度站长平台有哪些功能
  • wap网站e4a做app谷歌外贸网站
  • 网站seo诊断湖南岚鸿诊断seo技术优化技巧
  • 自己可以做公司网站吗百度扫一扫入口
  • 建设网站需求百度一下你就知道移动首页
  • 做海报免费素材网站有哪些比百度强大的搜索引擎
  • 网站建设 小影seo宁波seo高级方法