gpt4 book ai didi

java - HTTP 状态 403 - 未找到预期的 CSRF token

转载 作者:行者123 更新时间:2023-12-01 11:16:50 28 4
gpt4 key购买 nike

我正在从 Angular $http 向服务器发送信息,我得到了

HTTP Status 403 - Expected CSRF token not found. 

据我所知here一种解决方案是禁用 CSRF,但我不确定这是否是我想要的。请让我知道如何解决这个问题。

我在前端使用 Angular JS,在服务器端使用 Spring MVC 4 和 Spring security 3.2。

编辑:

SecurityConfig.java

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/signin")
.loginProcessingUrl("/signin/authenticate")
.failureUrl("/signin?param.error=bad_credentials")
.and()
.logout()
.logoutUrl("/signout")
.deleteCookies("JSESSIONID")
.and()
.authorizeRequests()
.antMatchers("search/**", "/c/**","/admin/**", "/favicon.ico", "/resources/**", "/auth/**", "/signin/**", "/signup/**", "/disconnect/facebook"/*, "/**"*/).permitAll()
.antMatchers("/**").authenticated()
.and()
.rememberMe()
.and()
.apply(
new SpringSocialConfigurer());
}

Angular JS ajax 请求:

$http({
method: 'POST',
url: 'addCampaign',
data: JSON.stringify(newCampaign)
}).
success(function (data, status, headers, config) {

//TODO Notification to show the campaign was successfully saved
$log.info("campaign successfully saved");
}).error(function (data, status, headers, config) {
$log.info("campaign could not be saved" + data + " " + status + " " + headers + " " + config);
//TODO to show notification that the campaign could not be saved succeffsully.
});

最佳答案

我找到了解决这个问题的方法。

我发现 spring 会将以下两个属性添加到我将返回的 JSP View 中

`${_csrf.token}` and `${_csrf.headerName}`

我正在 JSPmeta 标记中收集这些属性

<meta name="_csrf" content="${_csrf.token}" />

<meta name="_csrf_header" content="${_csrf.headerName}" />

在Javascript中,我编写了以下函数,

function getMetaContentByName(name, content) {
var content = (content == null) ? 'content' : content;
return document.querySelector("meta[name='" + name + "']").getAttribute(content);
}

在发送 ajax 请求时,我包含了存在的 CRFS token -

        $http({
method: 'POST',

headers: {
'X-CSRF-TOKEN': getMetaContentByName('_csrf');
},

url: requestURL,
data: data
}).
success(function (data, status, headers, config) {

$log.info("data : " + data);


})

关于java - HTTP 状态 403 - 未找到预期的 CSRF token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31723417/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com