spring处理跨域

跨域一共有两张方式处理,一个是配置文件,一个是配置类

先来说配置类

1.配置类

这是配置类,cors跨域的配置,直接粘贴复制自己改改就行,如果里面有一直报红不消的话,那是cors提供的跨域配置依赖版本问题

package com.**.gateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOrigins("**")
            .allowCredentials(true)
            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
            .maxAge(3600);
    }

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 配置跨域
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许哪个请求头
        corsConfiguration.addAllowedHeader("*");
        // 允许哪个方法进行跨域
        corsConfiguration.addAllowedMethod("*");
        // 允许哪个请求来源进行跨域
        // corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedOriginPattern("*");
        // 是否允许携带cookie进行跨域
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }


}

2.配置文件

配置文件就是直接粘贴就好了

spring:
  cloud:
    gateway:
      globalcors: #全局跨越处理
        add-to-simple-url-handler-mapping: true #解决options请求被拦截问题
        cors-configurations:
          '[/**]': #拦截所有
            allowCredentials: true
            #allowedOrigins: "*"#允许哪些网址跨域
            allowedHeaders: "*"  #允许在请求中添加头信息
            allowedOriginPatterns: "*" #是否允许跨域携带cookie
            maxAge: 36000 #这次跨域检测的有效期,期间内不需要再检测跨域
            allowedMethods: #允许跨域ajax的请求方式
              - "GET"
              - "POST"
              - "PUT"
              - "DELETE"
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true

就可以了

Spring Security中处理请求可以通过以下几种方式实现: 1. 使用CORS(资源共享)配置:可以在Spring Security配置类中添加CorsConfigurationSource Bean,并设置允许的请求来源、方法、头部等信息。示例代码如下: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable() // 其他配置... } @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedOrigin("http://example.com"); configuration.addAllowedMethod("GET"); configuration.addAllowedHeader("Authorization"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } } ``` 2. 自定义Filter处理请求:可以创建一个自定义的Filter来处理请求。示例代码如下: ```java @Component public class CorsFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.setHeader("Access-Control-Allow-Origin", "http://example.com"); response.setHeader("Access-Control-Allow-Methods", "GET"); response.setHeader("Access-Control-Allow-Headers", "Authorization"); filterChain.doFilter(request, response); } } ``` 3. 使用Spring Security的CSRF保护:如果你启用了Spring Security的CSRF保护(默认情况下是启用的),则需要在请求中包含CSRF令牌。可以通过在前端的请求中添加CSRF令牌,并在Spring Security的配置中禁用CSRF保护来处理请求。 以上是一些处理Spring Security请求的常见方法,你可以根据自己的需求选择适合的方式进行配置。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值