Vaadin Routing/Navigation

244 views Asked by At

So I have a RequestHandler (using the VaadinServiceInitListener):

public class ApplicationServiceInitListener implements VaadinServiceInitListener {

@Override
public void serviceInit(ServiceInitEvent event) {
    event.addRequestHandler((session, request, response) -> {
        ...
    });

}

And in the RequestHandler I want to check wether a user is logged in or not. So far so good.

If the user is logged in, the request will be treated usual. If not, I want to redirect him to the Login Page. Is that possible with the request Handler?

I used the navigate-Method of the UI class like this:

UI.getCurrent().navigate("..");

But the problem is that at the point of the request, there is no UI instantiated so I will get a NPE.

1

There are 1 answers

0
cfrick On

Not the answer to your exact question (whether it could be done), but usually this goes into a BeforeEnterListener (where you can reroute), as documented in routing lifecycle docs. For an example see the code in the spring security tutorial:

void beforeEnter(BeforeEnterEvent event) {
    if (!LoginView.class.equals(event.getNavigationTarget()) && !SecurityUtils.isUserLoggedIn()) {
        event.rerouteTo(LoginView.class);
    }
}