vaadin viewchangelistener change newView
-
22-12-2019 - |
Question
I try to change the newView of a navigation if the user is not authenticated and the newView is an instance of SecuredView
. My approach was to check inside the beforeViewChange
method if the constraints are given.
public boolean beforeViewChange(ViewChangeEvent event) {
if (event.getNewView().getClass().isInstance(SecuredView.class) && VaadinService.getCurrentRequest().getUserPrincipal() == null) {
event.getNavigator().navigateTo("login");
return false;
}
return true;
}
}
But that will result in a endless loop of redirection... How can i change the newView and stop the current navigation event?
Solution
1) If your View
is not 'a correct view' it will end up in an endless loop. Try this simple view:
public class SecuredView extends VerticalLayout implements View {
public SecureView() {
addComponent(new Label("Secured View"));
}
@Override
public void enter(ViewChangeEvent event) {
}
}
2) Your if statement needs to be changed:
instead of:
event.getNewView().getClass().isInstance(SecuredView.class)
write:
SecuredView.class.isInstance(event.getNewView());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow