Skip to content

Commit 029915f

Browse files
committed
Remove LazyCsrfTokenRepository usage
Closes gh-13194
1 parent 31f1604 commit 029915f

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/CsrfConfigurer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.springframework.security.web.csrf.CsrfTokenRepository;
4141
import org.springframework.security.web.csrf.CsrfTokenRequestHandler;
4242
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
43-
import org.springframework.security.web.csrf.LazyCsrfTokenRepository;
4443
import org.springframework.security.web.csrf.MissingCsrfTokenException;
4544
import org.springframework.security.web.session.InvalidSessionAccessDeniedHandler;
4645
import org.springframework.security.web.session.InvalidSessionStrategy;
@@ -83,7 +82,7 @@
8382
public final class CsrfConfigurer<H extends HttpSecurityBuilder<H>>
8483
extends AbstractHttpConfigurer<CsrfConfigurer<H>, H> {
8584

86-
private CsrfTokenRepository csrfTokenRepository = new LazyCsrfTokenRepository(new HttpSessionCsrfTokenRepository());
85+
private CsrfTokenRepository csrfTokenRepository = new HttpSessionCsrfTokenRepository();
8786

8887
private RequestMatcher requireCsrfProtectionMatcher = CsrfFilter.DEFAULT_CSRF_MATCHER;
8988

@@ -105,7 +104,7 @@ public CsrfConfigurer(ApplicationContext context) {
105104

106105
/**
107106
* Specify the {@link CsrfTokenRepository} to use. The default is an
108-
* {@link HttpSessionCsrfTokenRepository} wrapped by {@link LazyCsrfTokenRepository}.
107+
* {@link HttpSessionCsrfTokenRepository}.
109108
* @param csrfTokenRepository the {@link CsrfTokenRepository} to use
110109
* @return the {@link CsrfConfigurer} for further customizations
111110
*/

config/src/main/java/org/springframework/security/config/http/CsrfBeanDefinitionParser.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,6 @@
4343
import org.springframework.security.web.csrf.CsrfFilter;
4444
import org.springframework.security.web.csrf.CsrfLogoutHandler;
4545
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
46-
import org.springframework.security.web.csrf.LazyCsrfTokenRepository;
4746
import org.springframework.security.web.csrf.MissingCsrfTokenException;
4847
import org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor;
4948
import org.springframework.security.web.session.InvalidSessionAccessDeniedHandler;
@@ -109,13 +108,12 @@ public BeanDefinition parse(Element element, ParserContext pc) {
109108
this.requestHandlerRef = element.getAttribute(ATT_REQUEST_HANDLER);
110109
}
111110
if (!StringUtils.hasText(this.csrfRepositoryRef)) {
112-
RootBeanDefinition csrfTokenRepository = new RootBeanDefinition(HttpSessionCsrfTokenRepository.class);
113-
BeanDefinitionBuilder lazyTokenRepository = BeanDefinitionBuilder
114-
.rootBeanDefinition(LazyCsrfTokenRepository.class);
115-
lazyTokenRepository.addConstructorArgValue(csrfTokenRepository);
116-
this.csrfRepositoryRef = pc.getReaderContext().generateBeanName(lazyTokenRepository.getBeanDefinition());
117-
pc.registerBeanComponent(
118-
new BeanComponentDefinition(lazyTokenRepository.getBeanDefinition(), this.csrfRepositoryRef));
111+
BeanDefinitionBuilder httpSessionCsrfTokenRepository = BeanDefinitionBuilder
112+
.rootBeanDefinition(HttpSessionCsrfTokenRepository.class);
113+
this.csrfRepositoryRef = pc.getReaderContext()
114+
.generateBeanName(httpSessionCsrfTokenRepository.getBeanDefinition());
115+
pc.registerBeanComponent(new BeanComponentDefinition(httpSessionCsrfTokenRepository.getBeanDefinition(),
116+
this.csrfRepositoryRef));
119117
}
120118
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(CsrfFilter.class);
121119
builder.addConstructorArgReference(this.csrfRepositoryRef);

web/src/main/java/org/springframework/security/web/csrf/CsrfFilter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
*
5252
* <p>
5353
* Typically the {@link CsrfTokenRepository} implementation chooses to store the
54-
* {@link CsrfToken} in {@link HttpSession} with {@link HttpSessionCsrfTokenRepository}
55-
* wrapped by a {@link LazyCsrfTokenRepository}. This is preferred to storing the token in
56-
* a cookie which can be modified by a client application.
54+
* {@link CsrfToken} in {@link HttpSession} with {@link HttpSessionCsrfTokenRepository}.
55+
* This is preferred to storing the token in a cookie which can be modified by a client
56+
* application.
5757
* </p>
5858
*
5959
* @author Rob Winch
@@ -72,7 +72,7 @@ public final class CsrfFilter extends OncePerRequestFilter {
7272
/**
7373
* The attribute name to use when marking a given request as one that should not be
7474
* filtered.
75-
*
75+
* <p>
7676
* To use, set the attribute on your {@link HttpServletRequest}: <pre>
7777
* CsrfFilter.skipRequest(request);
7878
* </pre>

0 commit comments

Comments
 (0)