gpt4 book ai didi

Spring 在响应时不发送 CSRF token

转载 作者:行者123 更新时间:2023-12-03 19:53:02 24 4
gpt4 key购买 nike

自从我添加了 spring-security 以来,我在让我的 Angular2 客户端与我的 Spring 服务器通信时遇到了问题,即当我尝试将包含身份验证信息的 JSON 发送到服务器登录 url 时,我得到一个 403 "Invalid CSRF Token 'null ' 在请求参数 '_csrf' 或 header 'X-CSRF-TOKEN' 上找到。”

我知道我应该在响应的 header 上传递一个 token 以供客户端使用,但未传递此 token 。我试过 this answer但 token 仍未通过。我也通过 postman 发送请求,但 token 也没有到达。
webSecurityConfig.xml :

    <http entry-point-ref="restAuthenticationEntryPoint">
<intercept-url pattern="/api/**" access="hasRole('ROLE_USER')"/>

<form-login
authentication-success-handler-ref="mySuccessHandler"
authentication-failure-handler-ref="myFailureHandler"
/>

<logout />
</http>

<beans:bean id="mySuccessHandler"
class="com.eficid.cloud.security.rest.AuthenticationSuccessHandler"/>
<beans:bean id="myFailureHandler" class=
"org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"/>


<authentication-manager>
<authentication-provider>
<user-service>
<user name="temp" password="temp" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>

</beans:beans>
SpringSecurityConfiguration
@Configuration
@ImportResource({ "classpath:webSecurityConfig.xml" })
@ComponentScan("com.eficid.cloud.security.rest")
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

public SpringSecurityConfig() {
super();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests()
.antMatchers("/spring-security-rest/login").permitAll().anyRequest()
.authenticated().and().csrf()
.csrfTokenRepository(csrfTokenRepository()).and()
.addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
}


private Filter csrfHeaderFilter() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
String token = csrf.getToken();
if (cookie == null || token != null
&& !token.equals(cookie.getValue())) {
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setPath("/");
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response);
}
};
}

private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}

}

最佳答案

可能输入错误:

private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}

必须是 X-CSRF-TOKEN 而不是 X-XSRF-TOKEN。建议重构。

检查此链接: How to access Spring CSRF restful web service

关于Spring 在响应时不发送 CSRF token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36580295/

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