1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .web .filter ;
18
18
19
19
import java .io .IOException ;
20
+ import java .util .concurrent .locks .Lock ;
21
+ import java .util .concurrent .locks .ReentrantLock ;
20
22
21
23
import jakarta .servlet .Filter ;
22
24
import jakarta .servlet .FilterChain ;
@@ -97,7 +99,7 @@ public class DelegatingFilterProxy extends GenericFilterBean {
97
99
@ Nullable
98
100
private volatile Filter delegate ;
99
101
100
- private final Object delegateMonitor = new Object ();
102
+ private final Lock delegateLock = new ReentrantLock ();
101
103
102
104
103
105
/**
@@ -226,7 +228,8 @@ protected boolean isTargetFilterLifecycle() {
226
228
227
229
@ Override
228
230
protected void initFilterBean () throws ServletException {
229
- synchronized (this .delegateMonitor ) {
231
+ this .delegateLock .lock ();
232
+ try {
230
233
if (this .delegate == null ) {
231
234
// If no target bean name specified, use filter name.
232
235
if (this .targetBeanName == null ) {
@@ -241,6 +244,9 @@ protected void initFilterBean() throws ServletException {
241
244
}
242
245
}
243
246
}
247
+ finally {
248
+ this .delegateLock .unlock ();
249
+ }
244
250
}
245
251
246
252
@ Override
@@ -250,7 +256,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
250
256
// Lazily initialize the delegate if necessary.
251
257
Filter delegateToUse = this .delegate ;
252
258
if (delegateToUse == null ) {
253
- synchronized (this .delegateMonitor ) {
259
+ this .delegateLock .lock ();
260
+ try {
254
261
delegateToUse = this .delegate ;
255
262
if (delegateToUse == null ) {
256
263
WebApplicationContext wac = findWebApplicationContext ();
@@ -262,6 +269,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
262
269
}
263
270
this .delegate = delegateToUse ;
264
271
}
272
+ finally {
273
+ this .delegateLock .unlock ();
274
+ }
265
275
}
266
276
267
277
// Let the delegate perform the actual doFilter operation.
0 commit comments