Announcement Announcement Module
Collapse
No announcement yet.
ContextHolder.getContext() return null in some cases Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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()

    Comment


    • #3
      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

      Comment

      Working...
      X