栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot Security - 启用 CSRF 保护

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot Security - 启用 CSRF 保护

在上一篇文章中,我们实现了 Spring Boot Security - Password Encoding Using Bcrypt。
但到目前为止,在我们所有的示例中,我们都禁用了 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

您打开此页面并单击惊喜按钮 -


我们看到它已将名为 Hacker 的员工添加到我们的应用程序中。这是 CSRF 攻击。接下来我们看看如何应对这种 CSRF 攻击。

让我们开始-

我们将使用 CSRF 安全令牌仅授予授权用户访问权限。
我们将修改我们在之前的 Spring Boot Security - Password Encoding Using Bcrypt
Maven Project 中开发的代码如下 -
      在 pom.xml 中添加 spring-security-taglibs 依赖项。


	4.0.0

	com.javainuse
	boot-form-handling
	0.0.1-SNAPSHOT
	jar

	boot-form-handling
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-security
		

		
			org.springframework.boot
			spring-boot-starter-jdbc
		

		
			mysql
			mysql-connector-java
			runtime
			5.1.21
		

		
			org.apache.tomcat.embed
			tomcat-embed-jasper
		

		
			javax.servlet
			jstl
		

		
			org.springframework.security
			spring-security-taglibs
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


    接下来,我们通过注释 csrf disabled 命令来修改安全配置以启用 CSRF。
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" %>
  
启动应用程序 -

转到 localhost:8080/welcome,我们将被重定向到自定义登录页面。

使用凭据登录


 再次点击 CSRF 攻击页面的惊喜按钮
  所以我们的应用程序现在运行良好。

下载源代码 下载它 -
Spring Boot Security - 保护应用程序免受 CSRF 攻击

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/726528.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号