Results 1 to 4 of 4

Thread: 2 user logins on the same page

  1. #1
    Join Date
    Oct 2006
    Location
    Switzerland
    Posts
    2

    Default 2 user logins on the same page

    Hello all,

    I am developping a spring hibernate application and I would like to know if it is possible to have 2 user login forms on the same page? We have 2 sort of users in our application and when an anonymous user go to our homepage he can do some actions or log in as role1 or role2. I know it would be more logical to have only one login form on the same page but it is my client wish.

    If yes, how could do it? Could you give me the way to follow.

    Thank your for your time.

    Steve

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Can you provide some more details about exactly what you are trying to achieve.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Well you could have 2 logins but why not just create one login form with a dropdown box to select for which role the want to login? That way you just need one form.

    However if it is really unavoidable then you good create 2 forms in 1 page and each form includes a hidden field for the desired role. In your controller do some specific action(s) with the selected role.

    HTML Code:
    <form id="form1" action="your_login_url">
     <input type="hidden" name="role" value="role1"/>
     <input type="text" name="username"/>
     <input type="password" name="password" />
    </form>
    
    <form id="form2" action="your_login_url">
     <input type="hidden" name="role" value="role2"/>
     <input type="text" name="username"/>
     <input type="password" name="password" />
    </form>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4

    Default

    Hi!

    I do not see much sense in having two login forms on one page, but if your client insists ;-).

    Assuming that each form should direct to another page, after successfull logged in, you could write your own Controller that handles the request. This Controller do the authentication for you and forward to a page you want.

    Something like this:

    Code:
    //Here you could get the values from the first or second form
    //Or checking a hidden input to deside which form was send.
    UsernamePasswordAuthenticationToken authReq= new 
    UsernamePasswordAuthenticationToken(<given username>,<given password>);
    
    HttpServletRequest request= <method to get the request>;
    authReq.setDetails(new WebAuthenticationDetails(request));
    HttpSession session= request.getSession();
    session.setAttribute(AuthenticationProcessingFilter.ACEGI_SECURITY_LAST_USERNAME_KEY, <username>);
    Authentication auth= getAuthenticationManager.authenticate(authReq);
    SecurityContext secCtx= SecurityContextHolder.getContext();
    secCtx.setAuthentication(auth);
    session.setAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY, secCtx);
    
    List grantedAuth= Arrays.asList(auth.getAuthorities);
    <Now forward based on the authorities>
    You should pack it all in a try- catch Block and handle Exception that could occur when the login fails.
    I use this controller in JSF. Their I have to do the authentication completly by myself. With this code snip you should have enough freedom to design your page as you want and only use values from it you need.

    Best regards,

    Danny

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •