|
9 | 9 | import com.vaadin.flow.spring.SpringSecurityAutoConfiguration; |
10 | 10 | import jakarta.servlet.FilterChain; |
11 | 11 | import jakarta.servlet.http.HttpServletResponse; |
| 12 | + |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
12 | 17 | import org.junit.jupiter.api.AfterEach; |
13 | 18 | import org.junit.jupiter.api.BeforeEach; |
14 | 19 | import org.junit.jupiter.api.Test; |
|
43 | 48 | import org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter; |
44 | 49 | import org.springframework.security.web.access.ExceptionTranslationFilter; |
45 | 50 | import org.springframework.security.web.access.intercept.AuthorizationFilter; |
| 51 | +import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler; |
46 | 52 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
47 | 53 | import org.springframework.security.web.authentication.logout.LogoutFilter; |
48 | 54 | import org.springframework.security.web.authentication.logout.LogoutHandler; |
@@ -370,6 +376,110 @@ void hilla_checkAllowedRoutes() throws Exception { |
370 | 376 | } |
371 | 377 | } |
372 | 378 |
|
| 379 | + @Test |
| 380 | + void defaultSuccessUrl_withLoginView_successHandlerIsConfigured() |
| 381 | + throws Exception { |
| 382 | + http.with(configurer, |
| 383 | + c -> c.loginView("/login").defaultSuccessUrl("/dashboard")) |
| 384 | + .build(); |
| 385 | + |
| 386 | + var handler = http.getSharedObject( |
| 387 | + VaadinSavedRequestAwareAuthenticationSuccessHandler.class); |
| 388 | + |
| 389 | + assertThat(handler).isNotNull(); |
| 390 | + assertThat(getDefaultTargetUrl(handler)).isEqualTo("/dashboard"); |
| 391 | + assertThat(isAlwaysUseDefaultTargetUrl(handler)).isFalse(); |
| 392 | + } |
| 393 | + |
| 394 | + @Test |
| 395 | + void defaultSuccessUrl_withLoginViewClass_successHandlerIsConfigured() |
| 396 | + throws Exception { |
| 397 | + http.with(configurer, c -> c.loginView(TestLoginView.class) |
| 398 | + .defaultSuccessUrl("/home")).build(); |
| 399 | + |
| 400 | + var handler = http.getSharedObject( |
| 401 | + VaadinSavedRequestAwareAuthenticationSuccessHandler.class); |
| 402 | + |
| 403 | + assertThat(handler).isNotNull(); |
| 404 | + assertThat(getDefaultTargetUrl(handler)).isEqualTo("/home"); |
| 405 | + assertThat(isAlwaysUseDefaultTargetUrl(handler)).isFalse(); |
| 406 | + } |
| 407 | + |
| 408 | + @Test |
| 409 | + void defaultSuccessUrl_withOAuth2LoginPage_successHandlerIsConfigured() |
| 410 | + throws Exception { |
| 411 | + http.with(configurer, |
| 412 | + c -> c.oauth2LoginPage("/oauth2/authorization/google") |
| 413 | + .defaultSuccessUrl("/main")) |
| 414 | + .build(); |
| 415 | + |
| 416 | + var handler = http.getSharedObject( |
| 417 | + VaadinSavedRequestAwareAuthenticationSuccessHandler.class); |
| 418 | + |
| 419 | + assertThat(handler).isNotNull(); |
| 420 | + assertThat(getDefaultTargetUrl(handler)).isEqualTo("/main"); |
| 421 | + assertThat(isAlwaysUseDefaultTargetUrl(handler)).isFalse(); |
| 422 | + } |
| 423 | + |
| 424 | + @Test |
| 425 | + void defaultSuccessUrl_withAlwaysUseTrue_alwaysRedirectsToDefaultUrl() |
| 426 | + throws Exception { |
| 427 | + http.with(configurer, c -> c.loginView("/login") |
| 428 | + .defaultSuccessUrl("/dashboard", true)).build(); |
| 429 | + |
| 430 | + var handler = http.getSharedObject( |
| 431 | + VaadinSavedRequestAwareAuthenticationSuccessHandler.class); |
| 432 | + |
| 433 | + assertThat(handler).isNotNull(); |
| 434 | + assertThat(getDefaultTargetUrl(handler)).isEqualTo("/dashboard"); |
| 435 | + assertThat(isAlwaysUseDefaultTargetUrl(handler)).isTrue(); |
| 436 | + } |
| 437 | + |
| 438 | + @Test |
| 439 | + void defaultSuccessUrl_withAlwaysUseFalse_redirectsToSavedRequest() |
| 440 | + throws Exception { |
| 441 | + http.with(configurer, c -> c.loginView("/login") |
| 442 | + .defaultSuccessUrl("/dashboard", false)).build(); |
| 443 | + |
| 444 | + var handler = http.getSharedObject( |
| 445 | + VaadinSavedRequestAwareAuthenticationSuccessHandler.class); |
| 446 | + |
| 447 | + assertThat(handler).isNotNull(); |
| 448 | + assertThat(getDefaultTargetUrl(handler)).isEqualTo("/dashboard"); |
| 449 | + assertThat(isAlwaysUseDefaultTargetUrl(handler)).isFalse(); |
| 450 | + } |
| 451 | + |
| 452 | + @Test |
| 453 | + void defaultSuccessUrl_notSet_usesRootPath() throws Exception { |
| 454 | + http.with(configurer, c -> c.loginView("/login")).build(); |
| 455 | + |
| 456 | + var handler = http.getSharedObject( |
| 457 | + VaadinSavedRequestAwareAuthenticationSuccessHandler.class); |
| 458 | + |
| 459 | + assertThat(handler).isNotNull(); |
| 460 | + assertThat(getDefaultTargetUrl(handler)).isEqualTo("/"); |
| 461 | + assertThat(isAlwaysUseDefaultTargetUrl(handler)).isFalse(); |
| 462 | + } |
| 463 | + |
| 464 | + // Helper methods to access protected fields using reflection |
| 465 | + private String getDefaultTargetUrl( |
| 466 | + VaadinSavedRequestAwareAuthenticationSuccessHandler handler) |
| 467 | + throws Exception { |
| 468 | + Method method = AbstractAuthenticationTargetUrlRequestHandler.class |
| 469 | + .getDeclaredMethod("getDefaultTargetUrl"); |
| 470 | + method.setAccessible(true); |
| 471 | + return (String) method.invoke(handler); |
| 472 | + } |
| 473 | + |
| 474 | + private boolean isAlwaysUseDefaultTargetUrl( |
| 475 | + VaadinSavedRequestAwareAuthenticationSuccessHandler handler) |
| 476 | + throws Exception { |
| 477 | + Method method = AbstractAuthenticationTargetUrlRequestHandler.class |
| 478 | + .getDeclaredMethod("isAlwaysUseDefaultTargetUrl"); |
| 479 | + method.setAccessible(true); |
| 480 | + return (boolean) method.invoke(handler); |
| 481 | + } |
| 482 | + |
373 | 483 | @Route |
374 | 484 | static class TestLoginView extends Component { |
375 | 485 | } |
|
0 commit comments