Add a Login Screen to an Application
On this part of this tutorial, you’ll secure the Customer Relationship Management (CRM) application by setting up Spring Security and adding a login screen to limit access to logged-in users.
Login View
Start by creating a new view, LoginView, in the views package. You would do that like so:
LoginView.java,Copyto clipboard
package com.example.application.views;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.auth.AnonymousAllowed;
@Route("login") 1
@PageTitle("Login | Vaadin CRM")
@AnonymousAllowed
public class LoginView extends VerticalLayout implements BeforeEnterObserver {
private final LoginForm login = new LoginForm(); 2
public LoginView(){
addClassName("login-view");
setSizeFull(); 3
setAlignItems(Alignment.CENTER);
setJustifyContentMode(JustifyContentMode.CENTER);
login.setAction("login"); 4
add(new H1("Vaadin CRM"), login);
}
@Override
public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
// inform the user about an authentication error
if(beforeEnterEvent.getLocation() 5
.getQueryParameters()
.getParameters()
.containsKey("error")) {
login.setError(true);
}
}
}Map the view to the
"login"path.LoginViewshould encompass the entire browser window, so don’t useMainLayoutas the parent.Instantiate a
LoginFormcomponent to capture username and password.Make
LoginViewfull size and center its content — both horizontally and vertically — by callingsetAlignItems(`Alignment.CENTER)` andsetJustifyContentMode(`JustifyContentMode.CENTER)`.Set the
LoginFormaction to"login"to post the login form to Spring Security.Read query parameters and show an error if a login attempt fails.
Build the application and navigate to http://localhost:8080/login. You should see a centered login form like the one in the screenshot here:

Set Spring Security to Handle Logins
With the login screen in place, you now need to configure Spring Security to perform the authentication and to prevent unauthorized users from accessing views.
Installing Spring Security Dependencies
Add the Spring Security dependency in pom.xml like so:
pom.xml,Copyto clipboard
Confirm that the dependency is downloaded. If you’re unsure, run ./mvnw install from the command line to download the dependency.
Configure Spring Security
Create a new package, com.example.application.security for classes related to security.
Create Classes Automatically
Paste the class code into the security package to have IntelliJ automatically create the class for you.
Enable and configure Spring Security with a new class, SecurityConfig.java like this:
SecurityConfig.java,Copyto clipboard
Enable Spring Security.
Extend the
VaadinWebSecurityclass to configure Spring Security for Vaadin.Allow public access to the image directory.
Allow access to
LoginView.Configure an in-memory users for testing (see note below).
Never use hard-coded credentials in production.
Don’t use hard-coded credentials in real applications. You can change the Spring Security configuration to use an authentication provider for Lightweight Directory Access Protocol (LDAP), Java Authentication and Authorization Service (JAAS), and other real-world sources. Read more about Spring Security authentication providers.
Next, in the same package, create a service for accessing information on the logged-in user and for logging out the user.
SecurityService.java,Copyto clipboard
Finally, add @PermitAll annotations to both views to allow all logged-in users to access them.
ListView.java,Copyto clipboard
DashboardView.java,Copyto clipboard
Add a Logout Button
You can now log in to the application. The last item to add is a logout button in the application header.
In MainLayout, add a link to the header like this:
MainLayout.java,Copyto clipboard
Autowire the
SecurityServiceand save it in a field.Create a logout button that calls the
logout()method in the service.Add the button to the header layout.
Call
header.expand(logo)to make the logo take up all of the extra space in the layout. This can push the logout button to the far right.
Stop and restart the server to get the new Maven dependencies. You should now be able to log in and out of the application. Verify that you can’t access http://localhost:8080/dashboard without being logged in. You can log in with the username, user, and the password, password.

You have now built a full-stack CRM application with navigation and authentication.
Last updated
Was this helpful?