Search code examples
javaspringjsfspring-webflow

SpringWebflow authenticating user of two types


I am making a webapp using, Spring security Spring framework. spring webflow. I want to authenticate from same login page two different type of user i.e user1 and user2. Both have different tables in database for them and different entity classes defined for them I am using jsf .xhtml page for my login page. I have used primefaces gui and with help of tabs provided login page for user1 and user2 . Here is my webflow definition file.

The problem is I want to take values for the user1 and user2 from same view-state with id "welcome" but I dont know how to accept their credential since only 1 model is allowed as i have specified model="student".

 <?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow
        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">  


    <var name="student" class="be.project.studNtwrk.domain.StudentEntity"/>
    <var name="mentor" class="be.project.studNtwrk.domain.MentorEntity"/>

    <view-state id="welcome" view="welcome.xhtml" model="student" >
        <transition on="ssignUp" to="userType"></transition>
        <transition on="msignUp" to="userType"></transition>
        <transition on="studentSignIn" to="finish">
            <evaluate expression="studentAuthenticationProviderService.processUserAuthentication(student)" />
        </transition>
        <transition on="mentorSignIn" to="finish">
            <evaluate expression="mentorAuthenticationProviderService.processUserAuthentication(student)" />
        </transition>           
    </view-state>

    <view-state id="userType" view="ChooseUser.xhtml">
        <transition on="student" to="studentsignup" />
        <transition on="mentor" to="mentorsignup" />
    </view-state>



    <view-state id="studentsignup" view="studentregistration.xhtml" model="student">
        <transition on="backToSignIn" to="welcome"></transition>
        <transition on="backToChoice" to="userType"></transition>
        <transition on="confirmSignUp" to="StudentAuthentication">
            <evaluate expression="studentService.createUser(student)"></evaluate>
        </transition>
    </view-state>

    <action-state id="StudentAuthentication">
        <evaluate expression="studentAuthenticationProviderService.processUserAuthentication(student)" />
        <transition on="yes" to="finish"></transition>
        <transition on="no" to="welcome"></transition>
    </action-state>

    <view-state id="mentorsignup" view="mentorregistration.xhtml" model="mentor">
        <transition on="backToSignIn" to="welcome"></transition>
        <transition on="backToChoice" to="userType"></transition>
        <transition on="confirmSignUp" to="MentorAuthentication">
            <evaluate expression="mentorService.createUser(mentor)"></evaluate>
        </transition>
    </view-state>

    <action-state id="MentorAuthentication">
        <evaluate expression="mentorAuthenticationProviderService.processUserAuthentication(mentor)" />
        <transition on="yes" to="finish"></transition>
        <transition on="no" to="welcome"></transition>
    </action-state>

    <end-state id="finish" view="externalRedirect:account" />

</flow>

Solution

  • What you should do is separate the "student" and "mentor" each into their own flow (files) and if there is any duplicate logic use flow inheritance and put the duplicate logic in an abstract flow that both the new "student" and "mentor" flows extend.

    http://docs.spring.io/spring-webflow/docs/current/reference/html/flow-inheritance.html