Login Form and URL

I am using JavaEE with Wildfly 11 and Vaadin 8.
I have a Application with a LoginView and a lot of SubViews
When i open the Application with the Standard-URL, all is ik
localhost:8080/App
I get then the LoginView.

But when i give the URL localhost:8080/App/#!Person
then i get the View Person, without Login.

How can i disallow this?
I want that with correct username and password there is no possibility to reach another side of the Application.

My MainView is:

@CDIUI("")
public class MainView extends UI {
	

	@Inject
    CDIViewProvider viewProvider;
	
	private Navigator navigator;

	public static final String PERSON_VIEW = "Person";
	public static final String MASTER_DETAIL_VIEW = "MasterDetail";
	public static final String ACCOUNT_VIEW = "Account";
	public static final String HELP_VIEW = "Help";

	@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout mainLayout = new VerticalLayout();
		final CssLayout menuView = new CssLayout();
		final CssLayout contentView = new CssLayout();

		mainLayout.addComponent(menuView);
		mainLayout.addComponent(conten1View);
		mainLayout.setMargin(true);
		mainLayout.setSpacing(true);
		setContent(mainLayout);

		navigator = new Navigator(this, contentView);
		navigator.addProvider(viewProvider);

		LoginView loginView = new LoginView();
		navigator.addView("login", loginView);
		navigator.setErrorView(loginView);
	}
}

One typical approach is only instantiating the Navigator once the login has successfully completed.

-Olli

The problem is, that i navigate from my MaiView directly to the LoginView with the Navigator.
How can i change it to instantiate the Navigator after successfully login?
Because I must instantiate the navigator to show the LoginView.

Something like this:

	public class MyUI extends UI {
		protected void init(VaadinRequest request){
			setContent(new LoginView());
		}
		public void loginSuccessful(User user){
			setContent(new MainLayout(user));
		}
}

Of course, you can also check that the user is logged in (from the Session, typically) in the enter method of your Views and navigate away to the Login view if needed, BUT you need to be very careful that you do this in every enter method.

-Olli

Thanks for your answer
i want use your solution

but how must be the sourcecode for MainLayout(user).
I tried it so:

@CDIUI("")
public class Main extends UI {
	
	@Inject
    CDIViewProvider viewProvider;
	
	private Navigator navigator;

	public static final String PERSON_VIEW = "Person";
	public static final String MASTER_DETAIL_VIEW = "MasterDetail";
	public static final String ACCOUNT_VIEW = "Account";
	public static final String HELP_VIEW = "Help";
	
	private LoginView loginView;
	private Account loginUser;
	private MainLayout mainLayout;

	@Override
	protected void init(VaadinRequest request) {
		loginView = new LoginView();
		setContent(new LoginView());
	}
	
	
	public void loginSuccessful(Account user){
		setContent(new MainLayout());
		setLoginUser(user);
		navigator = new Navigator(this, mainLayout.getContentView());
		navigator.addProvider(viewProvider);


		loginView = new LoginView();
		navigator.addView(PERSON_VIEW, PersonView.class);
		navigator.setErrorView(loginView);	
	}
	
	public Account getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(Account loginUser) {
		this.loginUser = loginUser;
	}

}
public class MainLayout extends CustomComponent {
	
	final private CssLayout menuView;

	final private CssLayout contentView;
	
	public MainLayout() {
		final VerticalLayout mainLayout = new VerticalLayout();
		menuView = new CssLayout();
		contentView = new CssLayout();

		mainLayout.addComponent(menuView);
		mainLayout.addComponent(contentView);
		mainLayout.setMargin(true);
		mainLayout.setSpacing(true);

		setSizeUndefined();
		setCompositionRoot(mainLayout);
	}
	
	public CssLayout getContentView() {
		return contentView;
	}
}
@PushStateNavigation
@CDIView("")
public class LoginView extends VerticalLayout implements View {

	@Inject
	AccountService accountService;

	@Inject
	PasswordService passwordService;

	TextField username;
	TextField password;
	private Main main;

	public LoginView() {
		setSpacing(true);

		Label label = new Label("Enter your information below to log in.");
		username = new TextField("Username");
		password = new TextField("Password");

		addComponent(label);
		addComponent(username);
		addComponent(password);
		addComponent(loginButton);
	}

	public void enter(ViewChangeEvent event) {
		Notification.show("Welcome! Please log in.");
	}

	Button loginButton = new Button("Login", e -> {

		if (this.validate(username.getValue(), password.getValue())) {
			main = new Main();
			main.loginSuccessful(accountService.getAccountByName(username.getValue()));
//			getUI().getNavigator().navigateTo(Main.PERSON_VIEW);
		} else {
		}

	});

