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


一、启用了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;
    }
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security中,可以通过配置CorsFilter来处理跨域请求。具体做法如下: 1. 在Spring Boot项目中添加依赖 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建CorsFilter配置类 ```java @Configuration public class CorsConfig { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowedHeaders("*") .allowCredentials(true) .maxAge(3600); } }; } } ``` 3. 配置Spring Security ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable() .authorizeRequests() .antMatchers(HttpMethod.OPTIONS).permitAll() .antMatchers("/api/**").authenticated() .anyRequest().permitAll() .and() .httpBasic(); } } ``` 以上代码中,我们使用了`WebMvcConfigurerAdapter`来配置CorsFilter,允许所有来源的请求,并指定允许的HTTP方法、请求头和Credentials。在Spring Security中,我们使用`http.cors()`来启用CorsFilter,同时禁用了CSRF防护。 需要注意的是,由于跨域请求中会先发送OPTIONS请求,因此我们需要在Spring Security中允许OPTIONS请求。上述示例中,我们使用`antMatchers(HttpMethod.OPTIONS).permitAll()`来允许所有的OPTIONS请求

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值