Search code examples
casspring-webflow

inject model data into spring webflow in cas


I am upgrading a CAS 4 to a CAS 6. I have done several Spring Boot 2 apps, so I know what I am doing there. I can even do some webflow, but only from scratch.

The documentation clearly states not to mess with the base webflow xml, and to "inject" your own services.

How does one "inject" a service? I really just need to add a message of the day to the login page.

Does anyone have an example of something this simple?


Solution

  • Find below my approach, tested on a cas-maven-overlay installation with cas version at 5.3.x. Some things maybe different on cas 6 branch but I assume the main idea remains.

    First, we should create an Action class that will be injected in the login flow and will add the desired message in the flow scope in order to be available at the template(view).

    public class DailyMessageAction extends AbstractAction{
        @Override
        protected Event doExecute(RequestContext context) throws Exception {
            context.getFlowScope().asMap().put("dailyMessage", "YOUR_AWESOME_MESSAGE");
            return success();
        }       
    }
    

    Then create a WebflowConfigurer class and inject our newly created DailyMessageAction in the actions list(see doInitialize method).

    public class DailyMessageWebflowConfigurer extends AbstractCasWebflowConfigurer{
        final Action dailyMessageAction;
    
        public DailyMessageWebflowConfigurer(FlowBuilderServices flowBuilderServices,
                                   FlowDefinitionRegistry flowDefinitionRegistry,
                                   ApplicationContext applicationContext,
                                   CasConfigurationProperties casProperties,Action dailyMessageAction){
            super(flowBuilderServices, flowDefinitionRegistry, applicationContext, casProperties);
            this.dailyMessageAction = dailyMessageAction;
        }
    
        @Override
        protected void doInitialize() {
            final Flow flow = super.getLoginFlow();
            flow.getStartActionList().add(dailyMessageAction);
        }
    }
    

    After that we should inject DailyMessageWebflowConfigurer in cas runtime. This is achieved by creating a configuration class and inject our configurer.

    @Configuration
    public class CustomWebflowConfiguration {
    
    @Autowired
    private CasConfigurationProperties casProperties;
    
    @Autowired
    @Qualifier("loginFlowRegistry")
    private FlowDefinitionRegistry loginFlowDefinitionRegistry;
    
    @Autowired
    private ApplicationContext applicationContext;
    
    @Autowired
    private FlowBuilderServices flowBuilderServices; 
        @RefreshScope
        @ConditionalOnMissingBean(name = "dailyMessageAction")
        @Bean
        public Action dailyMessageAction(){
            return new DailyMessageAction();
        }
    
        @ConditionalOnMissingBean(name = "dailyMessageWebflowConfigurer")
        @Bean
        @RefreshScope
        public CasWebflowConfigurer dailyMessageWebflowConfigurer(){
            final DailyMessageWebflowConfigurer w = new DailyMessageWebflowConfigurer(flowBuilderServices,
                    loginFlowDefinitionRegistry,
                    applicationContext,
                    casProperties, 
                    dailyMessageAction());
            w.initialize();
            return w;
        }
    }
    

    Include our CustomWebflowConfigurationclass in META-INF/spring.factories:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=your_package.CustomWebflowConfiguration
    

    The final step is to present the added message in the view. Achieved by adding this line

    <div th:utext="${dailyMessage}"></div>
    

    in the templates/casLoginView.html file.