【安全漏洞】SpringBoot + SpringSecurity CORS跨域资源共享配置

3 篇文章 0 订阅
2 篇文章 0 订阅

SpringBoot CORS跨域资源共享



前言

一个健壮的系统上线时,以及后续验收过程中,通常都会做系统安全测评,这是信息系统正式上线安全运行的重要环节。我们大概率会遇到一个叫:跨域资源共享漏洞的漏洞要求整改,具体描述大概是:“经测试发现服务器存在不安全的CORS配置,存在恶意跨域请求和内部信息泄露的风险。”今天我们就来了解一下这个CROS漏洞,并记录一下在整合了springSecurity这一套安全框架下的springboot系统中如何在后端过滤器中通过正确注册WebMvcConfigurer bean来配置CORS的全局设置,实现对非白名单访问源的拦截。


一、什么是CORS?

CORS,全称是“跨源资源共享”(Cross-Origin Resource Sharing),是一种Web应用程序的安全机制,用于控制不同源的资源之间的交互。
在Web应用程序中,CORS定义了一种机制,通过该机制,浏览器能够限制哪些外部网页可以访问来自不同源的资源。源由协议、域名和端口组成。当一个网页请求另一个网页上的资源时,浏览器会检查请求是否符合CORS规范,以确定是否允许该请求。
CORS的工作原理是:当浏览器发送一个跨域请求时,它会附加一些额外的头部信息到请求中,这些头部信息包含了关于请求的来源和目的的信息。服务器可以检查这些头部信息并决定是否允许该请求。如果服务器允许请求,它会返回一个响应,其中包含一个名为“Access-Control-Allow-Origin”的头部信息,该信息指定了哪些源可以访问该资源。浏览器会检查返回的“Access-Control-Allow-Origin”头部信息,以确定是否允许该跨域请求。通过使用CORS,开发人员可以控制哪些外部网页可以访问他们的资源,从而提高应用程序的安全性。

二、配置CORS方法

1.nginx中配置跨域资源访问策略

nginx中配置跨域资源访问策略,在之前的博客中已经写过具体配置方法了,需要的朋友直接移步以下博客链接查看:
https://blog.csdn.net/weixin_42925623/article/details/134548502

2.springSecurity 过滤器链中配置跨域资源访问策略

在Spring框架中,我们可以在引入Spring Security依赖后,对Security的HttpSecurity进行设置,来开启CORS(跨域/源资源共享),同时能指定只被部分域/源、部分方法、部分头部信息访问资源。
(1)SecurityConfig中过滤器链的配置示例:

@Bean
    @Order(3)
    public SecurityFilterChain defaultFilterChain(HttpSecurity http
            , IGuavaCacheService guavaCacheService
            , IUsersService usersService
            , ICaptchaApi captchaApi
            , JwtEncoder jwtEncoder
            , JwtDecoder jwtDecoder) throws Exception {

        http.authorizeHttpRequests(requestMatcherRegistryCustomizer)
                .csrf().disable()
                //.cors().disable()//关闭cros配置
                //.cors(withDefaults())//默认cros配置允许所有来源访问
                .cors().and()//开启cros配置
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authenticationProvider(jwtAuthenticationProvider(jwtDecoder))

                //在UsernamePasswordAuthenticationFilter前边添加自定义用户认证处理器
                .addFilterBefore(new CustomUserAuthenticationFilter(authProperties.getLoginProcessingPath(), guavaCacheService.userLockService()), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(new CaptAuthenticationFilter(captchaApi, authProperties.getLoginProcessingPath()), CustomUserAuthenticationFilter.class)

                //主要用于控制UsernamePasswordAuthenticationFilter行为
                .formLogin()
                //login静态页面地址,需要和前端静态页面保持一致
                .loginPage(authProperties.getFrontLoginUrl())
                .loginProcessingUrl(authProperties.getLoginProcessingPath())
                .permitAll()
                .successHandler(new ResultAuthenticationSuccessHandler(usersService,
                        new JwtUtils(jwtEncoder), authProperties.getJwtLifetime(), authProperties.getIssuer(), macService))
                .failureHandler(new ResultAuthenticationFailedHandler(guavaCacheService.userLockService()))
                .and()
                //处理业务层的认证异常和权限问题,主要用于控制ExceptionTranslationFilter行为
                .exceptionHandling()
                .authenticationEntryPoint(new RedirectAuthenticationEntryPoint(authProperties.getFrontLoginUrl()))
                .and()
                //jwt认证
                .oauth2ResourceServer()
                .authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint())
                .jwt()
                .decoder(jwtDecoder);

        //让jwt过滤器共用认证器
        DefaultSecurityFilterChain build = http.build();
        return build;
    }
   

