title: Spring Security: Authentication returns null
body: <p>I have configured a Spring bean as follows to return a SecurityContext: </p>
  
  <pre><code>&lt;bean id="securityContext" class="org.springframework.security.context.SecurityContextHolder"
      factory-method="getContext"&gt;
  &lt;/bean&gt;  
  </code></pre>
  
  <p>When I use this bean the Authentication object returns null.</p>
  
  <pre><code>Authentication authentication = securityContext.getAuthentication();
  GrantedAuthority[] authorities = authentication.getAuthorities();
  </code></pre>
  
  <p>The second line above causes an NPE. Which seems odd to me, as the following code returns the authorities as expected:</p>
  
  <pre><code>GrantedAuthority[] authorities =   
  SecurityContextHolder.getContext().getAuthentication().getAuthorities();
  </code></pre>
  
  <p>Basically I'm trying to eliminate the static call to SecurityContextHolder.getContext() to make my code more testable. </p>
  
  <p>Any thoughts on how to remedy this? Why is the SecurityContext returned by Spring not able to return the authorities while a static call from within my own code can?</p>
  
  <p>FYI I am executing the code from within a Struts 2 Action.</p>