Results 1 to 8 of 8

Thread: Putting Objects in the Session when Authentication Successfu

  1. #1
    Join Date
    Sep 2004
    Location
    Manchester, UK
    Posts
    16

    Default Putting Objects in the Session when Authentication Successfu

    Hi,

    I have replaced my custom security in my web app with Acegi, and the authentication and security work a treat.

    However...

    When I had my manual solution, I was putting a collection of Integer objects in to the session (league Ids of which they are a member) after authentication. I could then display these in a drop down list on my web pages and let the user select a different league table to look at.

    Now when authentication is successful Acegi put an object in to the session, but how can I?

    What is the Acegi way :?:

    Martin

  2. #2

    Default

    I'm not going to be able to help you, but I am wanting to do some session stuff. I was wondering how you are placing/retrieving information into/from the session.

    The application I'm working on sends out an email to vendor. I have a domain object called Purchase which has a Vendor object and has a collection of items. Could you suggest how I might place this information into the session?

    thanks in advance,
    Mike

  3. #3
    Join Date
    Mar 2005
    Location
    Atlanta, GA, USA
    Posts
    19

    Default

    Subclass User, or write your own principal class that implements UserDetails, and include your collection of league IDs as a property of User. Then you can use something like
    Code:
    Set leagues = request.getUserPrincipal().getLeagues();
    David Carter

  4. #4
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    It's odd to place a collection of items into the session. You'd generally be populating that into a model. If using Spring MVC, you'd put it into the ModelAndView returned by your controller.

    If you wanted to store leagus against the user, you'd probably make a LeagueGrantedAuthority that implements GrantedAuthority. In turn your AuthenticationDao would add the LeagueGrantedAuthority to UserDetails. You can then access them via SecurityContextHolder.getContext().getAuthenticati on().getAuthorities().
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

  5. #5
    Join Date
    Sep 2005
    Location
    Manila. Philippines
    Posts
    17

    Default

    If another property is added to a subclass of UserDetails, userId, for instance, how do you access it?
    SecurityContextHolder.getContext().getAuthenticati on().get???

  6. #6
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Quote Originally Posted by annbc
    SecurityContextHolder.getContext().getAuthenticati on().get???
    ((CustomUserDetails)SecurityContextHolder.getConte xt().getAuthentication().getPrincipal()).getMySpec ialProperty();
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

  7. #7
    Join Date
    Oct 2004
    Location
    Germany, Mainz
    Posts
    19

    Default

    Hey Ben,
    I got it the same way you proposed it, but I always get an ClassCastException.
    What is wrong?
    My Class:
    Code:
    package clara.bo.model;
    
    import net.sf.acegisecurity.GrantedAuthority;
    import net.sf.acegisecurity.UserDetails;
    
    /**
     * 
     */
    
    /**
     * @author Johannes.Hiemer
     *
     */
    
    public class AuthenticatedUser implements UserDetails {
    
    	public User currentUser;
    
    	/**
    	 * @return Returns the currentUser.
    	 */
    	public User getCurrentUser() {
    		return currentUser;
    	}
    
    	/**
    	 * @param currentUser The currentUser to set.
    	 */
    	public void setCurrentUser(User currentUser) {
    		this.currentUser = currentUser;
    	}
    
    	public boolean isAccountNonExpired() {
    		return false;
    	}
    
    	public boolean isAccountNonLocked() {
    		return false;
    	}
    
    	public GrantedAuthority[] getAuthorities() {
    		return null;
    	}
    
    	public boolean isCredentialsNonExpired() {
    		return false;
    	}
    
    	public boolean isEnabled() {
    		return false;
    	}
    
    	public String getPassword() {
    		return null;
    	}
    
    	public String getUsername() {
    		return null;
    	}
    	
    
    }
    Code:
    ((AuthenticatedUser)acegiContext.getAuthentication().getPrincipal()).getCurrentUser();
    Thanks a lot

    Regards Johannes

  8. #8
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Is your AuthenticationDao returning an instance of your AuthenticatedUser? Try doing a getClass().getName() instead of calling your custom property - it shoud be AuthenticatedUser if your configuration is correct.
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

Similar Threads

  1. Hibernate Long Session Per Flow?
    By akw in forum Web Flow
    Replies: 21
    Last Post: Dec 12th, 2005, 08:06 PM
  2. Replies: 2
    Last Post: Oct 13th, 2005, 02:47 PM
  3. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  4. Replies: 16
    Last Post: Apr 12th, 2005, 03:13 AM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

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