微信登录

CSRF 保护 - 配置与禁用 - 配置或禁用 CSRF

CSRF 保护 - 配置与禁用 - 配置或禁用 CSRF

一、什么是 CSRF

CSRF(Cross-Site Request Forgery)即跨站请求伪造,是一种常见的 Web 安全漏洞。攻击者通过诱导用户在已登录的网站上执行恶意操作,利用用户的身份信息绕过身份验证。例如,用户在登录银行网站后,未退出登录,此时访问了恶意网站,恶意网站可能会伪装成用户向银行网站发送转账请求,由于用户处于登录状态,银行网站会认为是用户本人操作,从而导致资金损失。

二、Spring 中 CSRF 保护的配置

2.1 基于 Java 配置

在 Spring Security 中,默认情况下 CSRF 保护是开启的。下面是一个简单的 Java 配置示例:

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.web.SecurityFilterChain;
  5. @Configuration
  6. public class SecurityConfig {
  7. @Bean
  8. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  9. http
  10. .csrf(csrf -> csrf
  11. // 开启 CSRF 保护
  12. .ignoringAntMatchers("/public/**") // 忽略某些请求路径
  13. )
  14. .authorizeRequests(authorize -> authorize
  15. .antMatchers("/public/**").permitAll()
  16. .anyRequest().authenticated()
  17. )
  18. .formLogin();
  19. return http.build();
  20. }
  21. }

在上述代码中,我们使用 csrf() 方法开启 CSRF 保护,并通过 ignoringAntMatchers() 方法忽略了 /public/** 路径的 CSRF 检查。

2.2 在 JSP 中使用 CSRF 令牌

当 CSRF 保护开启时,表单提交需要包含 CSRF 令牌。在 JSP 中,可以使用 Spring 的标签库来自动添加 CSRF 令牌:

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <title>CSRF 表单示例</title>
  7. </head>
  8. <body>
  9. <form action="/submit" method="post">
  10. <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
  11. <input type="text" name="message" placeholder="输入消息">
  12. <input type="submit" value="提交">
  13. </form>
  14. </body>
  15. </html>

在上述代码中,我们通过 ${_csrf.parameterName}${_csrf.token} 获取 CSRF 令牌的参数名和值,并将其添加到表单的隐藏字段中。

2.3 在 Thymeleaf 中使用 CSRF 令牌

如果使用 Thymeleaf 作为模板引擎,可以更方便地处理 CSRF 令牌:

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>CSRF 表单示例</title>
  5. </head>
  6. <body>
  7. <form action="#" th:action="@{/submit}" th:method="post">
  8. <input type="text" name="message" placeholder="输入消息">
  9. <input type="submit" value="提交">
  10. </form>
  11. </body>
  12. </html>

Thymeleaf 会自动在表单中添加 CSRF 令牌。

三、Spring 中 CSRF 保护的禁用

3.1 基于 Java 配置禁用 CSRF

在某些情况下,可能需要禁用 CSRF 保护,例如开发 RESTful API 时。可以通过以下方式禁用:

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.web.SecurityFilterChain;
  5. @Configuration
  6. public class SecurityConfig {
  7. @Bean
  8. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  9. http
  10. .csrf(csrf -> csrf
  11. // 禁用 CSRF 保护
  12. .disable()
  13. )
  14. .authorizeRequests(authorize -> authorize
  15. .anyRequest().authenticated()
  16. )
  17. .formLogin();
  18. return http.build();
  19. }
  20. }

在上述代码中,我们使用 disable() 方法禁用了 CSRF 保护。

四、总结

操作 代码示例 适用场景
配置 CSRF 保护 http.csrf(csrf -> csrf.ignoringAntMatchers("/public/**")) 普通 Web 应用,需要保护用户免受 CSRF 攻击
在 JSP 中添加 CSRF 令牌 <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"> 使用 JSP 作为视图层的 Web 应用
在 Thymeleaf 中添加 CSRF 令牌 无需手动添加,Thymeleaf 自动处理 使用 Thymeleaf 作为视图层的 Web 应用
禁用 CSRF 保护 http.csrf(csrf -> csrf.disable()) 开发 RESTful API 等不需要 CSRF 保护的场景

五、注意事项

  • 禁用 CSRF 保护会增加应用的安全风险,应谨慎使用。
  • 在配置 CSRF 保护时,需要确保所有需要保护的请求都包含 CSRF 令牌。

通过以上配置和禁用 CSRF 保护的方法,可以根据应用的实际需求来保障 Web 应用的安全性。

CSRF 保护 - 配置与禁用 - 配置或禁用 CSRF