Build Secure Web Apps with Vaadin & Spring Boot


Guest post by Daniel Hügelmann

Dependable security with Spring Security

Almost all web application frameworks include their security-relevant functions and provide the respective interfaces to external security solutions. As a Java-based full-stack framework, Vaadin is compatible with the most popular Java security solutions. Worth naming here are Spring Security, Apache Shiro, and JAAS.

If you were using Vaadin and Spring boot to build web applications, then the Spring Framework’s Spring Security module would be the tool of choice. It is easily connected using Vaadin Flow’s Security Helpers. These allow view-based access control without the need of having to configure Spring Security greatly.

This allows each view to be secured separately with its security guidelines. The @DenyAll, @PermitAll, @RolesAllowed, and @AnonymousAllowed annotations are applied to the respective view classes.

For this, we require the following artifacts in the project: A log-in view, a mechanism for the user to log out, the Spring Security dependencies, a class for the security configuration (which extends VaadinWebSecurity), and one of the following annotations on each of our view classes: @PermitAll, @RolesAllowed, @AnonymousAllowed.

Naturally with a log-in view

The log-in view is our web application’s first bastion. This is where our users first authenticate themselves. Here we can use Vaadin’s log-in form component or implement our own log-in view. One can also work with modern authentication methods, such as the two-factor authentication offered by Google’s Authenticator Library. The following listing shows how a simple log-in view can be implemented with Vaadin’s component and i18n.

@PageTitle("JTAF - Login")
public class LoginView extends LoginOverlay implements AfterNavigationObserver, BeforeEnterObserver {

    private static final long serialVersionUID = 1L;

    public LoginView() {
        var i18n = LoginI18n.createDefault();

        i18n.setHeader(new LoginI18n.Header());
        i18n.getHeader().setDescription("Track and Field");

        i18n.setForm(new LoginI18n.Form());





    public void beforeEnter(BeforeEnterEvent event) {
        if (SecurityContext.isUserLoggedIn()) {
        } else {

    public void afterNavigation(AfterNavigationEvent event) {


Spring Boot security dependencies

To use the Spring Security Module, we have to add the Spring Boot Starter security dependency to our project. For this we use the groupID org.springframework.boot and the artifactID spring-boot-starter-security:



Our user should not just be logged out via time-out; he should also be able to log-out manually. For this, we can create a simple log-out button in our Header: 

public class MainLayout extends AppLayout {

    private SecurityService securityService;

    public MainLayout(@Autowired SecurityService securityService) {
        this.securityService = securityService;

        H1 logo = new H1("Unsere Web Applikation");
        HorizontalLayout header;
        if (securityService.getAuthenticatedUser() != null) {
            Button logout = new Button("Log-Out", click ->
            header = new HorizontalLayout(logo, logout);
        } else {
            header = new HorizontalLayout(logo);


This log-out button is then equipped with the Spring Security API’s functionality by implementing a SecurityService class:

public class SecurityService {

    private static final String LOGOUT_SUCCESS_URL = "/";

    public UserDetails getAuthenticatedUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        Object principal = context.getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            return (UserDetails) context.getAuthentication().getPrincipal();
        // Anonymous or no authentication.
        return null;

    public void logout() {
        SecurityContextLogoutHandler logoutHandler = new SecurityContextLogoutHandler();
                VaadinServletRequest.getCurrent().getHttpServletRequest(), null,

The Security Configuration

We now need to extend the VaadinWebSecurity class. For this, we create the class SecurityConfiguration:

public class SecurityConfiguration
                extends VaadinWebSecurity { 

    protected void configure(HttpSecurity http) throws Exception {


        setLoginView(http, LoginView.class); 

    public void configure(WebSecurity web) throws Exception {

    public UserDetailsManager userDetailsService() {
        UserDetails user =
        UserDetails admin =
        return new InMemoryUserDetailsManager(user, admin);

Our two hard-coded users, NormalUser and Administrator, only serve the example. In the real thing, a UserDetailsManager should, of course, look different.

Annotations for view classes

The following annotations restrict access to our views. If no annotation is assigned, then @DenyAll will automatically apply, which completely restricts access to the view for all users.

@PermitAll allow access to a single view for all (authenticated) users:

@Route(value = "private", layout = MainView.class)
@PageTitle("A private View")
public class PrivateView extends VerticalLayout {
    // ...

@RolesAllowed allows access to a view for all defined user roles:

@Route(value = "admin", layout = MainView.class)
@PageTitle("A view only for admins")
public class AdminView extends VerticalLayout {
    // ...

Note that the user roles are also case-sensitive!

@AnonymousAllowed allows access to a view for everyone, even for not authenticated users. This annotation should be used carefully and only for public views:

@Route(value = "", layout = MainView.class)
@PageTitle("A public view")
public class PublicView extends VerticalLayout {
    // ...

When using these annotations, one should remember that they are inheritable. This means that they are passed on from a parent class to a child class. One can, however, overwrite this by annotating the child’s class. Here the following applies: @DenyAll overwrites all other annotations, @AnonymousAllowed overwrites @RolesAllowed and @PermitAll, while @RolesAllowed overwrites @PermitAll.


These steps give our users the means to log in and -out again. We also learned how to specifically restrict the access rights for each of our views.

Related Posts

Simon Martinelli
Follow Me