前端跨域问题,后端解决方案

java 复制代码
import org.springframework.context.annotation.Bean;
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;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.*;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /*
         * Set these headers
         * response.setHeader("Strict-Transport-Security", "max-age=31536000");
           response.setHeader("X-XSS-Protection", "1; mode=block");
           response.setHeader("X-Content-Type-Options", "nosniff");
           response.setHeader("X-Frame-Options", "SAMEORIGIN");
         */
        http.csrf().disable();
        http.headers()
                .defaultsDisabled()
                .cacheControl()
                .and()
                .httpStrictTransportSecurity().maxAgeInSeconds(31536000).includeSubDomains(false)
                .and()
                .xssProtection().block(true)
                .and()
                .contentTypeOptions()
                .and()
                .frameOptions().sameOrigin()
                .and()
                .csrf().disable();
        http.cors(cors -> cors.configurationSource(corsFilter()));
    }

    @Bean
    public CorsConfigurationSource corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowedOrigins(Collections.singletonList("http://localhost:3000")); // Allow specific origin
        corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); // Allow HTTP methods
        corsConfiguration.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type", "X-Requested-With")); // Allow headers
        corsConfiguration.setAllowCredentials(true); // Allow credentials (cookies, etc.)
        source.registerCorsConfiguration("/**", corsConfiguration);
        return source;
    }

}

前后端分离项目,http://localhost:3000是前端地址

相关推荐
鹏北海24 分钟前
micro-app 微前端项目部署指南
前端·nginx·微服务
发现一只大呆瓜27 分钟前
虚拟列表:从定高到动态高度的 Vue 3 & React 满分实现
前端·vue.js·react.js
css趣多多31 分钟前
add组件增删改的表单处理
java·服务器·前端
证榜样呀36 分钟前
2026 大专计算机专业必考证书推荐什么
大数据·前端
蓝帆傲亦43 分钟前
前端性能极速优化完全指南:从加载秒开体验到丝滑交互
前端·交互
鱼毓屿御1 小时前
如何给用户添加权限
前端·javascript·vue.js
JustHappy1 小时前
「web extensions🛠️」有关浏览器扩展,开发前你需要知道一些......
前端·javascript·开源
何中应1 小时前
nvm安装使用
前端·node.js·开发工具
雯0609~1 小时前
hiprint:实现项目部署与打印3-vue版本-独立出模板设计与模板打印页面
前端·vue.js·arcgis
杜子不疼.1 小时前
【Linux】教你在 Linux 上搭建 Web 服务器,步骤清晰无门槛
linux·服务器·前端