我有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或标头传递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();
}
}
我在这里做错了什么?
最好的问候, 迈克尔
fyi:我很满意IntelliJ http客户端。
答案 0 :(得分:0)
这就是我所做的:
从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()"/>
对我有用。