Announcement Announcement Module
Collapse
No announcement yet.
SecurityContextHolder.getContext() is NULL Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SecurityContextHolder.getContext() is NULL

    Hi Guys,

    Hope you can help with a strange issue I am seeing. Hopefully I am doing something very silly and this error is easily fixable.

    I am trying to retrieve the username of the person currently logged in from the security context. I do this in the following manner in my unit test (it is broken up more than it has to be for debugging purposes):
    Code:
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication author = ctx.getAuthentication();
    String username = ((UserDetails) author.getPrincipal()).getUsername();
    After the first line, ctx's value is: org.acegisecurity.context.SecurityContextImpl@ffff ffff: Null authentication

    Other than this, my unit test works just fine -- authenticating the username/password passed in, and retrieving their roles.

    Here is my acegi configuration file:

    Code:
    <beans>
    
    	<bean id="initialDirContextFactory"
    		class="org.acegisecurity.ldap.DefaultInitialDirContextFactory">
    		<constructor-arg value="ldap://${ldap.url}/${ldap.dc}" />
    		<property name="managerDn"
    			value="CN=${ldap.user},${ldap.manager}" />
    		<property name="managerPassword" value="${ldap.password}" />
    		<property name="useConnectionPool" value="false" />
    		<property name="extraEnvVars">
    			<map>
    				<entry>
    					<key>
    						<value>java.naming.referral</value>
    					</key>
    					<value>follow</value>
    				</entry>
    			</map>
    		</property>
    	</bean>
    
    	<bean id="userSearch"
    		class="org.acegisecurity.ldap.search.FilterBasedLdapUserSearch">
    		<constructor-arg value="" />
    		<constructor-arg value="(sAMAccountName={0})" />
    		<constructor-arg ref="initialDirContextFactory" />
    		<property name="searchSubtree" value="true" />
    	</bean>
    
    	<bean id="ldapAuthenticationProvider"
    		class="org.acegisecurity.providers.ldap.LdapAuthenticationProvider">
    		<constructor-arg>
    			<bean
    				class="org.acegisecurity.providers.ldap.authenticator.BindAuthenticator">
    				<constructor-arg ref="initialDirContextFactory" />
    				<property name="userSearch" ref="userSearch" />
    			</bean>
    		</constructor-arg>
    		<constructor-arg>
    			<bean
    				class="org.acegisecurity.providers.ldap.populator.DefaultLdapAuthoritiesPopulator">
    				<constructor-arg>
    					<ref local="initialDirContextFactory" />
    				</constructor-arg>
    				<constructor-arg>
    					<value>${ldap.groupSearch}</value>
    				</constructor-arg>
    				<property name="groupRoleAttribute" value="CN" />
    				<property name="convertToUpperCase" value="true"/>
    			</bean>
    		</constructor-arg>
    	</bean>
    
    	<bean id="ldapUserDetailsMapper"
    		class="org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper">
    		<property name="passwordAttributeName" value="userpassword"/>
    		<property name="convertToUpperCase" value="true" />
    		<property name="rolePrefix" value="" />
    		<property name="roleAttributes">
    			<list>
    				<value>memberOf</value>
    			</list>
    		</property>
    	</bean>
    
    	<bean id="authenticationManager"
    		class="org.acegisecurity.providers.ProviderManager">
    		<property name="providers">
    			<list>
    				<ref local="ldapAuthenticationProvider" />
    			</list>
    		</property>
    	</bean>
    
    	<bean id="filterChainProxy"
    		class="org.acegisecurity.util.FilterChainProxy">
    		<property name="filterInvocationDefinitionSource">
    			<value>
    				CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
    				PATTERN_TYPE_APACHE_ANT
    				/**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor
    			</value>
    		</property>
    	</bean>
    
    	<!-- Log failed authentication attempts to commons-logging -->
    	<bean id="loggerListener"
    		class="org.acegisecurity.event.authentication.LoggerListener" />
    
    	<bean id="passwordEncoder"
    		class="org.acegisecurity.providers.encoding.Md5PasswordEncoder" />
    
    	<bean id="anonymousAuthenticationProvider"
    		class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
    		<property name="key">
    			<value>foobar</value>
    		</property>
    	</bean>
    
    	<bean id="authenticationProcessingFilterEntryPoint"
    		class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
    		<property name="loginFormUrl">
    			<value>/login/login.action</value>
    		</property>
    	</bean>
    
    	<bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter" />
    
    	<bean id="httpRequestAccessDecisionManager"
    		class="org.acegisecurity.vote.AffirmativeBased">
    		<property name="allowIfAllAbstainDecisions">
    			<value>false</value>
    		</property>
    		<property name="decisionVoters">
    			<list>
    				<ref bean="roleVoter" />
    			</list>
    		</property>
    	</bean>
    
    	<bean id="httpSessionContextIntegrationFilter"
    		class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" />
    
    	<bean id="authenticationProcessingFilter"
    		class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
    		<property name="authenticationManager">
    			<ref bean="authenticationManager" />
    		</property>
    		<property name="authenticationFailureUrl">
    			<value>/login/login.action?login_error=1</value>
    		</property>
    		<property name="defaultTargetUrl">
    			<value>/main.action</value>
    		</property>
    		<property name="filterProcessesUrl">
    			<value>/j_acegi_security_check</value>
    		</property>
    	</bean>
    
    	<bean id="anonymousProcessingFilter"
    		class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
    		<property name="key">
    			<value>foobar</value>
    		</property>
    		<property name="userAttribute">
    			<value>anonymousUser,ROLE_ANONYMOUS</value>
    		</property>
    	</bean>
    
    	<bean id="exceptionTranslationFilter"
    		class="org.acegisecurity.ui.ExceptionTranslationFilter">
    		<property name="authenticationEntryPoint"
    			ref="authenticationProcessingFilterEntryPoint" />
    	</bean>
    
    	<bean id="filterInvocationInterceptor"
    		class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
    		<property name="authenticationManager">
    			<ref bean="authenticationManager" />
    		</property>
    		<property name="accessDecisionManager">
    			<ref bean="httpRequestAccessDecisionManager" />
    		</property>
    		<property name="objectDefinitionSource">
    			<value>
    				CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
    				PATTERN_TYPE_APACHE_ANT
    				blah blah blah
    		</property>
    	</bean>
    
    </beans>

  • #2
    Nevermind, problem solved by adding ctx.setAuthentication(...);. DOH!

    Comment


    • #3
      more details?

      holder.setContext(?);?should be a SecurityContext but how can i instanciate it

      Comment


      • #4
        You do not need to. The default ThreadLocalSecurityContextHolderStrategy ensures that a context is created if none is available.
        So getContext() never returns null (as specified in the API doc).

        Regards,
        Andreas

        Comment


        • #5
          another question

          thanks for your reply;
          what i should do about the
          ctx.setAuthentication(...)
          do i need to instanticate a Authentication ?

          Comment


          • #6
            What is it you are trying to do? If you want to programmatically authenticate someone, then yes you need to.

            Comment

            Working...
            X