Project

General

Profile

Revision ece99302

IDece993022d988e180b31663463a150a0a2daee5b
Parent 5791a9f1
Child b3e242fe

Added by Andreas Kohlbecker over 3 years ago

solving spring scope related problems with the LoginView by introducing a RedirectToLoginView

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/ui/RegistrationUI.java
31 31
import com.vaadin.ui.themes.ValoTheme;
32 32

  
33 33
import eu.etaxonomy.cdm.dataInserter.RegistrationRequiredDataInserter;
34
import eu.etaxonomy.cdm.vaadin.view.LoginViewBean;
34
import eu.etaxonomy.cdm.vaadin.view.RedirectToLoginView;
35 35
import eu.etaxonomy.cdm.vaadin.view.registration.DashBoardView;
36 36
import eu.etaxonomy.cdm.vaadin.view.registration.ListViewBean;
37
import eu.etaxonomy.cdm.vaadin.view.registration.StartRegistrationView;
37
import eu.etaxonomy.cdm.vaadin.view.registration.StartRegistrationViewBean;
38 38
import eu.etaxonomy.vaadin.ui.MainMenu;
39 39
import eu.etaxonomy.vaadin.ui.UIInitializedEvent;
40 40
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
......
73 73
    SpringViewProvider viewProvider;
74 74

  
75 75
    protected void configureAccessDeniedView() {
76
        viewProvider.setAccessDeniedViewClass(LoginViewBean.class);
76
        viewProvider.setAccessDeniedViewClass(RedirectToLoginView.class);
77 77
    }
78 78
    //---------------------------------------------
79 79

  
80 80
    // public static final String INITIAL_VIEW = "workflow/edit/10";
81 81
    public static final String INITIAL_VIEW =  DashBoardView.NAME;
82
    static boolean debugMode = false;
82
    static boolean debugMode = true;
83 83

  
84 84
    /*
85 85
     * this HACKY solution forces the bean to be instantiated, TODO do it properly
......
111 111
        phycoBankLogo.addStyleName(ValoTheme.LABEL_HUGE);
112 112
        mainMenu.addMenuComponent(phycoBankLogo);
113 113

  
114
        mainMenu.addMenuItem("New", FontAwesome.EDIT, StartRegistrationView.NAME );
114
        mainMenu.addMenuItem("New", FontAwesome.EDIT, StartRegistrationViewBean.NAME );
115 115
        mainMenu.addMenuItem("Continue", FontAwesome.ARROW_RIGHT, ListViewBean.NAME + "/" + ListViewBean.OPTION_IN_PROGRESS);
116 116
        mainMenu.addMenuItem("List", FontAwesome.TASKS, ListViewBean.NAME + "/" + ListViewBean.OPTION_ALL);
117 117

  
src/main/java/eu/etaxonomy/cdm/vaadin/view/LoginPresenter.java
8 8
*/
9 9
package eu.etaxonomy.cdm.vaadin.view;
10 10

  
11
import java.util.List;
12

  
11 13
import org.apache.commons.lang.StringUtils;
12 14
import org.apache.log4j.Logger;
13 15
import org.springframework.beans.factory.annotation.Autowired;
......
21 23
import com.vaadin.spring.annotation.UIScope;
22 24

  
23 25
import eu.etaxonomy.cdm.vaadin.event.AuthenticationAttemptEvent;
24
import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
25 26
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
27
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
26 28
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
27 29

  
28 30
/**
......
56 58
    @Autowired
57 59
    protected ApplicationEventPublisher eventBus;
58 60

  
61
    private String redirectToState;
62

  
59 63
    public boolean authenticate(String userName, String password){
60 64

  
61 65
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userName, password);
......
66 70
            log.debug("user '" + userName + "' authenticated");
67 71
            currentSecurityContext().setAuthentication(authentication);
68 72
            if(NavigationManager.class.isAssignableFrom(getNavigationManager().getClass())){
69
                log.debug("reloading current view");
70
                getNavigationManager().reloadCurrentView();
71
                eventBus.publishEvent(new AuthenticationSuccessEvent(userName));
73
               /// eventBus.publishEvent(new AuthenticationSuccessEvent(userName));
74
                log.debug("redirecting to " + redirectToState);
75
                eventBus.publishEvent(new NavigationEvent(redirectToState));
72 76
            }
73 77
        }
74 78
        return false;
......
81 85
     */
82 86
    @Override
83 87
    public void handleViewEntered() {
88
        List<String> redirectToStateTokens = getNavigationManager().getCurrentViewParameters();
89
        redirectToState = String.join("/", redirectToStateTokens);
90

  
84 91
        // attempt to auto login
85 92
        if(StringUtils.isNotEmpty(System.getProperty(PROPNAME_USER)) && StringUtils.isNotEmpty(System.getProperty(PROPNAME_PASSWORD))){
86 93
            log.warn("Performing autologin with user " + System.getProperty(PROPNAME_USER));
src/main/java/eu/etaxonomy/cdm/vaadin/view/RedirectToLoginView.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.view;
10

  
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
13
import org.springframework.context.ApplicationEventPublisher;
14
import org.springframework.context.annotation.Scope;
15

  
16
import com.vaadin.navigator.Navigator;
17
import com.vaadin.navigator.View;
18
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
19
import com.vaadin.spring.annotation.SpringComponent;
20
import com.vaadin.ui.Alignment;
21
import com.vaadin.ui.Label;
22
import com.vaadin.ui.VerticalLayout;
23
import com.vaadin.ui.themes.ValoTheme;
24

  
25
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
26
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
27

  
28
/**
29
 * A {@link SpringViewProvider.setAccessDeniedViewClass(Class<? extends View> accessDeniedViewClass) accessDeniedViewClass}
30
 * can not be a view- or UI-scoped bean, since the view context is not set up at the time when the <code>accessDeniedViewClass</code>
31
 * is needed. Therefore the <code>RedirectToLoginView</code> is a scoped prototype bean which always available. Using the 'singleton'
32
 * scope should also work but is not a good idea for a login view.
33
 * The <code>RedirectToLoginView</code> redirects the request to the LoginView which then is in a correctly
34
 * set up view scope.
35

  
36
 *
37
 * @author a.kohlbecker
38
 * @since Jul 13, 2017
39
 *
40
 */
41
@SpringComponent
42
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
43
public class RedirectToLoginView extends VerticalLayout implements View {
44

  
45

  
46
    private static final long serialVersionUID = -8763747518841365925L;
47

  
48
    @Autowired
49
    NavigationManager navigationManager;
50

  
51
    @Autowired
52
    protected ApplicationEventPublisher eventBus;
53

  
54
    public RedirectToLoginView() {
55

  
56
        this.setWidth("100%");
57
        Label header = new Label("Access denied");
58
        header.setStyleName(ValoTheme.BUTTON_LARGE);
59
        header.setWidthUndefined();
60

  
61
        addComponent(header);
62
        setComponentAlignment(header, Alignment.MIDDLE_CENTER);
63
    }
64

  
65
    /**
66
     * {@inheritDoc}
67
     */
68
    @Override
69
    public void enter(ViewChangeEvent event) {
70

  
71
        String currentState = ((Navigator)navigationManager).getState();
72
        // redirect to the login view and pass over the current state
73
        eventBus.publishEvent(new NavigationEvent(LoginViewBean.NAME, currentState));
74
    }
75

  
76
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)