DeferredCsrfToken Implements Supplier
Closes gh-16870 Signed-off-by: Daeho Kwon <trewq231@naver.com>
This commit is contained in:
parent
43ef4262da
commit
9908d96644
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -87,7 +87,7 @@ public class SessionManagementConfigurerServlet31Tests {
|
||||||
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
|
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
|
||||||
CsrfTokenRequestHandler handler = new XorCsrfTokenRequestAttributeHandler();
|
CsrfTokenRequestHandler handler = new XorCsrfTokenRequestAttributeHandler();
|
||||||
DeferredCsrfToken deferredCsrfToken = repository.loadDeferredToken(request, this.response);
|
DeferredCsrfToken deferredCsrfToken = repository.loadDeferredToken(request, this.response);
|
||||||
handler.handle(request, this.response, deferredCsrfToken::get);
|
handler.handle(request, this.response, deferredCsrfToken);
|
||||||
CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
|
CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
|
||||||
request.setParameter(token.getParameterName(), token.getToken());
|
request.setParameter(token.getParameterName(), token.getToken());
|
||||||
request.getSession().setAttribute("attribute1", "value1");
|
request.getSession().setAttribute("attribute1", "value1");
|
||||||
|
|
|
||||||
|
|
@ -524,7 +524,7 @@ public final class SecurityMockMvcRequestPostProcessors {
|
||||||
TestCsrfTokenRepository.enable(request);
|
TestCsrfTokenRepository.enable(request);
|
||||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
DeferredCsrfToken deferredCsrfToken = repository.loadDeferredToken(request, response);
|
DeferredCsrfToken deferredCsrfToken = repository.loadDeferredToken(request, response);
|
||||||
handler.handle(request, response, deferredCsrfToken::get);
|
handler.handle(request, response, deferredCsrfToken);
|
||||||
CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
|
CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
|
||||||
String tokenValue = this.useInvalidToken ? INVALID_TOKEN_VALUE : token.getToken();
|
String tokenValue = this.useInvalidToken ? INVALID_TOKEN_VALUE : token.getToken();
|
||||||
if (this.asHeader) {
|
if (this.asHeader) {
|
||||||
|
|
|
||||||
|
|
@ -164,7 +164,7 @@ public class SecurityMockMvcRequestPostProcessorsCsrfTests {
|
||||||
HttpSessionCsrfTokenRepository repo = new HttpSessionCsrfTokenRepository();
|
HttpSessionCsrfTokenRepository repo = new HttpSessionCsrfTokenRepository();
|
||||||
CsrfTokenRequestHandler handler = new XorCsrfTokenRequestAttributeHandler();
|
CsrfTokenRequestHandler handler = new XorCsrfTokenRequestAttributeHandler();
|
||||||
DeferredCsrfToken deferredCsrfToken = repo.loadDeferredToken(request, response);
|
DeferredCsrfToken deferredCsrfToken = repo.loadDeferredToken(request, response);
|
||||||
handler.handle(request, response, deferredCsrfToken::get);
|
handler.handle(request, response, deferredCsrfToken);
|
||||||
CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
|
CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
|
||||||
MockHttpServletRequestBuilder requestWithCsrf = post("/")
|
MockHttpServletRequestBuilder requestWithCsrf = post("/")
|
||||||
.param(token.getParameterName(), token.getToken())
|
.param(token.getParameterName(), token.getToken())
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -69,7 +69,7 @@ public final class CsrfAuthenticationStrategy implements SessionAuthenticationSt
|
||||||
if (containsToken) {
|
if (containsToken) {
|
||||||
this.tokenRepository.saveToken(null, request, response);
|
this.tokenRepository.saveToken(null, request, response);
|
||||||
DeferredCsrfToken deferredCsrfToken = this.tokenRepository.loadDeferredToken(request, response);
|
DeferredCsrfToken deferredCsrfToken = this.tokenRepository.loadDeferredToken(request, response);
|
||||||
this.requestHandler.handle(request, response, deferredCsrfToken::get);
|
this.requestHandler.handle(request, response, deferredCsrfToken);
|
||||||
this.logger.debug("Replaced CSRF Token");
|
this.logger.debug("Replaced CSRF Token");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2023 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -108,7 +108,7 @@ public final class CsrfFilter extends OncePerRequestFilter {
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
DeferredCsrfToken deferredCsrfToken = this.tokenRepository.loadDeferredToken(request, response);
|
DeferredCsrfToken deferredCsrfToken = this.tokenRepository.loadDeferredToken(request, response);
|
||||||
request.setAttribute(DeferredCsrfToken.class.getName(), deferredCsrfToken);
|
request.setAttribute(DeferredCsrfToken.class.getName(), deferredCsrfToken);
|
||||||
this.requestHandler.handle(request, response, deferredCsrfToken::get);
|
this.requestHandler.handle(request, response, deferredCsrfToken);
|
||||||
if (!this.requireCsrfProtectionMatcher.matches(request)) {
|
if (!this.requireCsrfProtectionMatcher.matches(request)) {
|
||||||
if (this.logger.isTraceEnabled()) {
|
if (this.logger.isTraceEnabled()) {
|
||||||
this.logger.trace("Did not protect against CSRF since request did not match "
|
this.logger.trace("Did not protect against CSRF since request did not match "
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,14 +16,17 @@
|
||||||
|
|
||||||
package org.springframework.security.web.csrf;
|
package org.springframework.security.web.csrf;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface that allows delayed access to a {@link CsrfToken} that may be generated.
|
* An interface that allows delayed access to a {@link CsrfToken} that may be generated.
|
||||||
*
|
*
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
* @author Steve Riesenberg
|
* @author Steve Riesenberg
|
||||||
|
* @author Daeho Kwon
|
||||||
* @since 5.8
|
* @since 5.8
|
||||||
*/
|
*/
|
||||||
public interface DeferredCsrfToken {
|
public interface DeferredCsrfToken extends Supplier<CsrfToken> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the {@link CsrfToken}
|
* Gets the {@link CsrfToken}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue