SpringSecurity源码学习五:跨域与跨站请求伪造

news/2025/2/21 7:13:17/

目录

  • 什么是跨域
  • springboot是怎么解决跨域问题
  • 在springSecurity中怎么解决跨域问题
  • CORS源码
  • 跨域请求伪造CSRF
  • CSRF源码
  • 总结

什么是跨域

跨域是指在网络中,当一个网页的资源(如字体、脚本或样式表)尝试从不同的域名、端口或协议请求数据时,会遇到安全限制问题。这是由于浏览器的同源策略所导致的。同源策略要求网页只能从同一域名下加载资源,而跨域请求则违反了这个策略。

为了解决跨域问题,可以采取一些方法,如使用JSONP、CORS、代理服务器等。JSONP是通过动态创建

总结起来,跨域是指在网络中由于浏览器的同源策略而限制了不同域名、端口或协议之间的资源请求。通过使用适当的跨域解决方案,可以允许跨域请求并获取所需的数据。一般情况下我们呢都是使用CORS(跨域资源共享)来解决跨域问题。

springboot是怎么解决跨域问题

在Java中,可以使用CORS(跨域资源共享)来解决跨域问题。下面是一个Java代码示例,演示如何在Spring Boot应用程序中配置CORS。

首先,在Spring Boot应用程序的配置类中添加以下代码:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class CorsConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("http://example.com")  // 允许的跨域请求来源.allowedMethods("GET", "POST")  // 允许的请求方法.allowedHeaders("Origin", "Content-Type", "Accept")  // 允许的请求头.allowCredentials(true);  // 允许发送身份凭证}
}

在上述代码中,我们通过 addMapping() 方法指定了允许跨域请求的路径,使用 allowedOrigins() 方法指定了允许的跨域请求来源,使用 allowedMethods() 方法指定了允许的请求方法,使用 allowedHeaders() 方法指定了允许的请求头。最后,通过 allowCredentials(true) 方法允许发送身份凭证(如Cookie)。

以上只是个代码示例,实际使用时要根据具体情况做响应配置。

在springSecurity中怎么解决跨域问题

在Spring Security中,你可以使用 CorsConfigurationSource 接口来配置CORS(跨域资源共享)策略。下面是一个Java代码示例,演示如何在Spring Security中配置CORS。
首先,在你的Spring Security配置类中添加以下代码:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.cors();// ...}@Beanpublic CorsConfigurationSource corsConfigurationSource() {CorsConfiguration configuration = new CorsConfiguration();configuration.setAllowedOrigins(Arrays.asList("http://localhost:8080"));configuration.setAllowedMethods(Arrays.asList("GET", "POST"));configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", configuration);return source;}
}

在上述代码示例中,我们通过调用 http.cors()> 方法来允许跨域请求。然后,我们定义了一个 CorsConfigurationSource 的Bean,用于配置CORS策略。在这个Bean中,我们设置了允许的跨域请求来源、允许的请求方法和请求头。

