0
votes

The following configuration is resulting in a redirect loop to a CAS server that otherwise works for other apps. I'm able to login and the resulting url is:

http://localhost:18080/ourapp/app/j_spring_cas_security_check?ticket=ST-18-CQMfGMeDPcXkdKGjosfj-localsso.subdomain.mycompany.com

applicationContext-security.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">   

<!-- Enable security, let the casAuthenticationEntryPoint handle all intercepted 
urls. The CAS_FILTER needs to be in the right position within the filter 
chain. -->
<security:http entry-point-ref="casEntryPoint" use-expressions="true" auto-config="false">
    <!--<security:intercept-url pattern="/**" access="permitAll"/> -->
      <security:anonymous username="guest" granted-authority="isAnonymous()"/>
    <security:intercept-url pattern="/app/resources/**" access="hasAnyRole('ROLE_ANONYMOUS', 'cm_user')"/>
    <security:intercept-url pattern="/app/**" access="hasRole('cm_user')"/>
    <security:custom-filter position="CAS_FILTER" ref="casFilter"/>

    <security:session-management>
        <security:concurrency-control max-sessions="5" error-if-maximum-exceeded="true"/>
   </security:session-management>
</security:http>

<!-- Required for the casProcessingFilter, so define it explicitly set and 
specify an Id Even though the authenticationManager is created by default 
when namespace based config is used. -->
<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="casAuthenticationProvider"/>
</security:authentication-manager>

<!-- This section is used to configure CAS. The service is the actual redirect 
that will be triggered after the CAS login sequence. -->
<bean id="serviceProperties"
    class="org.springframework.security.cas.ServiceProperties"
    p:service="http://localhost:18080/ourapp/app/j_spring_cas_security_check"
    p:sendRenew="false"/>

<!-- The CAS filter handles the redirect from the CAS server and starts 
the ticket validation. -->
<bean id="casFilter"
    class="org.springframework.security.cas.web.CasAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
</bean>

<!-- The entryPoint intercepts all the CAS authentication requests. It redirects 
to the CAS loginUrl for the CAS login page. -->
<bean id="casEntryPoint"
    class="org.springframework.security.cas.web.CasAuthenticationEntryPoint"
    p:serviceProperties-ref="serviceProperties"
    p:loginUrl="https://devmcauth01.nexus.mycompany.com:5443/login"/>

<!-- Handles the CAS ticket processing. -->
<bean id="casAuthenticationProvider"
  class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
    <property name="userDetailsService" ref="userDetailsService"/>
    <property name="serviceProperties" ref="serviceProperties" />
    <property name="ticketValidator">
      <bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
        <constructor-arg index="0" value="https://devmcauth01.nexus.mycompany.com:5443" />
      </bean>
    </property>
    <property name="key" value="cas"/>
</bean>

<bean id="permissionConversionService"
    class="com.mycompany.connmgmt.misc.security.PermissionConversionServiceImpl"/>

<bean id="userDetailsService"
    class="com.mycompany.connmgmt.misc.security.CmUserDetailsService">
    <property name="userRepository" ref="userRepository"/>
    <property name="permissionConversionService" ref="permissionConversionService"/>
</bean>

<bean id="accessDecisionManager" 
        class="org.springframework.security.access.vote.AffirmativeBased">
    <property name="allowIfAllAbstainDecisions">
        <value>false</value>
    </property>
    <property name="decisionVoters">
        <list>
           <ref bean="roleVoter"/>
        </list>
    </property>
</bean>

<bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter">
    <property name="rolePrefix" value=""/>
</bean>

<bean id="loggerListener" 
    class="org.springframework.security.access.event.LoggerListener"/>

<!-- Enable JSR250 annotations, disable Spring Security annotations for now -->
<security:global-method-security secured-annotations="disabled" 
    jsr250-annotations="enabled" 
    access-decision-manager-ref="accessDecisionManager"/>

web.xml

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<display-name>Our App</display-name>

