我在 Spring Boot+Angular 7 应用程序中实现 csrf,我担心的是当我登录时,我可以毫无问题地登录应用程序。但是在登录 csrf cookie 后没有改变,所以我从服务器收到 403 (CSRF is expired) 错误,
但是当我刷新时它会正常工作。主要原因是 cookie 没有正确获取,我不知道问题出在哪里,比如来自客户端或服务器端。请帮忙
我正在分享我的代码
春季启动代码
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().csrf() // csrf config starts here
.csrfTokenRepository(csrfTokenRepository())
.ignoringAntMatchers("/", "/login", "/captcha-servlet", "/validateOTP", "supportApp/logout")
.and()
.addFilterAfter(new CustomCsrfFilter(), CsrfFilter.class);
}
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}
CSRF 过滤器
public class CustomCsrfFilter extends 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();
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setPath("/");
response.addCookie(cookie);
}
filterChain.doFilter(request, response);
}
}
角 CSRF 代码
import { HttpClient, HttpXsrfTokenExtractor, HttpHeaders } from '@angular/common/http';
constructor(private http: HttpClient, private tokenExtractor: HttpXsrfTokenExtractor) { }
const _csrf_token = this.tokenExtractor.getToken() as string;
return this.http.post(this._singleuserUrl, uid, {headers: new HttpHeaders().set('X-XSRF-TOKEN', _csrf_token), withCredentials: true});