解决!【启用了Security的SpringBoot项目跨域问题】以及【post请求403错误】

本文介绍了在启用SpringSecurity的SpringBoot项目中如何处理跨域问题,通过添加CORS配置允许所有来源,并禁用CSRF保护以解决POST请求的403错误。具体解决方案包括在SecurityConfig中配置CORS和关闭CSRF功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、启用了Security的SpringBoot项目跨域问题

可以通过以下两种方式来判断Spring Boot项目是否使用了Spring Security:

  1. 查看项目的依赖关系:在项目的pom.xml文件中查找是否包含spring-security相关的依赖项。例如,可以搜索包含"spring-security"或"spring-boot-starter-security"的依赖项。
  2. 搜索项目源代码:在项目的源代码中搜索是否存在Spring Security相关的注释或配置。例如,在项目中搜索带有@SecurityConfig注释的Java类,或者搜索application.properties或application.yml文件中的与安全性相关的属性。

在Spring Boot中使用Spring Security处理安全性问题时,使用Spring提供的跨域支持,创建一个以SecurityConfig命名的JavaClass文件(配置文件),并写入如下代码

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // ...
        http.cors();
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
        configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

至此就解决了前后端访问的跨域问题。

二、post请求403错误

参考这篇博客《SpringSecurity发送post请求403错误》
简言之,在上述配置文件的代码里加一句:

http.csrf().disable();//解决403问题

完整代码如下:

package com.sdcw.program.config;

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.Arrays;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // ...
        http.cors();
        http.csrf().disable();//解决403问题
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
        configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值