Announcement Announcement Module
Collapse
No announcement yet.
Changing user back to anonymous on 'logout' ? Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing user back to anonymous on 'logout' ?

    Hi,

    I have Acegi working with ROLE_USER and ROLE_ANONYMOUS. I want to have a 'logout' which should result in the Authenication object held in the session changing from a specific user back to being the anonymous user. What is the easiest way to do this ?

    thanks

  • #2
    answer

    :roll:


    quite simple really:

    ContextHolder.setContext(null);

    - when using acegi-security-0.8.2.jar


    or (I believe)

    SecurityContextHolder.getContext().setAuthenticati on(null);

    - when using 0.9 or higher

    Comment


    • #3
      Hi djmh68

      But when i call ContextHolder.setContext(null), it generated an exception like this

      java.lang.IllegalStateException: ContextHolder invalid: 'null': are your filters
      ordered correctly? HttpSessionContextIntegrationFilter should have already exec
      uted by this time (look for it in the stack dump below)

      what do you think?
      Thanks

      Comment


      • #4
        Don't know why you're getting an exception. My app 'logsout' without producing an exception, using ContextHolder.setContext(null). This is a copy of my config as it relates to Acegi:

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

        <beans>

        <!-- ======================== FILTER CHAIN ======================= -->
        <bean id="filterChainProxy" class="net.sf.acegisecurity.util.FilterChainProxy" >
        <property name="filterInvocationDefinitionSource">
        <value>
        CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
        PATTERN_TYPE_APACHE_ANT
        /**=httpSessionContextIntegrationFilter,authenticat ionProcessingFilter,anonymousProcessingFilter,secu rityEnforcementFilter
        </value>
        </property>
        </bean>

        <!-- ======================== AUTHENTICATION ======================= -->

        <!-- authenticationManager -->
        <bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderMana ger">
        <property name="providers">
        <list>
        <ref bean="daoAuthenticationProvider" />
        <ref local="anonymousAuthenticationProvider"/>
        </list>
        </property>
        </bean>

        <!-- authenticationDao -->
        <bean id="authenticationDao" class="net.sf.acegisecurity.providers.dao.jdbc.Jdb cDaoImpl">
        <property name="dataSource"><ref bean="dataSource" /></property>
        </bean>

        <!-- daoAuthenticationProvider -->
        <bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthe nticationProvider">
        <property name="authenticationDao"><ref bean="authenticationDao" /></property>
        </bean>

        <!-- Automatically receives AuthenticationEvent messages from DaoAuthenticationProvider -->
        <bean id="loggerListener" class="net.sf.acegisecurity.providers.dao.event.Lo ggerListener"/>

        <!-- anonymousProcessingFilter -->
        <bean id="anonymousProcessingFilter" class="net.sf.acegisecurity.providers.anonymous.An onymousProcessingFilter">
        <property name="key"><value>foobar</value></property>
        <property name="userAttribute"><value>anonymousUser,ROLE_ANO NYMOUS</value></property>
        </bean>

        <!-- anonymousAuthenticationProvider -->
        <bean id="anonymousAuthenticationProvider" class="net.sf.acegisecurity.providers.anonymous.An onymousAuthenticationProvider">
        <property name="key"><value>foobar</value></property>
        </bean>

        <!-- roleVoter -->
        <bean id="roleVoter" class="net.sf.acegisecurity.vote.RoleVoter" />

        <!-- accessDecisionManager -->
        <bean id="accessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased" >
        <property name="allowIfAllAbstainDecisions"><value>false</value></property>
        <property name="decisionVoters">
        <list>
        <ref bean="roleVoter" />
        </list>
        </property>
        </bean>

        <!-- httpSessionContextIntegrationFilter -->
        <bean id="httpSessionContextIntegrationFilter" class="net.sf.acegisecurity.context.HttpSessionCon textIntegrationFilter">
        <property name="context"><value>net.sf.acegisecurity.context .security.SecureContextImpl</value></property>
        </bean>

        <!-- securityEnforcementFilter -->
        <bean id="securityEnforcementFilter" class="net.sf.acegisecurity.intercept.web.Security EnforcementFilter">
        <property name="filterSecurityInterceptor"><ref bean="filterInvocationInterceptor" /></property>
        <property name="authenticationEntryPoint"><ref bean="authenticationEntryPoint" /></property>
        </bean>

        <!-- authenticationProcessingFilter -->
        <bean id="authenticationProcessingFilter" class="net.sf.acegisecurity.ui.webapp.Authenticati onProcessingFilter">
        <property name="authenticationFailureUrl"><value>/security/login.html?login_error=true</value></property>
        <property name="defaultTargetUrl"><value>/home/welcome.html</value></property>
        <property name="filterProcessesUrl"><value>/security/j_acegi_security_check.html</value></property>
        <property name="authenticationManager"><ref bean="authenticationManager"/></property>
        </bean>

        <!-- authenticationEntryPoint -->
        <bean id="authenticationEntryPoint" class="net.sf.acegisecurity.ui.webapp.Authenticati onProcessingFilterEntryPoint">
        <property name="loginFormUrl"><value>/security/login.html</value></property>
        </bean>

        <!-- httpRequestAccessDecisionManager -->
        <bean id="httpRequestAccessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased" >
        <property name="allowIfAllAbstainDecisions"><value>false</value></property>
        <property name="decisionVoters">
        <list>
        <ref bean="roleVoter"/>
        </list>
        </property>
        </bean>

        <!-- filterInvocationInterceptor -->
        <bean id="filterInvocationInterceptor" class="net.sf.acegisecurity.intercept.web.FilterSe curityInterceptor">
        <property name="authenticationManager"><ref bean="authenticationManager" /></property>
        <property name="accessDecisionManager"><ref bean="accessDecisionManager" /></property>
        <property name="objectDefinitionSource">
        <value>
        CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
        PATTERN_TYPE_APACHE_ANT
        /security/**=ROLE_ANONYMOUS,ROLE_REGISTERED
        /home/**=ROLE_ANONYMOUS,ROLE_REGISTERED
        /registered/**=ROLE_REGISTERED
        </value>
        </property>
        </bean>

        </beans>

        Comment

        Working...
        X