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

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

相关推荐
明月_清风22 分钟前
打字机效果优化:用 requestAnimationFrame 缓冲高频文字更新
前端·javascript
明月_清风23 分钟前
Markdown 预解析:别等全文完了再渲染,如何流式增量渲染代码块和公式?
前端·javascript
掘金安东尼1 小时前
用 CSS 打造完美的饼图
前端·css
掘金安东尼8 小时前
纯 CSS 实现弹性文字效果
前端·css
牛奶9 小时前
Vue 基础理论 & API 使用
前端·vue.js·面试
牛奶9 小时前
Vue 底层原理 & 新特性
前端·vue.js·面试
anOnion9 小时前
构建无障碍组件之Radio group pattern
前端·html·交互设计
pe7er9 小时前
状态提升:前端开发中的状态管理的设计思想
前端·vue.js·react.js
SoaringHeart10 小时前
Flutter调试组件:打印任意组件尺寸位置信息 NRenderBox
前端·flutter
晚风予星11 小时前
Ant Design Token Lens 迎来了全面升级!支持在 .tsx 或 .ts 文件中直接使用 Design Token
前端·react.js·visual studio code