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

网站后缀有什么区别网站的开发环境设计

网站后缀有什么区别,网站的开发环境设计,钓鱼网站怎样做,上海专业做网站建设公司跨域问题是前端与后端分离开发中的常见挑战,以下是14种完整的解决方案: 1 前端解决方案( 开发环境代理) 1.1 Webpack开发服务器代理 // vue.config.js 或 webpack.config.js module.exports {devServer: {proxy: {/api: {target: http://localhost:8…

跨域问题是前端与后端分离开发中的常见挑战,以下是14种完整的解决方案:

1 前端解决方案( 开发环境代理)

1.1 Webpack开发服务器代理

// vue.config.js 或 webpack.config.js
module.exports = {devServer: {proxy: {'/api': {target: 'http://localhost:8080',changeOrigin: true,pathRewrite: {'^/api': ''}}}}
}

1.2 Vite代理配置

// vite.config.js
export default defineConfig({server: {proxy: {'/api': {target: 'http://localhost:8080',changeOrigin: true,rewrite: (path) => path.replace(/^\/api/, '')}}}
})

1.3 JSONP (仅限GET请求)

function jsonp(url, callback) {const script = document.createElement('script');script.src = `${url}?callback=${callback}`;document.body.appendChild(script);
}// 后端需要返回类似 callbackName(data) 的响应

1.4 WebSocket

const socket = new WebSocket('ws://your-backend-url');

1.5 修改浏览器安全策略 (仅开发环境)

  • Chrome启动参数:--disable-web-security --user-data-dir=/tmp/chrome

2 后端解决方案

2.1 Spring框架解决方案

2.1.1 使用@CrossOrigin注解

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*") // 允许所有来源
public class MyController {// 控制器方法
}

2.1.2 全局CORS配置

@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("http://localhost:3000", "http://example.com").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS").allowedHeaders("*").allowCredentials(true).maxAge(3600);}
}

3.1.2 过滤器方式

@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(true);config.addAllowedOrigin("http://localhost:3000");config.addAllowedHeader("*");config.addAllowedMethod("*");source.registerCorsConfiguration("/**", config);return new FilterRegistrationBean<>(new CorsFilter(source));
}

2.2 Spring Boot特定配置

2.2.1 application.properties配置

# 允许的源
cors.allowed-origins=http://localhost:3000,http://example.com
# 允许的方法
cors.allowed-methods=GET,POST,PUT,DELETE,OPTIONS
# 允许的头部
cors.allowed-headers=*
# 是否允许凭证
cors.allow-credentials=true
# 预检请求缓存时间
cors.max-age=3600

2.3 Spring Security解决方案

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.cors().and()// 其他安全配置.csrf().disable(); // 通常需要禁用CSRF以简化API开发}@BeanCorsConfigurationSource corsConfigurationSource() {CorsConfiguration configuration = new CorsConfiguration();configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000"));configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","DELETE","OPTIONS"));configuration.setAllowedHeaders(Arrays.asList("*"));configuration.setAllowCredentials(true);UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", configuration);return source;}
}

3 生产环境解决方案

3.1 Nginx反向代理

server {listen 80;server_name yourdomain.com;location /api {proxy_pass http://backend-server:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location / {root /path/to/frontend/dist;try_files $uri $uri/ /index.html;}
}

4 高级方案

4.1 基于Token的跨域认证

// 后端添加响应头
response.setHeader("Access-Control-Expose-Headers", "Authorization");
response.setHeader("Authorization", "Bearer " + token);

4.2 预检请求(OPTIONS)处理

@RestController
public class OptionsController {@RequestMapping(value = "/**", method = RequestMethod.OPTIONS)public ResponseEntity<?> handleOptions() {return ResponseEntity.ok().build();}
}

4.3 动态允许源

@Bean
public CorsFilter corsFilter() {return new CorsFilter(new UrlBasedCorsConfigurationSource() {@Overridepublic CorsConfiguration getCorsConfiguration(HttpServletRequest request) {CorsConfiguration config = new CorsConfiguration();String origin = request.getHeader("Origin");if (allowedOrigins.contains(origin)) { // 检查是否在允许列表中config.addAllowedOrigin(origin);config.setAllowedMethods(Arrays.asList("GET","POST","PUT","DELETE","OPTIONS"));config.setAllowedHeaders(Arrays.asList("*"));config.setAllowCredentials(true);}return config;}});
}

选择哪种解决方案取决于具体需求、安全要求和部署环境。生产环境中推荐使用Nginx反向代理或API网关方案,开发环境中可以使用代理或后端CORS配置。

http://www.dtcms.com/wzjs/789450.html

相关文章:

  • 无网站营销网站建设规划书模板
  • 服务器做视频网站网站建设公司对比
  • 网站建站卖首饰侵权商城网站怎么做
  • 哪些行业做网站的多网站建设外包公司招聘
  • 国外做电子元器件在哪个网站页面设计时最好用多少种颜色
  • 网站建设朋友圈最新网页传奇
  • api接口开发网站开发昆明网站营销
  • wordpress图站自己做网站想更换网址
  • 免费行情软件网站下载安装佛山合展商务网站建设
  • 河南省住房建设厅网站首页王者荣耀wordpress
  • 努力把网站建设成为wordpress分类目录下文章过多_添加文章目录导航
  • 温州哪里可以做企业网站个性化的个人网站
  • 那些做兼职的小网站如何做网站效果更好
  • 做网站详细步骤青岛注册公司的步骤和流程
  • 网站模板怎么修改教程免费入驻的电商平台
  • 美丽乡村 村级网站建设大连建设工程信息网怎么申请
  • 有域名 有主机 怎么建设网站产品毕业设计网站建设
  • wordpress站点全屏产品设计ppt案例
  • 做食品网站seo具体是什么
  • 企业网站有哪些例子seo策略分析
  • 农村创业的好项目seo托管
  • 做的网站不能放视频软件对于ICP而言 主要承担网站信息
  • asp源码 自助建站康定网站建设工作室
  • 高端网站优化重庆市设计院
  • 炫客网站建设开封做网站优化
  • 网站建设入门房屋室内设计学校
  • 网站设计经典案例怎样提高网站知名度
  • html5高端网站建设织梦模板下载免费全面的seo教程
  • 宣传网站模板网站推广120
  • 做公司网站需要多少钱山西省住房城乡建设厅网站