<filter>
    <filter-name>OpenPersistenceManagerInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter
    </filter-class>
    <init-param>
      <param-name>persistenceManagerFactoryBeanName</param-name>
      <param-value>internalPersistenceManagerFactory</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>dsmProdOpenPersistenceManagerInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter
    </filter-class>
    <init-param>
      <param-name>persistenceManagerFactoryBeanName</param-name>
      <param-value>dsmProdInternalPersistenceManagerFactory</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>dsmTestOpenPersistenceManagerInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter
    </filter-class>
    <init-param>
      <param-name>persistenceManagerFactoryBeanName</param-name>
      <param-value>dsmTestInternalPersistenceManagerFactory</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>onboarderOpenPersistenceManagerInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter
    </filter-class>
    <init-param>
      <param-name>persistenceManagerFactoryBeanName</param-name>
      <param-value>onboarderInternalPersistenceManagerFactory</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    </filter-class>
    <init-param>
      <param-name>entityManagerFactoryBeanName</param-name>
      <param-value>orgDataDir_EntityManagerFactory</param-value>
    </init-param>
</filter>

<!-- 
    Route all requests through Spring Security for A&A
-->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<!--  
    There are three means to configure Wickets configuration mode and they are
    tested in the order given. 
    1) A system property: -Dwicket.configuration
    2) servlet specific <init-param>
    3) context specific <context-param>
    The value might be either "development" (reloading when templates change)
    or "deployment". If no configuration is found, "development" is the default.
-->
<filter>
    <filter-name>wicket</filter-name>
    <filter-class>
        org.apache.wicket.protocol.http.WicketFilter
    </filter-class>
    <init-param>
        <param-name>applicationFactoryClassName</param-name>
        <param-value>
            org.apache.wicket.spring.SpringWebApplicationFactory
        </param-value>
    </init-param>
</filter>

<!-- The SpringWebApplicationFactory will need access to a Spring Application context, configured like this... -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:/applicationSecurityContext.xml
        classpath:/applicationContext.xml
        classpath:/orgDataDir-config.xml
        classpath:/dsm-prod-spring-jdo-config.xml
        classpath:/dsm-test-spring-jdo-config.xml
        classpath:/dsm-prod-beans.xml
        classpath:/dsm-test-beans.xml
        classpath:/exchange-config.xml
        classpath:/reporting.xml
        classpath:/profile-conversion.xml
        classpath:/com/mycompany/connmgmt/integration/jms/jms-client.xml
        classpath:/com/mycompany/connmgmt/integration/jms/test-jms-client.xml
        classpath:/com/mycompany/connmgmt/integration/jms/jms-consumers.xml
        classpath:/com/mycompany/connmgmt/integration/jms/jms-producers.xml
        classpath*:/com/mycompany/core/odd/odd-applicationContext.xml
        classpath:/task-support.xml
    </param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<!-- Support for session- and request-scoped beans -->
<listener>
   <listener-class>
        org.springframework.web.context.request.RequestContextListener
   </listener-class>
</listener> 

<!-- 
    Listen for Session events and publish for Spring Security's consumption
-->
<listener>
    <listener-class>
        org.springframework.security.web.session.HttpSessionEventPublisher
    </listener-class>
</listener>

<filter-mapping>
    <filter-name>OpenPersistenceManagerInViewFilter</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>dsmProdOpenPersistenceManagerInViewFilter</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>dsmTestOpenPersistenceManagerInViewFilter</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>onboarderOpenPersistenceManagerInViewFilter</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>wicket</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

<session-config>
    <!-- 10 hours [TRAC-83] -->
    <session-timeout>600</session-timeout> 
</session-config>
</web-app>

Note that this is a different problem from related errors: This suggests pre-auth-filter, which shouldn't be required per the Spring/CAS docs or example app: How to prevent JA-SIG CAS spring security redirect loop?

This mentions that j_spring_cas_security_check needs to be under a URL that's protected by CasAuthenticationFilter: Redirect loop with CAS and Spring Security

This mentions that a user details service is required, which I have: Redirect loop with Grails Spring Security CAS plugin

1

1 Answers

1
votes

The solution was to add filterProcessesUrl for the url used by the filter. I think this is required because an intercept-url entry is mapped to that location. When CAS returns the ticket, it can't reach the location because the user hasn't authenticated yet.

<bean id="casFilter"
    class="org.springframework.security.cas.web.CasAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="filterProcessesUrl" value="/app/j_spring_cas_security_check"/>
</bean>