- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
帮助设置 header 以消除该错误消息:“在请求参数‘_csrf’或 header ‘X-CSRF-TOKEN’上发现无效的 CSRF token ‘null’。”
HTML:
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
我的 JS 代码:
var recipe = getRecipe();
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
console.log(token);
console.log(header);
console.log(recipe);
var headers = {};
// How set up header for include CSRF-Token
$.ajax({
url: "/recipe",
type: "POST",
dataType: "json",
contentType: "application/json",
headers: headers,
data: JSON.stringify(recipe, null, "\t"),
success: function(data) {
console.log(data);
},
error : getErrorMsg
});
我的 Controller 代码:
@RequestMapping(value = "/recipe", method = RequestMethod.POST, produces = {"application/json"})
@ResponseStatus(HttpStatus.OK)
public @ResponseBody
String addRecipe(@RequestBody String jsonString) {
Recipe recipe = Recipe.fromJson(jsonString);
recipe.setUser(getLoggedUser());
if (recipe.getCategory() != null)
recipe.setCategory(categoryService.findById(recipe.getCategory().getId()));
recipe.setFavoriteUsers(recipeService.findById(recipe.getId()).getFavoriteUsers());
recipe.setPhoto(recipeService.findById(recipe.getId()).getPhoto());
recipeService.save(recipe);
return recipe.toJson();
}
和安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().hasRole("USER")
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.successHandler(loginSuccessHandler())
.failureHandler(loginFailureHandler())
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/login")
.and()
.csrf();
}
我如何确定启用了 csrf?我必须如何设置我的 ajax 请求的 header ?任何帮助将不胜感激。
最佳答案
token 可以像您的示例一样读取:
var token = $("meta[name='_csrf']").attr("content");
然后您可以设置 jQuery 以在所有后续请求中将 CSRF token 作为请求 header 发送(您不必再担心):
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-TOKEN', token);
}
});
关于ajax - 如何在 ajax POST 请求中设置 header 以包含 CSRF token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39434971/
假设我的 Web 应用程序使用 CSRF token 防止 CSRF 攻击,此外,它使用 SSL 并防止 XSS 攻击。此外,出于这个问题的目的,假设它仅在最近的浏览器中使用并且它们没有错误。我可以使
很多人都在谈论实现 CSRF 来阻止对网页的跨站点攻击。但我认为破坏 CSRF 并向服务器发出请求非常容易。 那么它是如何工作的呢? 您从一个页面开始,呈现一个表单并使用 CSRF token 保留一
在阅读了许多有关 CSRF 的文档后,我仍然有点困惑。所以我希望有人可以向我解释一下: 假设我有一个仅供经过身份验证的用户使用的个人资料页面,比如说 abc.com/profile,它会显示我所有的私
我们基于 Angular 的 web 应用程序与在不同域和上下文路径上运行的企业门户集成。我正在使用基于 Spring Security 的 CSRF token 来验证传入的请求。该应用程序在本地完
我正在开发一个 Web API。身份验证是通过 cookie 进行的。所有端点通过JSON接收参数在请求正文中。 我需要实现 CSRF token保护他们?这怎么可能被利用呢?是否可以通过正常发送JS
我正在开发一个从 cookie header 解析 CSRF token 的应用程序。我想知道 CSRF token 是否使用 URL 安全字符进行 base64 编码(参见 https://simp
我知道当提交时表单中未包含 csrf token 时会发生此错误,但这次并非如此。 我正在尝试登录管理站点。管理员登录表单包含 csrf token ,我可以看到该 csrf token 的值与 cs
有没有办法对 Controller 的某些操作禁用 CSRF 验证,同时对其他操作保持启用状态? 就我而言,我有几个可配置的 Action 类,它们旨在注入(inject)到 Controller 中
我正在编写一个应用程序(Django,确实如此),我只想了解“CSRF token ”实际上是什么以及它如何保护数据。 如果不使用CSRF token ,发布数据不安全吗? 最佳答案 简单来说跨站请求
来自维基百科关于同源政策 https://en.wikipedia.org/wiki/Same-origin_policy The same-origin policy helps protect s
我在 Vue 环境中使用 axios 与用 Symfony 编写的网络服务对话。每个请求都需要设置一个 X-Auth-Token header 。该值存储在 auth_token cookie 中。
我想保护我的 REST 调用免受 XSRF 攻击。我正在做的是: 服务器在用户成功登录后向浏览器发送一个记录的 cookie。 在每个请求(GET、POST、DELETE)上,我将登录的 cookie
我知道这个问题以前有人问过。我已经尝试了人们给出的几乎所有选项,但我似乎无法解决它。我是一个完整的新手,所以请让我知道我哪里出错了。 我正在尝试编写一个简单的原始表单。到目前为止,我还没有实现任何身份
似乎 Laravel 5 默认将 CSRF 过滤器应用于所有非获取请求。这对于表单 POST 是可以的,但对于 POST DELETE 等的 API 可能是一个问题。 简单的问题: 如何设置没有 CS
当我从客户端向服务器发出 DELETE 请求时,我遇到了错误。 “CSRF token 已关联到此客户端”。响应代码:403 和响应头 { "cache-control": "no-cache,
to prevent CSRF attacks, a random CSRF secret has been generated. 以上内容来自 symfony: http://www.symfony
我正在使用 Django Rest Framework还有 django-rest-auth . 我有标准的 API 端点(/login、/logout、/registration...) 使用我的浏
这个问题在这里已经有了答案: OAuth2.0 Server stack how to use state to prevent CSRF? for draft2.0 v20 (3 个回答) 4年前关
我需要知道如何在 Impresspages cms 中禁用 CSRF 功能。我在之前的帖子中看到了一个可能的答案,但没有完全分类。当我的客户在 cleanwaterpartnership.co.uk
我正在使用 Django 1.7 和 django-rest-framework。 我制作了一个 API,它返回一些 JSON 数据并将其放入我的 settings.py REST_FRAMEWORK
我是一名优秀的程序员,十分优秀!