Access control

    The application we’ve been building will inevitably need some administrative tools. Creation and deletion of users, for example, is generally something that we’d like to do during runtime. Let’s create a simple View for creating a new user:

    Java

    checks the Views for a specific annotation, javax.annotation.security.RolesAllowed. You can get access to it by adding the following dependency to your pom.xml:

    XML

    1. <dependency>
    2. <groupId>javax.annotation</groupId>
    3. <artifactId>javax.annotation-api</artifactId>
    4. <version>1.2-b01</version>
    5. </dependency>

    Java

    1. @CDIView
    2. @RolesAllowed({ "admin" })
    3. public class CreateUserView extends CustomComponent implements View {

    To add access control to our application we’ll need to have a concrete implementation of the AccessControl abstract class. Vaadin CDI comes bundled with a simple JAAS implementation, but configuring a JAAS security domain is outside the scope of this tutorial. Instead we’ll opt for a simpler implementation.

    We’ll go ahead and alter our UserInfo class to include hold roles.

    Let’s extend AccessControl and use our freshly modified UserInfo in it.

    Java

    1. package com.vaadin.cdi.tutorial;
    2. import javax.enterprise.inject.Alternative;
    3. import javax.inject.Inject;
    4. import com.vaadin.cdi.access.AccessControl;
    5. @Alternative
    6. public class CustomAccessControl extends AccessControl {
    7. @Inject
    8. private UserInfo userInfo;
    9. @Override
    10. public boolean isUserSignedIn() {
    11. return userInfo.getUser() != null;
    12. }
    13. @Override
    14. public boolean isUserInRole(String role) {
    15. if (isUserSignedIn()) {
    16. for (String userRole : userInfo.getRoles()) {
    17. if (role.equals(userRole)) {
    18. return true;
    19. }
    20. }
    21. }
    22. return false;
    23. }
    24. @Override
    25. public String getPrincipalName() {
    26. return userInfo.getUser().getUsername();
    27. return null;
    28. }
    29. }

    Note the @Alternative annotation. The JAAS implementation is set as the default, and we can’t have multiple default implementations. We’ll have to add our custom implementation to the beans.xml:

    XML

    1. <beans>
    2. <alternatives>
    3. <class>com.vaadin.cdi.tutorial.UserGreetingImpl</class>
    4. <class>com.vaadin.cdi.tutorial.CustomAccessControl</class>
    5. </alternatives>
    6. <decorators>
    7. <class>com.vaadin.cdi.tutorial.NavigationLogDecorator</class>
    8. </decorators>
    9. </beans>

    Now let’s add a button to navigate to this view.

    ChatView:

    Java

    Java

    1. package com.vaadin.cdi.tutorial;
    2. import javax.inject.Inject;
    3. import com.vaadin.cdi.access.AccessControl;
    4. import com.vaadin.navigator.View;
    5. import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
    6. import com.vaadin.ui.Button;
    7. import com.vaadin.ui.Button.ClickEvent;
    8. import com.vaadin.ui.Button.ClickListener;
    9. import com.vaadin.ui.CustomComponent;
    10. import com.vaadin.ui.Label;
    11. import com.vaadin.ui.VerticalLayout;
    12. public class ErrorView extends CustomComponent implements View {
    13. @Inject
    14. private AccessControl accessControl;
    15. @Inject
    16. private javax.enterprise.event.Event<NavigationEvent> navigationEvent;
    17. @Override
    18. public void enter(ViewChangeEvent event) {
    19. VerticalLayout layout = new VerticalLayout();
    20. layout.setSizeFull();
    21. layout.setMargin(true);
    22. layout.setSpacing(true);
    23. layout.addComponent(new Label(
    24. "Unfortunately, the page you've requested does not exists."));
    25. layout.addComponent(createChatButton());
    26. } else {
    27. layout.addComponent(createLoginButton());
    28. }
    29. setCompositionRoot(layout);
    30. }
    31. private Button createLoginButton() {
    32. Button button = new Button("To login page");
    33. button.addClickListener(new ClickListener() {
    34. @Override
    35. public void buttonClick(ClickEvent event) {
    36. navigationEvent.fire(new NavigationEvent("login"));
    37. }
    38. });
    39. return button;
    40. }
    41. private Button createChatButton() {
    42. Button button = new Button("Back to the main page");
    43. button.addClickListener(new ClickListener() {
    44. @Override
    45. public void buttonClick(ClickEvent event) {
    46. navigationEvent.fire(new NavigationEvent("chat"));
    47. }
    48. });
    49. return button;
    50. }
    51. }

    To use this we’ll modify our NavigationService to add the error view to the Navigator.

    NavigationServiceImpl:

    Java

    1. @Inject
    2. private ErrorView errorView;
    3. @PostConstruct
    4. public void initialize() {
    5. if (ui.getNavigator() == null) {
    6. Navigator navigator = new Navigator(ui, ui);
    7. navigator.addProvider(viewProvider);
    8. navigator.setErrorView(errorView);
    9. }

    We don’t really want the admin-only buttons to be visible to non-admin users. To programmatically hide them we can inject AccessControl to our view.

    ChatView:

    Java

    Some further topics

    Sometimes there’s a need for a more complex custom access control implementations. You may need to use something more than Java Strings to indicate user roles, you may want to alter access rights during runtime. For those purposes we could extend the CDIViewProvider (with either the @Specializes annotation or with a beans.xml entry) and override isUserHavingAccessToView(Bean<?> viewBean).