@@ -164,13 +164,8 @@ public void doFilterWhenNoSessionThenChainIsContinued() throws Exception {
164
164
MockHttpServletRequest request = new MockHttpServletRequest ();
165
165
MockHttpServletResponse response = new MockHttpServletResponse ();
166
166
RedirectStrategy redirect = mock (RedirectStrategy .class );
167
- SessionRegistry registry = mock (SessionRegistry .class );
168
- SessionInformation information = new SessionInformation ("user" , "sessionId" ,
169
- new Date (System .currentTimeMillis () - 1000 ));
170
- information .expireNow ();
171
- given (registry .getSessionInformation (anyString ())).willReturn (information );
172
167
String expiredUrl = "/expired" ;
173
- ConcurrentSessionFilter filter = new ConcurrentSessionFilter (registry , expiredUrl );
168
+ ConcurrentSessionFilter filter = new ConcurrentSessionFilter (mockSessionRegistry () , expiredUrl );
174
169
filter .setRedirectStrategy (redirect );
175
170
MockFilterChain chain = new MockFilterChain ();
176
171
filter .doFilter (request , response , chain );
@@ -199,13 +194,8 @@ public void doFilterWhenCustomRedirectStrategyThenCustomRedirectStrategyUsed() t
199
194
request .setSession (session );
200
195
MockHttpServletResponse response = new MockHttpServletResponse ();
201
196
RedirectStrategy redirect = mock (RedirectStrategy .class );
202
- SessionRegistry registry = mock (SessionRegistry .class );
203
- SessionInformation information = new SessionInformation ("user" , "sessionId" ,
204
- new Date (System .currentTimeMillis () - 1000 ));
205
- information .expireNow ();
206
- given (registry .getSessionInformation (anyString ())).willReturn (information );
207
197
String expiredUrl = "/expired" ;
208
- ConcurrentSessionFilter filter = new ConcurrentSessionFilter (registry , expiredUrl );
198
+ ConcurrentSessionFilter filter = new ConcurrentSessionFilter (mockSessionRegistry () , expiredUrl );
209
199
filter .setRedirectStrategy (redirect );
210
200
filter .doFilter (request , response , new MockFilterChain ());
211
201
verify (redirect ).sendRedirect (request , response , expiredUrl );
@@ -218,13 +208,8 @@ public void doFilterWhenOverrideThenCustomRedirectStrategyUsed() throws Exceptio
218
208
request .setSession (session );
219
209
MockHttpServletResponse response = new MockHttpServletResponse ();
220
210
RedirectStrategy redirect = mock (RedirectStrategy .class );
221
- SessionRegistry registry = mock (SessionRegistry .class );
222
- SessionInformation information = new SessionInformation ("user" , "sessionId" ,
223
- new Date (System .currentTimeMillis () - 1000 ));
224
- information .expireNow ();
225
- given (registry .getSessionInformation (anyString ())).willReturn (information );
226
211
final String expiredUrl = "/expired" ;
227
- ConcurrentSessionFilter filter = new ConcurrentSessionFilter (registry , expiredUrl + "will-be-overrridden" ) {
212
+ ConcurrentSessionFilter filter = new ConcurrentSessionFilter (mockSessionRegistry () , expiredUrl + "will-be-overrridden" ) {
228
213
@ Override
229
214
protected String determineExpiredUrl (HttpServletRequest request , SessionInformation info ) {
230
215
return expiredUrl ;
@@ -241,12 +226,7 @@ public void doFilterWhenNoExpiredUrlThenResponseWritten() throws Exception {
241
226
MockHttpSession session = new MockHttpSession ();
242
227
request .setSession (session );
243
228
MockHttpServletResponse response = new MockHttpServletResponse ();
244
- SessionRegistry registry = mock (SessionRegistry .class );
245
- SessionInformation information = new SessionInformation ("user" , "sessionId" ,
246
- new Date (System .currentTimeMillis () - 1000 ));
247
- information .expireNow ();
248
- given (registry .getSessionInformation (anyString ())).willReturn (information );
249
- ConcurrentSessionFilter filter = new ConcurrentSessionFilter (registry );
229
+ ConcurrentSessionFilter filter = new ConcurrentSessionFilter (mockSessionRegistry ());
250
230
filter .doFilter (request , response , new MockFilterChain ());
251
231
assertThat (response .getContentAsString ()).contains (
252
232
"This session has been expired (possibly due to multiple concurrent logins being attempted as the same user)." );
@@ -259,12 +239,7 @@ public void doFilterWhenCustomLogoutHandlersThenHandlersUsed() throws Exception
259
239
MockHttpSession session = new MockHttpSession ();
260
240
request .setSession (session );
261
241
MockHttpServletResponse response = new MockHttpServletResponse ();
262
- SessionRegistry registry = mock (SessionRegistry .class );
263
- SessionInformation information = new SessionInformation ("user" , "sessionId" ,
264
- new Date (System .currentTimeMillis () - 1000 ));
265
- information .expireNow ();
266
- given (registry .getSessionInformation (anyString ())).willReturn (information );
267
- ConcurrentSessionFilter filter = new ConcurrentSessionFilter (registry );
242
+ ConcurrentSessionFilter filter = new ConcurrentSessionFilter (mockSessionRegistry ());
268
243
filter .setLogoutHandlers (new LogoutHandler [] { handler });
269
244
filter .doFilter (request , response , new MockFilterChain ());
270
245
verify (handler ).logout (eq (request ), eq (response ), any ());
@@ -276,12 +251,7 @@ public void doFilterWhenCustomSecurityContextHolderStrategyThenHandlersUsed() th
276
251
MockHttpSession session = new MockHttpSession ();
277
252
request .setSession (session );
278
253
MockHttpServletResponse response = new MockHttpServletResponse ();
279
- SessionRegistry registry = mock (SessionRegistry .class );
280
- SessionInformation information = new SessionInformation ("user" , "sessionId" ,
281
- new Date (System .currentTimeMillis () - 1000 ));
282
- information .expireNow ();
283
- given (registry .getSessionInformation (anyString ())).willReturn (information );
284
- ConcurrentSessionFilter filter = new ConcurrentSessionFilter (registry );
254
+ ConcurrentSessionFilter filter = new ConcurrentSessionFilter (mockSessionRegistry ());
285
255
SecurityContextHolderStrategy securityContextHolderStrategy = spy (
286
256
new MockSecurityContextHolderStrategy (new TestingAuthenticationToken ("user" , "password" )));
287
257
filter .setSecurityContextHolderStrategy (securityContextHolderStrategy );
@@ -300,5 +270,12 @@ public void setLogoutHandlersWhenEmptyThenThrowsException() {
300
270
ConcurrentSessionFilter filter = new ConcurrentSessionFilter (new SessionRegistryImpl ());
301
271
assertThatIllegalArgumentException ().isThrownBy (() -> filter .setLogoutHandlers (new LogoutHandler [0 ]));
302
272
}
303
-
273
+ private SessionRegistry mockSessionRegistry (){
274
+ SessionRegistry registry = mock (SessionRegistry .class );
275
+ SessionInformation information = new SessionInformation ("user" , "sessionId" ,
276
+ new Date (System .currentTimeMillis () - 1000 ));
277
+ information .expireNow ();
278
+ given (registry .getSessionInformation (anyString ())).willReturn (information );
279
+ return registry ;
280
+ }
304
281
}
0 commit comments