title: HttpSession returned null object for SPRING_SECURITY_CONTEXT
body: <p>I'm trying to integrate the <a href="http://static.springsource.org/spring-security/site/extensions/saml/index.html" rel="noreferrer">Spring Saml library</a> in a sample webapplication, using Shibboleth as IDP.
  I'm able to load the login page, to login and to show the index page.</p>
  
  <p>The problem is that when I click on other links the webapp redirect me to the login page, then the IDP recognizes me and redirects to the requested page (if the network is fast it's very difficult to see this). It's like I'm not logged in for Spring security.</p>
  
  <p>I checked the log and I found this:</p>
  
  <blockquote>
    <p>org.springframework.security.web.context.HttpSessionSecurityContextRepository - HttpSession returned null object for SPRING_SECURITY_CONTEXT
    org.springframework.security.web.context.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@fde8fb. A new one will be created.</p>
  </blockquote>
  
  <p>This is the web.xml</p>
  
  <pre><code>&lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;
          /WEB-INF/spring/root-context.xml
          /WEB-INF/spring/security/securityContext.xml
      &lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Processes application requests --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  
  
  &lt;!-- Custom error pages --&gt;
  &lt;error-page&gt;
      &lt;error-code&gt;400&lt;/error-code&gt;
      &lt;location&gt;/errors/missing-en.html&lt;/location&gt;
  &lt;/error-page&gt;
  &lt;error-page&gt;
      &lt;error-code&gt;401&lt;/error-code&gt;
      &lt;location&gt;/errors/restricted-en.html&lt;/location&gt;
  &lt;/error-page&gt;
  &lt;error-page&gt;
      &lt;error-code&gt;403&lt;/error-code&gt;
      &lt;location&gt;/errors/restricted-en.html&lt;/location&gt;
  &lt;/error-page&gt;
  &lt;error-page&gt;
      &lt;error-code&gt;404&lt;/error-code&gt;
      &lt;location&gt;/errors/missing-en.html&lt;/location&gt;
  &lt;/error-page&gt;
  &lt;error-page&gt;
      &lt;error-code&gt;500&lt;/error-code&gt;
      &lt;location&gt;/errors/missing-en.html&lt;/location&gt;
  &lt;/error-page&gt;
  &lt;error-page&gt;
      &lt;error-code&gt;503&lt;/error-code&gt;
      &lt;location&gt;/errors/missing-en.html&lt;/location&gt;
  &lt;/error-page&gt;
  </code></pre>
  
  <p></p>
  
  <p>and the securityContext</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:security="http://www.springframework.org/schema/security" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/security 
          http://www.springframework.org/schema/security/spring-security-3.1.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  &lt;!-- Enable autowiring --&gt;
  &lt;context:annotation-config /&gt;
  &lt;context:component-scan base-package="org.springframework.security.saml" /&gt;
  
  &lt;security:http pattern="/logout.jsp" security="none" /&gt;
  &lt;security:http pattern="/login.jsp" security="none" /&gt;
  &lt;security:http pattern="/index.html" security="none" /&gt;
  
  &lt;security:http entry-point-ref="samlEntryPoint"&gt;
      &lt;security:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" /&gt;
      &lt;security:custom-filter before="FIRST" ref="metadataGeneratorFilter" /&gt;
      &lt;security:custom-filter after="BASIC_AUTH_FILTER" ref="samlFilter" /&gt;
  &lt;/security:http&gt;
  
  &lt;bean id="samlFilter" class="org.springframework.security.web.FilterChainProxy"&gt;
      &lt;security:filter-chain-map path-type="ant"&gt;
          &lt;security:filter-chain pattern="/saml/login/**" filters="samlEntryPoint" /&gt;
          &lt;security:filter-chain pattern="/saml/logout/**" filters="samlLogoutFilter" /&gt;
          &lt;security:filter-chain pattern="/saml/SSO/**" filters="samlWebSSOProcessingFilter" /&gt;
          &lt;security:filter-chain pattern="/saml/SSOHoK/**" filters="samlWebSSOHoKProcessingFilter" /&gt;
          &lt;security:filter-chain pattern="/saml/SingleLogout/**" filters="samlLogoutProcessingFilter" /&gt;
      &lt;/security:filter-chain-map&gt;
  &lt;/bean&gt;
  
  &lt;!-- Handler deciding where to redirect user after successful login --&gt;
  &lt;bean id="successRedirectHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt;
      &lt;property name="defaultTargetUrl" value="/" /&gt;
  &lt;/bean&gt;
  &lt;!-- Use the following for interpreting RelayState coming from unsolicited response as redirect URL: &lt;bean id="successRedirectHandler" class="org.springframework.security.saml.SAMLRelayStateSuccessHandler"&gt; 
      &lt;property name="defaultTargetUrl" value="/" /&gt; &lt;/bean&gt; --&gt;
  
  &lt;!-- Handler for successful logout --&gt;
  &lt;bean id="successLogoutHandler" class="org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler"&gt;
      &lt;property name="defaultTargetUrl" value="/logout.jsp" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Register authentication manager with SAML provider --&gt;
  &lt;security:authentication-manager alias="authenticationManager"&gt;
      &lt;security:authentication-provider ref="samlAuthenticationProvider" /&gt;
  &lt;/security:authentication-manager&gt;
  
  &lt;!-- Logger for SAML messages and events --&gt;
  &lt;bean id="samlLogger" class="org.springframework.security.saml.log.SAMLDefaultLogger" /&gt;
  
  &lt;!-- Central storage of cryptographic keys --&gt;
  &lt;bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager"&gt;
      &lt;constructor-arg value="/WEB-INF/spring/security/myKeystore.jks" /&gt;
      &lt;constructor-arg type="java.lang.String" value="betfair" /&gt;
      &lt;constructor-arg&gt;
          &lt;map&gt;
              &lt;entry key="tomcat" value="betfair" /&gt;
          &lt;/map&gt;
      &lt;/constructor-arg&gt;
      &lt;constructor-arg type="java.lang.String" value="tomcat" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Entry point to initialize authentication, default values taken from properties file --&gt;
  &lt;bean id="samlEntryPoint" class="org.springframework.security.saml.SAMLEntryPoint"&gt;
      &lt;property name="defaultProfileOptions"&gt;
          &lt;bean class="org.springframework.security.saml.websso.WebSSOProfileOptions"&gt;
              &lt;property name="includeScoping" value="false" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- IDP Discovery Service --&gt;
  &lt;bean id="samlIDPDiscovery" class="org.springframework.security.saml.SAMLDiscovery"&gt;
      &lt;property name="idpSelectionPath" value="/WEB-INF/security/idpSelection.jsp" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Filter automatically generates default SP metadata --&gt;
  &lt;bean id="metadataGeneratorFilter" class="org.springframework.security.saml.metadata.MetadataGeneratorFilter"&gt;
      &lt;constructor-arg&gt;
          &lt;bean class="org.springframework.security.saml.metadata.MetadataGenerator" /&gt;
      &lt;/constructor-arg&gt;
  &lt;/bean&gt;
  
  &lt;!-- The filter is waiting for connections on URL suffixed with filterSuffix and presents SP metadata there --&gt;
  &lt;bean id="metadataDisplayFilter" class="org.springframework.security.saml.metadata.MetadataDisplayFilter" /&gt;
  
  &lt;!-- IDP Metadata configuration - paths to metadata of IDPs in circle of trust is here --&gt;
  &lt;!-- Do no forget to call iniitalize method on providers --&gt;
  &lt;bean id="metadata" class="org.springframework.security.saml.metadata.CachingMetadataManager"&gt;
      &lt;constructor-arg&gt;
          &lt;list&gt;
  
              &lt;bean class="org.springframework.security.saml.metadata.ExtendedMetadataDelegate"&gt;
                  &lt;constructor-arg&gt;
                      &lt;bean class="org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider"&gt;
                          &lt;constructor-arg&gt;
                              &lt;value type="java.io.File"&gt;/WEB-INF/spring/security/shibboleth.xml&lt;/value&gt;
                          &lt;/constructor-arg&gt;
                          &lt;property name="parserPool" ref="parserPool" /&gt;
                      &lt;/bean&gt;
                  &lt;/constructor-arg&gt;
                  &lt;constructor-arg&gt;
                      &lt;bean class="org.springframework.security.saml.metadata.ExtendedMetadata"&gt;
                      &lt;/bean&gt;
                  &lt;/constructor-arg&gt;
              &lt;/bean&gt;
              &lt;bean class="org.springframework.security.saml.metadata.ExtendedMetadataDelegate"&gt;
                  &lt;constructor-arg&gt;
                      &lt;bean class="org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider"&gt;
                          &lt;constructor-arg&gt;
                              &lt;value type="java.io.File"&gt;/WEB-INF/spring/security/localhost_sp.xml&lt;/value&gt;
                          &lt;/constructor-arg&gt;
                          &lt;property name="parserPool" ref="parserPool" /&gt;
                      &lt;/bean&gt;
                  &lt;/constructor-arg&gt;
                  &lt;constructor-arg&gt;
                      &lt;bean class="org.springframework.security.saml.metadata.ExtendedMetadata"&gt;
                          &lt;property name="local" value="true" /&gt;
                          &lt;property name="alias" value="localhost" /&gt;
                          &lt;property name="securityProfile" value="metaiop" /&gt;
                          &lt;property name="sslSecurityProfile" value="pkix" /&gt;
                          &lt;property name="signingKey" value="tomcat" /&gt;
                          &lt;property name="encryptionKey" value="tomcat" /&gt;
                          &lt;property name="tlsKey" value="tomcat" /&gt;
                          &lt;property name="requireArtifactResolveSigned" value="false" /&gt;
                          &lt;property name="requireLogoutRequestSigned" value="false" /&gt;
                          &lt;property name="requireLogoutResponseSigned" value="false" /&gt;
                      &lt;/bean&gt;
                  &lt;/constructor-arg&gt;
              &lt;/bean&gt;
          &lt;/list&gt;
      &lt;/constructor-arg&gt;      
      &lt;!-- OPTIONAL used when one of the metadata files contains information about this service provider --&gt;
      &lt;property name="hostedSPName" value="localhost"/&gt;
      &lt;!-- OPTIONAL property: can tell the system which IDP should be used for authenticating user by default. --&gt;
      &lt;!-- &lt;property name="defaultIDP" value="http://localhost:8080/opensso"/&gt; --&gt;
  &lt;/bean&gt;
  
  &lt;!-- SAML Authentication Provider responsible for validating of received SAML messages --&gt;
  &lt;bean id="samlAuthenticationProvider" class="org.springframework.security.saml.SAMLAuthenticationProvider"&gt;
      &lt;!-- OPTIONAL property: can be used to store/load user data after login --&gt;
      &lt;!-- &lt;property name="userDetails" ref="bean" /&gt; --&gt;
  &lt;/bean&gt;
  
  &lt;!-- Provider of default SAML Context --&gt;
  &lt;bean id="contextProvider" class="org.springframework.security.saml.context.SAMLContextProviderImpl" /&gt;
  
  &lt;!-- Processing filter for WebSSO profile messages --&gt;
  &lt;bean id="samlWebSSOProcessingFilter" class="org.springframework.security.saml.SAMLProcessingFilter"&gt;
      &lt;property name="authenticationManager" ref="authenticationManager" /&gt;
      &lt;property name="authenticationSuccessHandler" ref="successRedirectHandler" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Processing filter for WebSSO Holder-of-Key profile --&gt;
  &lt;bean id="samlWebSSOHoKProcessingFilter" class="org.springframework.security.saml.SAMLWebSSOHoKProcessingFilter"&gt;
      &lt;property name="authenticationManager" ref="authenticationManager" /&gt;
      &lt;property name="authenticationSuccessHandler" ref="successRedirectHandler" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Logout handler terminating local session --&gt;
  &lt;bean id="logoutHandler" class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"&gt;
      &lt;property name="invalidateHttpSession" value="false" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Override default logout processing filter with the one processing SAML messages --&gt;
  &lt;bean id="samlLogoutFilter" class="org.springframework.security.saml.SAMLLogoutFilter"&gt;
      &lt;constructor-arg ref="successLogoutHandler" /&gt;
      &lt;constructor-arg ref="logoutHandler" /&gt;
      &lt;constructor-arg ref="logoutHandler" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Filter processing incoming logout messages --&gt;
  &lt;!-- First argument determines URL user will be redirected to after successful global logout --&gt;
  &lt;bean id="samlLogoutProcessingFilter" class="org.springframework.security.saml.SAMLLogoutProcessingFilter"&gt;
      &lt;constructor-arg ref="successLogoutHandler" /&gt;
      &lt;constructor-arg ref="logoutHandler" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Class loading incoming SAML messages from httpRequest stream --&gt;
  &lt;bean id="processor" class="org.springframework.security.saml.processor.SAMLProcessorImpl"&gt;
      &lt;constructor-arg&gt;
          &lt;list&gt;
              &lt;ref bean="redirectBinding" /&gt;
              &lt;ref bean="postBinding" /&gt;
              &lt;ref bean="artifactBinding" /&gt;
              &lt;ref bean="soapBinding" /&gt;
              &lt;ref bean="paosBinding" /&gt;
          &lt;/list&gt;
      &lt;/constructor-arg&gt;
  &lt;/bean&gt;
  
  &lt;!-- SAML 2.0 WebSSO Assertion Consumer --&gt;
  &lt;bean id="webSSOprofileConsumer" class="org.springframework.security.saml.websso.WebSSOProfileConsumerImpl" /&gt;
  
  &lt;!-- SAML 2.0 Holder-of-Key WebSSO Assertion Consumer --&gt;
  &lt;bean id="hokWebSSOprofileConsumer" class="org.springframework.security.saml.websso.WebSSOProfileConsumerHoKImpl" /&gt;
  
  &lt;!-- SAML 2.0 Web SSO profile --&gt;
  &lt;bean id="webSSOprofile" class="org.springframework.security.saml.websso.WebSSOProfileImpl" /&gt;
  
  &lt;!-- SAML 2.0 Holder-of-Key Web SSO profile --&gt;
  &lt;bean id="hokWebSSOProfile" class="org.springframework.security.saml.websso.WebSSOProfileConsumerHoKImpl" /&gt;
  
  &lt;!-- SAML 2.0 ECP profile --&gt;
  &lt;bean id="ecpprofile" class="org.springframework.security.saml.websso.WebSSOProfileECPImpl" /&gt;
  
  &lt;!-- SAML 2.0 Logout Profile --&gt;
  &lt;bean id="logoutprofile" class="org.springframework.security.saml.websso.SingleLogoutProfileImpl" /&gt;
  
  &lt;!-- Bindings, encoders and decoders used for creating and parsing messages --&gt;
  &lt;bean id="postBinding" class="org.springframework.security.saml.processor.HTTPPostBinding"&gt;
      &lt;constructor-arg ref="parserPool" /&gt;
      &lt;constructor-arg ref="velocityEngine" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="redirectBinding" class="org.springframework.security.saml.processor.HTTPRedirectDeflateBinding"&gt;
      &lt;constructor-arg ref="parserPool" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="artifactBinding" class="org.springframework.security.saml.processor.HTTPArtifactBinding"&gt;
      &lt;constructor-arg ref="parserPool" /&gt;
      &lt;constructor-arg ref="velocityEngine" /&gt;
      &lt;constructor-arg&gt;
          &lt;bean class="org.springframework.security.saml.websso.ArtifactResolutionProfileImpl"&gt;
              &lt;constructor-arg&gt;
                  &lt;bean class="org.apache.commons.httpclient.HttpClient" /&gt;
              &lt;/constructor-arg&gt;
              &lt;property name="processor"&gt;
                  &lt;bean id="soapProcessor" class="org.springframework.security.saml.processor.SAMLProcessorImpl"&gt;
                      &lt;constructor-arg ref="soapBinding" /&gt;
                  &lt;/bean&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/constructor-arg&gt;
  &lt;/bean&gt;
  
  &lt;bean id="soapBinding" class="org.springframework.security.saml.processor.HTTPSOAP11Binding"&gt;
      &lt;constructor-arg ref="parserPool" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="paosBinding" class="org.springframework.security.saml.processor.HTTPPAOS11Binding"&gt;
      &lt;constructor-arg ref="parserPool" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Initialization of OpenSAML library --&gt;
  &lt;bean class="org.springframework.security.saml.SAMLBootstrap" /&gt;
  
  &lt;!-- Initialization of the velocity engine --&gt;
  &lt;bean id="velocityEngine" class="org.springframework.security.saml.util.VelocityFactory" factory-method="getEngine" /&gt;
  
  &lt;!-- XML parser pool needed for OpenSAML parsing --&gt;
  &lt;bean id="parserPool" class="org.opensaml.xml.parse.BasicParserPool" scope="singleton" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Any idea?</p>
  
  <p>Thanks
  emanuele   </p>