Skip to content

Commit 5c05970

Browse files
authored
Update OncePerRequestFilter to match with spring-web
Closes gh-1658
1 parent 0cd0bfb commit 5c05970

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

gradle/dependency-management.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencyManagement {
1414
entry 'hazelcast-client'
1515
}
1616

17+
dependency 'org.aspectj:aspectjweaver:1.9.6'
1718
dependency 'com.h2database:h2:1.4.200'
1819
dependency 'com.ibm.db2:jcc:11.5.0.0'
1920
dependency 'com.microsoft.sqlserver:mssql-jdbc:7.4.1.jre8'

spring-session-core/spring-session-core.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ dependencies {
2525
testCompile "org.springframework.security:spring-security-core"
2626
testCompile "org.junit.jupiter:junit-jupiter-api"
2727
testCompile "org.junit.jupiter:junit-jupiter-params"
28+
testCompile "org.aspectj:aspectjweaver"
2829
testRuntime "org.junit.jupiter:junit-jupiter-engine"
2930
}

spring-session-core/src/main/java/org/springframework/session/web/http/OncePerRequestFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public final void doFilter(ServletRequest request, ServletResponse response, Fil
6464
}
6565
HttpServletRequest httpRequest = (HttpServletRequest) request;
6666
HttpServletResponse httpResponse = (HttpServletResponse) response;
67-
String alreadyFilteredAttributeName = this.alreadyFilteredAttributeName;
67+
String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
6868
boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;
6969

7070
if (hasAlreadyFilteredAttribute) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2014-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session.web.http;
18+
19+
import org.aspectj.lang.annotation.AfterReturning;
20+
import org.aspectj.lang.annotation.Aspect;
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.Mockito;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
28+
import org.springframework.mock.web.MockFilterChain;
29+
import org.springframework.mock.web.MockHttpServletRequest;
30+
import org.springframework.mock.web.MockHttpServletResponse;
31+
import org.springframework.session.SessionRepository;
32+
import org.springframework.session.web.http.OncePerRequestFilterAopTests.Config;
33+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
34+
35+
import static org.assertj.core.api.Assertions.assertThatCode;
36+
37+
@SpringJUnitConfig(classes = Config.class)
38+
class OncePerRequestFilterAopTests {
39+
40+
@Test
41+
void doFilterOnce(@Autowired final OncePerRequestFilter filter) {
42+
assertThatCode(() -> filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(),
43+
new MockFilterChain())).as("`doFilter` does not throw NPE with the bean is being proxied by Spring AOP")
44+
.doesNotThrowAnyException();
45+
}
46+
47+
@SuppressWarnings({ "rawtypes", "unchecked" })
48+
@Configuration
49+
@EnableAspectJAutoProxy(proxyTargetClass = true)
50+
@Aspect
51+
public static class Config {
52+
53+
@Bean
54+
public SessionRepository sessionRepository() {
55+
return Mockito.mock(SessionRepository.class);
56+
}
57+
58+
@Bean
59+
public SessionRepositoryFilter filter() {
60+
return new SessionRepositoryFilter(sessionRepository());
61+
}
62+
63+
@AfterReturning("execution(* SessionRepositoryFilter.doFilterInternal(..))")
64+
public void doInternalFilterPointcut() {
65+
// no op
66+
}
67+
68+
}
69+
70+
}

0 commit comments

Comments
 (0)