	private boolean validate(String username, String password) {
		Account account = new Account();

		try {
			account = accountService.getAccountByName(username);
		} catch (Exception e) {
			Notification.show("Username not exist");
			return false;
		}

		if (passwordService.validatePassword(password, account.getPassword())) {
			return true;
		} else {
			Notification.show("Sorry, password not correct");
			return false;
		}
	}
}

I get the following error:

10:19:43,740 SEVERE [com.vaadin.server.DefaultErrorHandler]
 (default task-37) : java.lang.NullPointerException
	at org.app.ui.login.LoginView.lambda$new$3a0fb27$1(LoginView.java:56)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:499)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:273)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:237)
	at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1014)
	at com.vaadin.ui.Button.fireClick(Button.java:384)
	at com.vaadin.ui.Button$1.click(Button.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:155)
	at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:116)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:445)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:410)
	at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:274)
	at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:90)
	at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
	at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1601)
	at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:445)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
	at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

I think that the error is in the Class LoginView

			main = new Main();
			main.loginSuccessful(accountService.getAccountByName(username.getValue()));

accountService is null, maybe? You probably shouldn’t be creating LoginView with new, but injecting it instead.

I have made it so to simplify my code:

@CDIUI("")
public class Main extends UI {
	
	@Inject
    CDIViewProvider viewProvider;
	
	@Inject
    LoginView loginView;
	
	private Navigator navigator;

	public static final String PERSON_VIEW = "Person";
	public static final String MASTER_DETAIL_VIEW = "MasterDetail";
	public static final String ACCOUNT_VIEW = "Account";
	public static final String HELP_VIEW = "Help";
	
	private Account loginUser;
	private MainLayout mainLayout;


	@Override
	protected void init(VaadinRequest request) {
		loginView = new LoginView();
		setContent(new LoginView());
	}
	
	
	public void loginSuccessful(){
		setContent(new MainLayout());
		navigator = new Navigator(this, mainLayout.getContentView());
		navigator.addProvider(viewProvider);

		navigator.addView(PERSON_VIEW, PersonView.class);
		navigator.setErrorView(loginView);	}
	
	public Account getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(Account loginUser) {
		this.loginUser = loginUser;
	}
}
@PushStateNavigation
@CDIView("")
public class LoginView extends VerticalLayout implements View {

	@Inject
	AccountService accountService;

	@Inject
	PasswordService passwordService;

	TextField username;
	TextField password;
	private Main main;

	public LoginView() {
		setSpacing(true);

		Label label = new Label("Enter your information below to log in.");
		username = new TextField("Username");
		password = new TextField("Password");

		addComponent(label);
		addComponent(username);
		addComponent(password);
		addComponent(loginButton);
	}

	public void enter(ViewChangeEvent event) {
		Notification.show("Welcome! Please log in.");
	}

	Button loginButton = new Button("Login", e -> {
		main = new Main();
		main.loginSuccessful();
	});
}

But now i get a the following error in the Borwser:

ontext Path:/AppFrontend
Servlet Path:
Path Info:/
Query String:v-1526633228807
Stack Trace:
javax.servlet.ServletException: com.vaadin.server.ServiceException: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type com.vaadin.cdi.ViewScoped
	at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:447)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
	at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: com.vaadin.server.ServiceException: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type com.vaadin.cdi.ViewScoped
	at com.vaadin.server.VaadinService.handleExceptionDuringRequest(VaadinService.java:1653)
	at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1613)
	at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:445)
	... 40 more
Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type com.vaadin.cdi.ViewScoped
	at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:705)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:94)
	at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
	at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:758)
	at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:858)
	at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
	at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:358)
	at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:369)
	at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:70)
	at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
	at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72)
	at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:117)
	at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
	at org.apache.deltaspike.core.util.context.ContextualStorage.createContextualInstance(ContextualStorage.java:131)
	at org.apache.deltaspike.core.util.context.AbstractContext.get(AbstractContext.java:129)
	at com.vaadin.cdi.internal.ContextWrapper.get(ContextWrapper.java:51)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
	at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
	at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:758)
	at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:781)
	at org.jboss.weld.util.ForwardingBeanManager.getReference(ForwardingBeanManager.java:61)
	at org.jboss.weld.bean.builtin.BeanManagerProxy.getReference(BeanManagerProxy.java:85)
	at com.vaadin.cdi.CDIUIProvider.createInstance(CDIUIProvider.java:97)
	at com.vaadin.cdi.CDIUIProvider$Proxy$_$$_WeldClientProxy.createInstance(Unknown Source)
	at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:193)
	at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:76)
	at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
	at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1601)
	... 41 more