So the exact problem I was having was that it would work the first time. So if I restarted the app server and then signed up, everything was ok, I would be able to access a restricted page no problem. But if I invalidated the session and then signed up again with a different user I would get the login page. The log file said:
Code:
2004-09-06 10:20:04,078 (net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter) - Authentication failed - adding target URL to Session: http://localhost:8081/myapp/secure/debug.htm
net.sf.acegisecurity.AuthenticationCredentialsNotFoundException: Authentication credentials were not found in the SecureContext
at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.interceptor(AbstractSecurityInterceptor.java:289)
at net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:78)
at net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter.doFilter(SecurityEnforcementFilter.java:165)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:88)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:84)
at net.sf.acegisecurity.ui.AbstractIntegrationFilter.doFilter(AbstractIntegrationFilter.java:170)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:88)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:84)
at net.sf.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:368)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:88)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:84)
at org.springframework.orm.hibernate.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:117)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:73)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:84)
at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:177)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:177)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:221)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:263)
at com.caucho.server.port.TcpConnection.run(TcpConnection.java:323)
at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:430)
at com.caucho.util.ThreadPool.run(ThreadPool.java:377)
at java.lang.Thread.run(Thread.java:536)
This would happen even if I closed all browsers. So something else was at play besides HttpSession. I found a fix, set the SecureContext to null explicitly. This does not make sense to me since I thought the SecureContext only hung around for the life of the thread, and each request is a new thread right? So anyway the code that makes it work is:
Code:
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(webUser,webUser.getPassword());
auth.setAuthenticated(true);
auth.setDetails(request.getRemoteAddr());
auth.setAuthorities(webUser.getAuthorities());
session.setAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY, auth);
SecureContext context = (SecureContext)ContextHolder.getContext();
if (context != null) {
ContextHolder.setContext(null);
}