title: grails -  simple App to test spring-security-core secure-channel on Heroku
body: <p>Because setting the ports <a href="https://stackoverflow.com/questions/9090709/grails-spring-security-core-secure-channel-causing-redirect-loop-on-heroku">in this question</a> didn't solve the redirect loop problem, I created a bare-bones test app (grails 2.0, latest spring-security-core 1.2.7.1), so the redirect loop problem is easy to reproduce / hopefully solve:  Here are the steps:</p>
  
  <p>1- Created new app called test, and created a controller called simple (also in a test package), i.e.:</p>
  
  <pre><code>   package test
  
     class SimpleController {
         def index()  { redirect action: 'start'    }
         def start()  { render "start - not secure" }
         def middle() { render "middle - secure"    }
         def end()    { render "end - not secure"   }
     }  
  </code></pre>
  
  <p>2- Did  <strong><em>grails install spring-security-core</em></strong>  and also <strong><em>grails s2-quickstart test User Role</em></strong> </p>
  
  <p>3- In Config.groovy, added following lines, and then did <strong><em>run-app -https</em></strong> to test on my dev machine:</p>
  
  <pre><code>   grails.plugins.springsecurity.secureChannel.definition = [
       '/simple/middle/**':    'REQUIRES_SECURE_CHANNEL',
       '/simple/end/**':       'REQUIRES_INSECURE_CHANNEL',
       '/simple/**':           'ANY_CHANNEL'
     ]
  </code></pre>
  
  <p>RESULT: all checks out, can enter URLs  ../simple/start, and then entering ../simple/middle enters SSL/https mode, as desired.</p>
  
  <p>4- In Config.groovy, I then configured <a href="http://grails-plugins.github.com/grails-spring-security-core/docs/manual/guide/17%20Channel%20Security.html" rel="nofollow noreferrer">ports</a> to run on Heroku (guestimate, found no docs/refs):</p>
  
  <pre><code>  grails.plugins.springsecurity.portMapper.httpPort = '80'
    grails.plugins.springsecurity.portMapper.httpsPort = '443'
  </code></pre>
  
  <p>5- After doing the <a href="http://grails-plugins.github.com/grails-heroku/docs/manual/index.html" rel="nofollow noreferrer">steps</a> <strong><em>git init</em></strong>, <strong><em>git add .</em></strong>, <strong><em>git commit -m "initial commit"</em></strong>, and <strong><em>heroku create --stack cedar</em></strong> , I went to Heroku and added the PiggyBack SSL to the newly created app there.   I then did a <strong><em>git push heroku master</em></strong> to deploy.</p>
  
  <p>RESULT: Got common "IllegalStateException, no thread-bound request found", a problem described <a href="http://jira.grails.org/browse/GPSPRINGSECURITYCORE-98" rel="nofollow noreferrer">here</a> and <a href="http://padcom13.blogspot.com/2011/12/grails-heroku-and-spring-security-core.html" rel="nofollow noreferrer">here</a>. </p>
  
  <p>6- As such, in BuildConfig.groovy, I made the following 2 changes to the plugins {} section:</p>
  
  <pre><code>    //runtime ":resources:1.1.5"
      compile ":webxml:1.4.1"   
  </code></pre>
  
  <p>RESULT: IllegalStateException goes away, and I'm able to access the simple controller (and login controller).  I'm able to prefix those urls with https, and all is good.  However, if I enter the ../simple/middle URL, I get the redirect loop (traces similar to <a href="https://stackoverflow.com/questions/9090709/grails-spring-security-core-secure-channel-causing-redirect-loop-on-heroku">here</a>).</p>
  
  <p>7- Out of curiosity I commented out half the change in step 6, namely <em>//compile: "webxml:1.4.1"</em>:</p>
  
  <p>RESULT: got same IllegalStateException</p>
  
  <p>8- <em>Because</em> I wasn't sure how the heroku and cloud support plugins factored into the basic steps steps above, I waited to add them in until this step.  I then did  <strong><em>grails install-plugin cloud-support</em></strong> 
   and <strong><em>grails install-plugin heroku</em></strong>, and updated BuildConfig.groovy so it now looks like:</p>
  
  <pre><code>   plugins {
       runtime ":hibernate:$grailsVersion"
       runtime ":jquery:1.7.1"
  
        //runtime ":resources:1.1.5"
        //compile ":webxml:1.4.1"
        compile ':heroku:1.0'
        compile ':cloud-support:1.0.8'
  
        compile ":tomcat:$grailsVersion"
     }
  </code></pre>
  
  <p>and then did a <strong><em>clean</em></strong> and updated the git repository and pushed out to heroku.</p>
  
  <p>RESULT:  same IllegalStateException (note the webxml dependency is commented out).</p>
  
  <p>9- I then uncommented <em>compile ":webxml:1.4.1"</em>, and pushed out to heroku again:</p>
  
  <p>RESULT: exactly as in step 3.  I.e. with the exception of ../simple/middle, all URLs work, including adding an https prefix.  Goiing to the ../simple/middle URL causes the redirect loop problem.</p>
  
  <p>Can you please advise how to solve this?  If there are things for me to try out (i.e. non-definite solution), please just update the <strong>comments</strong> section and I'll respond back with results.  Thanks much.</p>
  
  <h2>------------------------------------------------------------------------------------------</h2>
  
  <p>Update using new spring-security 1.2.7.2.</p>
  
  <p>My BuildConfig.groovy is now:</p>
  
  <pre><code> dependencies {
      // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
      runtime 'mysql:mysql-connector-java:5.1.16'
  }
  
  plugins {
      runtime ":hibernate:$grailsVersion"
      runtime ":jquery:1.7.1"
  
      runtime ":resources:1.1.6"
      compile ":spring-security-core:1.2.7.2"
  
      compile ":webxml:1.4.1"
  
      compile ':heroku:1.0'
      compile ':cloud-support:1.0.8'
  
      build ":tomcat:$grailsVersion"
  }
  </code></pre>
  
  <p>Got a PostGres dependency error deploying to Heroku even though using MySql.  So I removed all apps on Heroku and blew away my git repository, and started with a fresh deployment/fresh app.</p>
  
  <p>Still get this PostGres dependency error (below).  Note: got the "No Transaction manager found - if you webapp requires one, please configure one" warning previously when the MySql worked.  Hmm.</p>
  
  <pre><code>  12-02-03T07:18:09+00:00 app[web.1]: 2012-02-03 07:18:09.810:INFO:omjr.Runner:Runner
    12-02-03T07:18:09+00:00 app[web.1]: 2012-02-03 07:18:09.811:WARN:omjr.Runner:No tx manager found
    12-02-03T07:18:09+00:00 app[web.1]: 2012-02-03 07:18:09.852:INFO:omjr.Runner:Deploying  file:/app/target/momentum-0.1.war @ /
    12-02-03T07:18:09+00:00 app[web.1]: 2012-02-03 07:18:09.869:INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
   12-02-03T07:18:09+00:00 app[web.1]: [o.e.j.w.WebAppContext{/,null},file:/app/target/momentum-0.1.war]
   12-02-03T07:18:09+00:00 app[web.1]: 2012-02-03  07:18:09.915:INFO:oejw.WebInfConfiguration:Extract jar:file:/app/target/momentum-0.1.war!/ to   /tmp/jetty-0.0.0.0-43683-momentum-0.1.war-_-any-/webapp
   12-02-03T07:18:14+00:00 app[web.1]: 2012-02-03 07:18:14.500:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
   12-02-03T07:18:18+00:00 app[web.1]: 2012-02-03 07:18:18.361:INFO:/:Initializing Spring root WebApplicationContext
   12-02-03T07:18:24+00:00 app[web.1]:
   12-02-03T07:18:24+00:00 app[web.1]: Configuring Spring Security Core ...
   12-02-03T07:18:24+00:00 app[web.1]: ... finished configuring Spring Security Core
   12-02-03T07:18:24+00:00 app[web.1]:
   12-02-03T07:18:24+00:00 app[web.1]: 2012-02-03 07:18:24.490:WARN:oejw.WebAppContext:Failed    startup of context o.e.j.w.WebAppContext{/,file:/tmp/jetty-0.0.0.0-43683-momentum-0.1.war-_-any- /webapp/},file:/a
     /target/momentum-0.1.war
     12-02-03T07:18:24+00:00 app[web.1]: org.springframework.beans.factory.BeanCreationException:   Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; 
      ************************************************************   
      nested exception is org.springframework.beans.factory.BeanCreationException: Error  creating bean with name 'sessionFactory': Cannot resolve reference to bean 'lobHandlerDetector' while  setting bean property 'lobHandler'; 
        nested exception is org.springframework.beans.factory.BeanCreationException:  Error creating bean with name 'lobHandlerDetector': Invocation of init method failed; 
     nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'org.postgresql.Driver'
     *************************************************************
  
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.codehaus.groovy.grails.commons.spring.ReloadAwareAutowireCapableBeanFactory.doCreateBean(ReloadAwareAutowireCapableBeanFactory.java:126)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:707)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.codehaus.groovy.grails.commons.spring.DefaultRuntimeSpringConfiguration.getApplicationContext(DefaultRuntimeSpringConfiguration.java:153)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator.configure(GrailsRuntimeConfigurator.java:124)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator.configure(GrailsRuntimeConfigurator.java:165)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.codehaus.groovy.grails.web.context.GrailsConfigUtils.configureWebApplicationContext(GrailsConfigUtils.java:121)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.codehaus.groovy.grails.web.context.GrailsContextLoader.initWebApplicationContext(GrailsContextLoader.java:104)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:643)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:233)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1213)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:589)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:454)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    12-02-03T07:18:24+00:00 app[web.1]:   at  org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:167)
    12-02-03T07:18:24+00:00 app[web.1]:   at  org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.server.Server.doStart(Server.java:261)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.mortbay.jetty.runner.Runner.run(Runner.java:500)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.mortbay.jetty.runner.Runner.main(Runner.java:639)
    12-02-03T07:18:24+00:00 app[web.1]:   at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
  </code></pre>