最后,我们使用 UrlBasedCorsConfigurationSource 类来注册CORS配置,并将其应用于所有的请求路径( /** )。

CORS源码

源码入口:

	public CorsConfigurer<HttpSecurity> cors() throws Exception {return getOrApply(new CorsConfigurer<>());}private <C extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity>> C getOrApply(C configurer)throws Exception {C existingConfig = (C) getConfigurer(configurer.getClass());if (existingConfig != null) {return existingConfig;}//执行configurerreturn apply(configurer);}

可以看到最终就是获取一个CorsConfigurer类,我们看CorsConfigurer类的逻辑。主要看CorsConfigurer类的configure()方法

	@Overridepublic void configure(H http) {ApplicationContext context = http.getSharedObject(ApplicationContext.class);//获取corsFilter过滤器CorsFilter corsFilter = getCorsFilter(context);Assert.state(corsFilter != null, () -> "Please configure either a " + CORS_FILTER_BEAN_NAME + " bean or a "+ CORS_CONFIGURATION_SOURCE_BEAN_NAME + "bean.");//把corsFilter过滤器添加到过滤器链中http.addFilter(corsFilter);}

获取corsFilter过滤器并把它添加到过滤器链中。看getCorsFilter(context)方法

	private CorsFilter getCorsFilter(ApplicationContext context) {if (this.configurationSource != null) {return new CorsFilter(this.configurationSource);}//corsFilter过滤器bean是否存在boolean containsCorsFilter = context.containsBeanDefinition(CORS_FILTER_BEAN_NAME);if (containsCorsFilter) {return context.getBean(CORS_FILTER_BEAN_NAME, CorsFilter.class);}//corsConfigurationSource对象是否存在boolean containsCorsSource = context.containsBean(CORS_CONFIGURATION_SOURCE_BEAN_NAME);if (containsCorsSource) {//把corsConfigurationSource对象塞进CorsFilter过滤器中CorsConfigurationSource configurationSource = context.getBean(CORS_CONFIGURATION_SOURCE_BEAN_NAME,CorsConfigurationSource.class);return new CorsFilter(configurationSource);}boolean mvcPresent = ClassUtils.isPresent(HANDLER_MAPPING_INTROSPECTOR, context.getClassLoader());if (mvcPresent) {return MvcCorsFilter.getMvcCorsFilter(context);}return null;}

可以看到,对应cors处理有两种情况,

  1. 没有配置corsConfigurationSource对象,直接使用默认的corsFilter过滤器
  2. 配置corsConfigurationSource对象,把配置corsConfigurationSource对象添加到默认的过滤器。

最后是调用的springWeb的CorsFilter,并把此过滤器加入到SpringSecurity过滤器链中。

跨域请求伪造CSRF

跨域请求伪造(Cross-Site Request Forgery,CSRF)是一种安全漏洞,攻击者利用该漏洞通过伪造请求来执行未经授权的操作。在CSRF攻击中,攻击者诱使受害者在已登录的状态下访问恶意网站,从而触发受害者在其他网站上的操作,如转账、更改密码等。

在前后端分离的架构中,可以使用CSRF令牌(CSRF Token)来验证请求的合法性。下面是一个使用Spring Security防止CSRF攻击的Java代码示例:

  1. 配置Spring Security启用CSRF保护:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http// 其他Spring Security配置.and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());}
}
  1. 在前端页面中设置CSRF令牌:
html
<html>
<body><form method="post" action="/submit"><input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /><!-- 其他表单字段 --><button type="submit">提交</button></form>
</body>
</html>

在上述示例中,首先通过 csrf() 方法启用了CSRF保护,并使用 CookieCsrfTokenRepository 来存储CSRF令牌。然后,在前端页面的表单中,通过 ${_csrf.parameterName} 和 ${_csrf.token} 获取CSRF令牌,并将其作为隐藏字段传递给服务器。

这样,在每次提交表单时,CSRF令牌会被包含在请求中,服务器会验证请求中的CSRF令牌是否与服务器生成的令牌匹配,从而防止CSRF攻击的发生。

CSRF源码

代码入口:

	public CsrfConfigurer<HttpSecurity> csrf() throws Exception {ApplicationContext context = getContext();return getOrApply(new CsrfConfigurer<>(context));}private <C extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity>> C getOrApply(C configurer)throws Exception {C existingConfig = (C) getConfigurer(configurer.getClass());if (existingConfig != null) {return existingConfig;}//执行configurerreturn apply(configurer);}

主要是看CsrfConfigurer这个类

public final class CsrfConfigurer<H extends HttpSecurityBuilder<H>>extends org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer<CsrfConfigurer<H>, H> {//初始化CsrfTokenRepository,默认为LazyCsrfTokenRepositoryprivate CsrfTokenRepository csrfTokenRepository = new LazyCsrfTokenRepository(new HttpSessionCsrfTokenRepository());

默认是使用LazyCsrfTokenRepository,并使用HttpSessionCsrfTokenRepository初始化。接下俩看CsrfConfigurer的configure方法。

	@Overridepublic void configure(H http) {//初始化CsrfFilter过滤器CsrfFilter filter = new CsrfFilter(this.csrfTokenRepository);RequestMatcher requireCsrfProtectionMatcher = getRequireCsrfProtectionMatcher();if (requireCsrfProtectionMatcher != null) {filter.setRequireCsrfProtectionMatcher(requireCsrfProtectionMatcher);}AccessDeniedHandler accessDeniedHandler = createAccessDeniedHandler(http);if (accessDeniedHandler != null) {filter.setAccessDeniedHandler(accessDeniedHandler);}org.springframework.security.config.annotation.web.configurers.LogoutConfigurer<H> logoutConfigurer = http.getConfigurer(org.springframework.security.config.annotation.web.configurers.LogoutConfigurer.class);if (logoutConfigurer != null) {logoutConfigurer.addLogoutHandler(new CsrfLogoutHandler(this.csrfTokenRepository));}org.springframework.security.config.annotation.web.configurers.SessionManagementConfigurer<H> sessionConfigurer = http.getConfigurer(org.springframework.security.config.annotation.web.configurers.SessionManagementConfigurer.class);if (sessionConfigurer != null) {sessionConfigurer.addSessionAuthenticationStrategy(getSessionAuthenticationStrategy());}filter = postProcess(filter);//加载到过滤器链中http.addFilter(filter);}

这段很简单,初始化CsrfFilter,并把CsrfFilter添加到过滤器链中。

在LazyCsrfTokenRepository中,就是对CsrfToken的操作

public interface CsrfTokenRepository {/*** Generates a {@link CsrfToken}* @param request the {@link HttpServletRequest} to use* @return the {@link CsrfToken} that was generated. Cannot be null.*/CsrfToken generateToken(HttpServletRequest request);/*** Saves the {@link CsrfToken} using the {@link HttpServletRequest} and* {@link HttpServletResponse}. If the {@link CsrfToken} is null, it is the same as* deleting it.* @param token the {@link CsrfToken} to save or null to delete* @param request the {@link HttpServletRequest} to use* @param response the {@link HttpServletResponse} to use*/void saveToken(CsrfToken token, HttpServletRequest request, HttpServletResponse response);/*** Loads the expected {@link CsrfToken} from the {@link HttpServletRequest}* @param request the {@link HttpServletRequest} to use* @return the {@link CsrfToken} or null if none exists*/CsrfToken loadToken(HttpServletRequest request);}

