Search code examples
spring-securityvaadin10vaadin-flow

Vaadin 10 ( - Only Java - ) & Spring Security Login Form


i am trying to use V10 & Spring Sec. based on Bakery Demo.

But Demo has Polymer based login button and it has action method on its html form.

i implemented login just using Java. But the login button does not trigger anything.

FormLayout formLayout = new FormLayout();
    TextField txtUsername = new TextField();
    txtUsername.setPlaceholder("Kullanıcı Adı");

    PasswordField passwordField = new PasswordField();
    passwordField.setPlaceholder("Parola");

    Button btnLogin = new Button("Giriş");

    btnLogin.addClickListener(buttonClickEvent -> {
        System.out.println(txtUsername.getValue() + " " + passwordField.getValue());
    });

    formLayout.add(txtUsername, passwordField, btnLogin);
    add(formLayout);

any working example with Spring sec. and pure java.


Solution

  • it's hard to tell what is wrong just by the code snippet you posted. It looks valid to me. Since Vaadin 13 there are dedicated login components you could use that simplify the task if you want to have a form-based login.

    @Tag("sa-login-view")
    @Route(value = LoginView.ROUTE)
    @PageTitle("Login")
    public class LoginView extends VerticalLayout {
            public static final String ROUTE = "login";
    
            private LoginOverlay login = new LoginOverlay();
    
            public LoginView(){
                login.setAction("login");
                login.setOpened(true);
                login.setTitle("Spring Secured Vaadin");
                login.setDescription("Login Overlay Example");
                getElement().appendChild(login.getElement());
            }
    }
    

    The login.setAction() method defines the endpoint the final form data is send to. You will have to configure Spring Security accordingly.

    This code snippet is part of our Spring Security tutorial: https://vaadin.com/tutorials/securing-your-app-with-spring-security/form-based

    Might be worth checking it for you.

    Cheers, Paul