Coding authorization logic in UI components

The first approach we will discuss is including the authorization logic in the UI components themselves. This is what we have done in the example application, where we show a PrivateComponent if the user is authenticated or a PublicComponent if they are not. You can extrapolate this and use it with, for example, roles. Suppose there were two roles: employee and admin. You have to show a hypothetical AdminComponent to users with the role admin and an EmployeeComponent to users with the employee role. You can easily code a method that returns the right component according to the role as follows:

private Optional<Component> getComponent(User user) {

    if (user.getRole().equals(Role.Admin)) {
        return new ...

Get Data-Centric Applications with Vaadin 8 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.