具体的实现类就不展开说,感兴趣的可自行查看代码。

总结

  1. 跨域问题:当前端应用和后端API不在同一个域下时,浏览器会限制跨域请求。为了解决跨域问题,可以在后端配置允许跨域请求的头信息,如Access-Control-Allow-Origin。
  2. CSRF保护:跨站请求伪造(CSRF)是一种安全漏洞,攻击者利用用户在其他网站上的身份信息发起恶意请求。为了防止CSRF攻击,可以在Spring Security中启用CSRF保护。通过生成和验证CSRF令牌,确保请求的合法性。
    • 在后端配置中启用CSRF保护,并指定CSRF令牌的存储方式,如CookieCsrfTokenRepository。
    • 在前端发起请求时,将CSRF令牌作为请求的参数或头信息的一部分发送给后端。
    • 后端服务器验证请求中的CSRF令牌是否与服务器生成的令牌匹配,以确保请求的合法性。

http://www.ppmy.cn/news/1168487.html

相关文章

华为OD 最大差(100分)【java】A卷+B卷

华为OD统一考试A卷+B卷 新题库说明 你收到的链接上面会标注A卷还是B卷。目前大部分收到的都是B卷。 B卷对应20022部分考题以及新出的题目,A卷对应的是新出的题目。 我将持续更新最新题目 获取更多免费题目可前往夸克网盘下载,请点击以下链接进入: 我用夸克网盘分享了「华为O…

卷积神经网络CNN学习笔记-卷积计算Conv2D函数的理解

目录 1.全连接层存在的问题2.卷积运算3.填充(padding)3.1填充(padding)的意义 4.步幅(stride)5.三维数据的卷积运算6.结合方块思考7.批处理8.Conv2D函数解析9.conv2d代码9.1 stride19.2 stride2 参考文章 1.全连接层存在的问题 在全连接层中&#xff0c;相邻层的神经元全部连接…

Leetcode1833. 雪糕的最大数量

Every day a Leetcode 题目来源&#xff1a;1833. 雪糕的最大数量 解法1&#xff1a;贪心 排序 本题唯一的难点在于计数排序。 计数排序详解&#xff1a;C算法之计数排序 为了尽可能多的买到雪糕&#xff0c;我们选择从价格低的雪糕开始买&#xff0c;统计能够买到的雪糕…

运行stable-diffusion-xl-refiner-1.0遇到version `GLIBCXX_3.4.29‘ not found的问题

一、问题背景 https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0 在运行示例程序时候遇到GLIBCXX_3.4.29‘ not found diffusers to > 0.18.0 import torch from diffusers import StableDiffusionXLImg2ImgPipeline from diffusers.utils import loa…

【React】高频面试题

1. 简述下 React 的事件代理机制&#xff1f; React使用了一种称为“事件代理”&#xff08;Event Delegation&#xff09;的机制来处理事件。事件代理是指将事件处理程序绑定到组件的父级元素上&#xff0c;然后在需要处理事件的子元素上触发事件时&#xff0c;事件将被委托给…

【Qt控件之QToolBox】介绍及使用

概述 QToolBox类提供了一个列式的带有选项卡的小部件条目。工具箱是一个小部件&#xff0c;以一个列式的选项卡显示在上方&#xff0c;并在当前选项卡下方显示当前的小部件条目。每个选项卡在选项卡列中有一个索引位置。选项卡的小部件条目是一个QWidget。 每个小部件条目都有…

Oracle的立场:官网更换首页与以色列站在一起

Oracle公司的官网&#xff0c;更换了首页内容&#xff0c;明确表明立场&#xff1a;Oracle与以色列站在一起。 声明指出&#xff1a; Oracle谴责针对以色列及其公民的恐怖袭击。Oracle将为其员工、以色列政府和国防机构提供一切必要的支持。 Magen David Adom是一家为以色列公民…

传输层协议(TCP/UDP协议)

全文目录 端口号端口号范围划分 传输层UDP协议特点基于UDP的应用层协议 TCP协议确认应答机制&#xff08;可靠性&#xff09;延迟应答机制超时重传机制流量控制连接管理机制TIME_WAIT 状态CLOSE_WAIT 状态拥塞控制滑动窗口 TCP、UDP对比TCP的listen第二个参数 端口号 在套接字…