Simple Login control

Hi everybody, I’m begginer using Vaadin, I’d like to create a simple login control to my application, it does not work well, I’d like to “Login” and save in the session and navigate to “MainView”, when MainView shows, I’d like to get session value to show the user who is logged, see the code bellow




@SuppressWarnings("serial")
@Theme("agile")
public class AgileUI extends UI {

	@WebServlet(value = "/*", asyncSupported = true)
	@VaadinServletConfiguration(productionMode = false, ui = AgileUI.class, heartbeatInterval = 300)
	public static class Servlet extends VaadinServlet {
	}

	static public Navigator navigator;
	protected static final String MAINVIEW = "main";

	@Override
	protected void init(VaadinRequest request) {
		getPage().setTitle("SID ");

		Principal main = new Principal();

		// Create a navigator to control the views
		navigator = new Navigator(this, this);

		// Create and register the views
		//navigator.addView("login", new SimpleLoginView());
		navigator.addView("login", new Login());
		navigator.addView("", main);

		// AgileUI navigatorUI = new AgileUI();
		setContent(main);
		
	}

}


//Login screen
public class Login extends VerticalLayout implements View{

	@Override
	public void enter(ViewChangeEvent event) {
		final TextField username = new TextField("Username : ");
		addComponent(username);
		
		Button login = new Button("Login ");
		login.addClickListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				getSession().setAttribute("user",  username.getValue() );
				AgileUI.navigator.navigateTo("");
			}
			
		});
		addComponent(login);
		
		
	}

	
	
}




//The main screen, it will show the name od the "user"
public class Principal extends VerticalLayout implements View {

	@Override
	public void enter(ViewChangeEvent event) {

		Label labelUser = new Label();
		String username = String.valueOf(getSession().getAttribute("user"));
		labelUser.setCaption("Ola " + username);
		addComponent(labelUser);

		Button toLogin = new Button("To login");
		toLogin.addClickListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				AgileUI.navigator.navigateTo("login");

			}
		});
		
		addComponent(toLogin);
		Button logout = new Button("Logout");
		logout.addClickListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				getSession().setAttribute("user", null);
				getSession().close();
				AgileUI.navigator.navigateTo("login");
				

			}
		});
		addComponent(logout);

	}

}

The problems are…

  • I cant “logout”
  • I cant clear the information when I use navigateTo

Thanks

I am just making my login.
I use Spring Security it is the best thing to do.
See examples here (but not running on the first time!)

https://github.com/nfrankel

particularily for authentification

https://github.com/nfrankel/twaattin/tree/master/src/main/java/com/packtpub/learnvaadin

Is it compatible with Vaadin 7? I’m sorry but I’m new with vaadin and I tryied and it didn’t work, do you have any Demo runniing?

What exactly is not working? The navigation or the part with receiving the session attribute? And how is it not working (any exceptions)?

Adding spring to your vaadin project for something as simple as saving attributes in a session is a bit overkill in my opinion.
You should just make sure that you receive the same session instance when you use getSession (use VaadinSession.getCurrent() which should work if you don’t invalidate the session when changing the view).

Spring is completely compatible with Vaadin.
I succeed with internationalization, email.
next security acl, ssl, and Neo4J… but i shall see?
here is a link to learn Spring

http://www.mkyong.com/tutorials/spring-security-tutorials/