重点在于cors 这一块语法配置
//关闭cros配置
.cors().disable()
//使用默认cros配置允许所有来源访问
.cors(withDefaults())
//开启cros配置,配置规则以自定义注册的cros配置为准
.cors().and()

(2)使用addCorsMappings方法来配置CORS
在Spring框架中,我们可以使用addCorsMappings(CorsRegistry registry)方法来配置CORS。
这个方法接收一个CorsRegistry对象,我们可以在这个对象中添加我们的路径和对应的CORS配置。
以下是addCorsMappings方法来配置CORS示例代码:

package com.dg.dp.user.auth.server.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author:
 * @Date:2024/9/4 18:16
 * CORS跨域资源共享允许访问源
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${allow.origin}")
    String allowOrigin;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        String[] allowDomain = allowOrigin.split(",");

        // 允许所有路径的跨域请求
        registry.addMapping("/**")
                // 配置允许的访问源
                .allowedOrigins(allowDomain)
                // 允许的请求方法
                .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
                // 允许的请求头
                .allowedHeaders("*")
                // 是否允许携带凭证
                .allowCredentials(true);
    }
}

其中,allowOrigin为yml配置文件中配置的访问源白名单:

#CORS跨域资源共享允许访问源白名单,多个访问源用逗号分隔
allow:
  origin: http://example.com,http://127.1.2.3:28080

.allowedOrigins()配置允许访问源可以为多个,可以放到字符串数组塞进去;
.addMapping(“/**”)表示允许所有路径的跨域请求;
WebMvcConfigurer 需要pom.xml文件引入依赖:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

(3)postman测试配置后的效果
同域下的请求(即Origin配置域名与接口路径前缀域名相同),不存在跨域问题,所以直接可以访问:
在这里插入图片描述
注意,如果请求的地址前缀是ip:port,那需要请求头配置的Origin值的ip:port跟接口请求路径中的ip端口一模一样,如果不一样会被认为是跨域请求,这时候如果代码的allowedOrigins配置白名单也没有该地址,就会被当做异常请求被拦截。
请求头携带的Origin中ip和接口中ip一致,端口不一致会被拦截:
在这里插入图片描述
请求头携带的Origin中配置的域名在代码allowedOrigins配置白名单中,会被认为是可信访问源而被放行:
在这里插入图片描述
在这里插入图片描述

请求头携带的Origin中配置的域名不在代码allowedOrigins配置白名单中,也不与接口前缀域名相同时,会被认为是非法访问源而被拦截掉:
在这里插入图片描述

3.springBoot 中配置跨域资源访问策略

如果springBoot项目没有引入springSecurity框架,这个时候,以上使用addCorsMappings(CorsRegistry registry)方法,或者接下来要讲的,创建一个 CorsFilter 的 Bean 并将其添加到 Spring 容器中,都可以实现cors配置。
addCorsMappings方法来配置CORS示例代码:

package com.dg.dp.user.auth.server.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author:
 * @Date:2024/9/4 18:16
 * CORS跨域资源共享允许访问源
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${allow.origin}")
    String allowOrigin;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        String[] allowDomain = allowOrigin.split(",");

        // 允许所有路径的跨域请求
        registry.addMapping("/**")
                // 配置允许的访问源
                .allowedOrigins(allowDomain)
                // 允许的请求方法
                .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
                // 允许的请求头
                .allowedHeaders("*")
                // 是否允许携带凭证
                .allowCredentials(true);
    }
}

创建一个配置类CorsConfig,并CorsFilter corsFilter()方法作为bean注入到spring 容器,代码示例如下:

package com.digitalgd.sasacdataboard.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author:
 * @Date:2024/9/5 14:55
 */

@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("http://example.com");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(source);
    }

}


总结

以上就是CROS跨域资源共享安全漏洞修复方法的全部内容,欢迎评论区交流学习!

  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值