angularjs - 如何增强对现有 spring boot 和 anjularjs 应用程序的 csrf 保护?
问题描述
我需要为我现有的 Spring Boot 应用程序添加 csrf 保护,其中 angularJS 是客户端框架。
解决方案
spring-security 需要设置 cookie 'XSRF-TOKEN' 但不在 HttpOnly 中。然后 Angular 的 javascript 将读取 cookie 并将其添加到传出 http 请求的标头中。
在你的 spring-boot 代码库中应该有一个类似于:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// class content
}
在这个类中应该有一个以“http”开头的链。
在这个链中添加
.csrf(csrf ->
csrf
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
)
来自文档:“默认情况下,CookieCsrfTokenRepository 将写入名为 XSRF-TOKEN 的 cookie,并从名为 X-XSRF-TOKEN 的标头或 HTTP 参数 _csrf 中读取它。这些默认值来自 AngularJS”。
推荐阅读
- ios - 在 Visual Studio 中将 iOS 应用程序部署到应用程序商店时面临问题
- java - 在 GATE 中使用 Stanford Parser 从 tokenID 获取令牌字符串
- javascript - reactjs如何下载文件
- cassandra - 从 cassandra 中提取数据 - OpenShift
- ios - 如何在 UIPageViewController 中的两个 ViewController 之间传递数据
- python - 使用 SSH 在 Raspberry Pi 上启动终端
- javascript - 将窗口调整大小添加到我的 Javascript
- python - 我可以/应该在 Docker 容器中添加数据库吗?
- angular - Angular 6 - httpClient 在 httpOptions 中传递基本身份验证
- node.js - 如何使用 node.js 从文本文件中提取信息