我有Spring Boot 2.1.0.RELEASE应用程序。我正在尝试理解登录和CSRF配置,所以我首先发送一个GET请求:
GET http://localhost:8080/devices
Accept: application/json
Authorization: Basic user test对我收到的JSessionID再次执行同样的操作,也是有效的:
GET http://localhost:8080/devices
Accept: application/json
#Authorization: Basic user test
cookie: JSESSIONID=162A7A29081CC89EA444423D9508F286到目前为止一切都很好。我收到了200个回复,我得到了一个CSRF令牌。后者:
HTTP/1.1 200
Set-Cookie: XSRF-TOKEN=2adcabdd-d804-4f13-a2a6-b95621ce868c; Path=/
....好了,现在我想直接尝试POST请求:
POST localhost:8080/devices/check
Content-Type: application/json
#Authorization: Basic user password
cookie: JSESSIONID=162A7A29081CC89EA444423D9508F286; XSRF-TOKEN=2adcabdd-d804-4f13-a2a6-b95621ce868c
#X-XSRF-TOKEN: 2adcabdd-d804-4f13-a2a6-b95621ce868c但是独立地,如果我尝试通过Cookie或Header传递XSRF令牌,我将得到一个403禁止。
这是我的Spring Security配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository
.withHttpOnlyFalse()).and()
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic().and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}我在这里做错了什么?
仅供参考:我正在使用IntelliJ超文本传输协议客户端。
发布于 2019-08-19 02:55:50
下面是我所做的:
从您的RequestContext检索您的cookie:
function getMyToken() {
Cookies[] cookies = getRequestContext().getRequest().getCookies()
for (cookie in cookies) {
if (cookie.getName().equals("XSRF-TOKEN")) return cookie.getValue()
}
}然后在我的表格中:
<input name="_csrf" value="getMyToken()"/>这对我很管用。
https://stackoverflow.com/questions/53823547
复制相似问题