在上一篇文章中,我们实现了 Spring Boot Security - Password Encoding Using Bcrypt。
但到目前为止,在我们所有的示例中,我们都禁用了 CSRF。CSRF 代表跨站点请求伪造。这是一种强制最终用户在当前对其进行身份验证的 Web 应用程序上执行不需要的操作的攻击。CSRF 攻击专门针对状态更改请求,而不是窃取数据,因为攻击者无法查看对伪造请求的响应。
不要关闭上面的窗口。现在假设您收到一封包含以下内容的邮件。
我们看到它已将名为 Hacker 的员工添加到我们的应用程序中。这是 CSRF 攻击。接下来我们看看如何应对这种 CSRF 攻击。
我们将修改我们在之前的 Spring Boot Security - Password Encoding Using Bcrypt
Maven Project 中开发的代码如下 -
但到目前为止,在我们所有的示例中,我们都禁用了 CSRF。CSRF 代表跨站点请求伪造。这是一种强制最终用户在当前对其进行身份验证的 Web 应用程序上执行不需要的操作的攻击。CSRF 攻击专门针对状态更改请求,而不是窃取数据,因为攻击者无法查看对伪造请求的响应。
Spring Boot 安全性 - 目录
Spring Boot + 简单的安全配置 Spring Boot Form Security Login Hello World 示例 Spring Boot Security - 自定义登录页面示例 Spring Boot Security - JDBC 身份验证示例 Spring Boot Security - 使用 JdbcUserDetailsManager 以编程方式创建用户 Spring Boot Security - 使用 Bcrypt 进行密码编码 Spring Boot Security -启用 CSRF 保护 Spring Boot 安全性 - 身份验证处理程序示例 Spring Boot 安全性 - OAuth 简介 Spring Boot OAuth2 第 1 部分 - 获取授权代码 Spring Boot OAuth2 第 2 部分 - 获取访问令牌并使用它来获取数据。视频
本教程在下面的 Youtube 视频中进行了解释。了解 CSRF 攻击-
以前我们有 Spring Boot Security - Password Encoding Using Bcrypt。启动此应用程序并使用有效密码登录。不要关闭上面的窗口。现在假设您收到一封包含以下内容的邮件。
Hi JavaInUse
<form method = "post" action="http://localhost:8080/addNewEmployee">
<input id ="empId" type="hidden" name="empId" value="Hacker001"/>
<input id ="empName" type="hidden" name="empName" value="hacker"/>
<input type="SUBMIT" value="Surprise..Surprise..See What This Does" />
</form>
您打开此页面并单击惊喜按钮 -
我们看到它已将名为 Hacker 的员工添加到我们的应用程序中。这是 CSRF 攻击。接下来我们看看如何应对这种 CSRF 攻击。
让我们开始-
我们将使用 CSRF 安全令牌仅授予授权用户访问权限。我们将修改我们在之前的 Spring Boot Security - Password Encoding Using Bcrypt
Maven Project 中开发的代码如下 -
在 pom.xml 中添加 spring-security-taglibs 依赖项。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javainuse</groupId>
<artifactId>boot-form-handling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>boot-form-handling</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.javainuse.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.JdbcUserDetailsManager;
@Configuration
@EnableWebSecurity
public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// Enable jdbc authentication
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder());
}
@Bean
public JdbcUserDetailsManager jdbcUserDetailsManager() throws Exception {
JdbcUserDetailsManager jdbcUserDetailsManager = new JdbcUserDetailsManager();
jdbcUserDetailsManager.setDataSource(dataSource);
return jdbcUserDetailsManager;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/register").permitAll().antMatchers("/welcome")
.hasAnyRole("USER", "ADMIN").antMatchers("/getEmployees").hasAnyRole("USER", "ADMIN")
.antMatchers("/addNewEmployee").hasAnyRole("ADMIN").anyRequest().authenticated().and().formLogin()
.loginPage("/login").permitAll().and().logout().permitAll();
//http.csrf().disable();
}
// @Autowired
// public void configureGlobal(AuthenticationManagerBuilder authenticationMgr)
// throws Exception {
// authenticationMgr.inMemoryAuthentication().withUser("admin").password("admin").authorities("ROLE_USER").and()
// .withUser("javainuse").password("javainuse").authorities("ROLE_USER",
// "ROLE_ADMIN");
// }
}
接下来在所有jsp页面中添加spring security taglib和csrf token tag
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:csrfInput />
启动应用程序 -
- 转到 localhost:8080/welcome,我们将被重定向到自定义登录页面。
- 使用凭据登录
- 再次点击 CSRF 攻击页面的惊喜按钮
下载源代码
下载它 -Spring Boot Security - 保护应用程序免受 CSRF 攻击