Tomcat behind Apache HTTP server using Spring Security

Since I have been struggling with applications not working properly behind Apache HTTP server that use Spring Security I dedicated this blog to this issue.

When you develop an application using Spring security this works fine when just running on a Tomcat server e.g. http://localhost:8080/app-context/. The problems start when you want to setup your environment properly by adding an Apache HTTP server in front of it using proxy AJP. You would start by adding the following to your apache config file:

ProxyPass /app-context/ ajp://localhost:8009/app-context/


This works fine when trying to go to http://servername/app-context/ but when you try to login you will be redirected back to the login page everytime. To avoid this behavior you need to add the following line to your Apache configuration:

ProxyPass /app-context/j_spring_security_check ajp://localhost:8009/app-context/j_spring_security_check


For some reason it doesn't work without this line which seems weird to me since you basically map the context and everything that comes after it with the previous line. After trying a lot of options I just tried adding this and this seemed to work. I discovered that when I was logging in (using firebug) that it couldn't find the path /app-context/j_spring_security_check so I tried adding it and it finally worked :-).

There is still one thing left to do though since you also want to be able to logout you have to add the following line too:

ProxyPass /app-context/j_spring_security_check ajp://localhost:8009/app-context/j_spring_security_logout

Comments

  1. Hey Salomon,

    I've the exact problem that u've addressed above. I've followed every instruction listed but my issues is still not solved.
    What do u suggest I try out?
    Is port 8009 the tomcat port?

    ReplyDelete
  2. The second example should be this.

    ProxyPass /app-context/j_spring_security_logout ajp://localhost:8009/app-context/j_spring_security_logout

    ReplyDelete
  3. I was also having this problem and removing the trailing slashes of :

    ProxyPass /app-context/ ajp://localhost:8009/app-context/

    to :

    ProxyPass /app-context ajp://localhost:8009/app-context

    solved it.

    ReplyDelete
  4. I agree with above comment. Without adding spring security endpoints, I simply removed trailing slashes / in ProxyPass and ProxyPassReverse made it work

    ReplyDelete

Post a Comment

Popular posts from this blog

Fastest XML parser

Cargo-itest open source project