Anonymous Access to Screens

By default, only the login screen is available to the anonymous (not authenticated) session. By creating the new login screen, you can add any information on it or even add the WorkArea component and be able to open inside it other screens available to the anonymous user. But as soon as the user logs in, all screens opened in the anonymous mode will be closed.

Sometimes it is necessary to provide some application screens regardless of whether the user is authenticated in the system or not. Consider the following requirements:

  • When users open the application, they see the main screen with the side menu available for anonymous users.

  • There is an Info screen with some publicly available information. By clicking the Show the Login Screen button, users can go to the login screen and continue working with the rest of the system as authenticated users.

  • Users can open the Info screen both from the main menu and by entering a URL in the web browser.

Implementation steps are described below.

  1. Create the new main screen using the Main screen with side menu template.

  2. Create your screen, for example, MyAnonymousScreen, with some information for anonymous users and a button that opens the login screen:

    my-anonymous-screen.xml
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <window xmlns="http://jmix.io/schema/ui/window"
            caption="Info">
        <layout>
            <vbox spacing="true">
                <label value="Some information for anonymous users" stylename="h1"/>
                <button id="showLoginScreenBtn" caption="Show the Login Screen"/>
            </vbox>
        </layout>
    </window>

    Annotate the controller with @Route to be able to open it using a link:

    MyAnonymousScreen.java
    @UiController("MyAnonymousScreen")
    @UiDescriptor("my-anonymous-screen.xml")
    @Route(path = "anonymous") (1)
    public class MyAnonymousScreen extends Screen {
        @Autowired
        private Screens screens;
    
        @Autowired
        private UiProperties uiProperties;
    
        @Subscribe("showLoginScreenBtn")
        protected void onShowLoginScreenBtnClick(Button.ClickEvent event) {
            String loginScreenId = uiProperties.getLoginScreenId();
            Screen loginScreen = screens.create(loginScreenId, OpenMode.ROOT);
            loginScreen.show();
        }
    }
    1 Specifies the screen’s address. When the screen is opened in the top-level window (as root), its address will be http://localhost:8080/#anonymous.
  3. Define the new menu item under the Application menu:

    menu.xml
    <item id="anonymousScreen"
          screen="MyAnonymousScreen"
          caption="Info"/>
  4. Create the AnonymousRole resource role and allow access to MyAnonymousScreen and MainScreenSideMenu:

    @ResourceRole(name = "AnonymousRole", code = AnonymousRole.CODE)
    public interface AnonymousRole {
    
        String CODE = "anonymous-role";
    
        @MenuPolicy(menuIds = {"anonymousScreen"})
        @ScreenPolicy(screenIds = {"MainScreenSideMenu", "MyAnonymousScreen"})
        void screens();
    
        @MenuPolicy(menuIds = {"application"})
        void commonMenus();
    }
  5. Grant AnonymousRole to the anonymous user in DatabaseUserRepository:

    @Override
    protected void initAnonymousUser(User anonymousUser) {
        Collection<GrantedAuthority> authorities = getGrantedAuthoritiesBuilder()
                .addResourceRole(AnonymousRole.CODE)
                .build();
        anonymousUser.setAuthorities(authorities);
    }
  6. In the application.properties file enable anonymous access and specify which screen appears for the anonymous user:

    # enable anonymous access
    jmix.ui.allow-anonymous-access=true
    
    # initial screen for anonymous user
    jmix.ui.initial-screen-id=MainScreenSideMenu

    Keep in mind that you’ll have two main screens in your app. The default one will be used for authenticated users and the anonymous one for anonymous:

    jmix.ui.main-screen-id=MainScreen
    jmix.ui.initial-screen-id=MainScreenSideMenu
  7. Launch the application and make sure that the screen you created opens from the main menu and at /#anonymous.