Results 1 to 3 of 3

Thread: ContextHolder.getContext() return null in some cases

  1. #1
    Join Date
    Feb 2005
    Posts
    7

    Default ContextHolder.getContext() return null in some cases

    I can't log user manually (after successful registration) becouse ContextHolder.getContext(); return null.

    This is fragment of my spring Controller:
    Code:
            try { 
                UsernamePasswordAuthenticationToken upat = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword()); 
                Authentication auth = authenticationManager.authenticate(upat);
                SecureContext c = (SecureContext) ContextHolder.getContext(); 
                c.setAuthentication(auth);  //NullPointerException here
            } catch (AuthenticationException e) { 
                log.info("Authentication request failed: " + e); 
            }
    Thanks for help,
    Arthur

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

    Default

    In 0.7.0 the HttpSessionIntegrationFilter has a secureContext property. That is used to create a non-null SecureContext that is put into the ContextHolder. You must check ContextHolder returns a non-null Context before calling it, as a null Context should be expected if the principal hasn't been authenticated.

    In 0.8.0 (currently in CVS) the HttpSessionIntegrationFilter is removed, and replaced by HttpSessionContextIntegrationFilter. This latter class is a great improvement on the previous approach. It guarantees to setup a non-null Context every request, irrespective of whether the principal has logged in. This lets you use the Context for functions unrelated to security, such as storing the user's preferred locale.

    I'd try out CVS, as it will be released in a few days anyway, and it will make your controller much simpler. Indeed your posted code block will always work, as the Context is never null. There's also a new static method you might find more convenient:

    Code:
    SecureContextUtils.getSecureContext()

  3. #3
    Join Date
    Feb 2005
    Posts
    7

    Default

    Thanks for help. I create new SecureContext if is it null and it works great. E.g.:

    Code:
           try { 
                UsernamePasswordAuthenticationToken upat = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword()); 
                Authentication auth = authenticationManager.authenticate(upat);
                if (ContextHolder.getContext() == null || !(ContextHolder.getContext() instanceof SecureContext)) {
                    try {
                        SecureContext sc = new SecureContextImpl() 
                        sc.setAuthentication(auth);
                        ContextHolder.setContext( sc );
                        
                        log.info("User " + user + " was loged in successfully");
                    } catch (Exception e) {
                        log.error("Can't login user " + user + " after successful registration: " + e);
                    }
                } else {
                      ((SecureContext) ContextHolder.getContext()).setAuthentication(auth);
                }
    Later that day I'll try new version from cvs.

    Once again thanks for help,
    Arthur

Similar Threads

  1. Replies: 2
    Last Post: Oct 17th, 2005, 08:41 PM
  2. Replies: 4
    Last Post: Sep 27th, 2005, 11:31 PM
  3. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  4. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  5. Strange Data Access Error
    By webifyit in forum Data
    Replies: 2
    Last Post: Dec 28th, 2004, 11:06 AM

Posting Permissions

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