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

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是前端地址

相关推荐
BillKu16 分钟前
el-radio-group 与 el-dropdown 组合使用的注意事项
前端·javascript·vue.js
黑匣子~31 分钟前
Electron 后台常驻服务实现(托盘 + 开机自启)
前端·javascript·electron
逍遥德1 小时前
CSS display有几种属性值
前端·css·css3
Ares-Wang1 小时前
net Core》》包与库 LibMan、NPM
前端·npm·node.js
charlee441 小时前
实现一个前端动态模块组件(Vite+原生JS)
前端·javascript·html·vite
繁依Fanyi1 小时前
用 UniApp 开发 TilePuzzle:一个由 CodeBuddy 主动驱动的拼图小游戏
前端·uni-app·编辑器·codebuddy首席试玩官
繁依Fanyi1 小时前
用 CodeBuddy 搭建「MiniGoal 小目标打卡器」:一次流畅的 UniApp 开发体验
前端·游戏·uni-app·codebuddy首席试玩官
橙子家3 小时前
简单介绍下 Vue 2.x 中的几种生命周期钩子(Lifecycle Hooks)
前端
优雅的落幕3 小时前
从零开始的抽奖系统创作(2)
java·服务器·前端
BillKu3 小时前
Vue3 scoped样式使用通配符 * 的影响分析
前端·javascript·vue.js