spring-bootstatic-resource

Spring boot: Intercepting requests for statically hosted resource files


From my Spring boot application, I am hosting files external to my application. Did so by addResourceHandler() method.

Also, I need to validate the access to these files by checking a couple of session attributes. For this I added an interceptor.

Problem: While intercepting, in the preHandle of the interceptor, the HttpServletRequest does not have any session info (no session) when deployed on LINUX via a proxy router apache settings. However, when run on windows directly from my IDE, HttpServletRequest does have the correct session.

Below is the code:

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class ULCMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

    @Autowired
    private ThumbnailViewRequestInterceptor thumbnailViewRequestInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/content/thumbnails/**").addResourceLocations("file:/app/content/files/).setCachePeriod(60*60*24);
    }

    @Bean
    public MappedInterceptor createThumbnailInterceptor() {
        return new MappedInterceptor("/content/thumbnails/**", "/content/thumbnails/public/**", thumbnailViewRequestInterceptor);
    }
}


@Component
public class ThumbnailViewRequestInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private UserService userService;

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception arg3) throws Exception {
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model) throws Exception {
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        final boolean allowAccess = someOperationOnRequestObject(request.getSession(false), request.getRequestURL());
        return allowAccess;
    }
}

Below is my apache conf settings:

<VirtualHost *:80>

      Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

        <Proxy "balancer://cluster">
                 BalancerMember http://10.76.2.72:8081/ keepalive=on retry=20 route=1
                 BalancerMember http://10.76.2.72:8082/ keepalive=on retry=20 route=2
           ProxySet stickysession=ROUTEID
        </Proxy>
        <Location /balancer-manager>
                SetHandler balancer-manager
        </Location>
        ProxyPass /balancer-manager !
        ProxyPass / "balancer://cluster/" maxattempts=6 timeout=60
        ProxyPassReverse / "balancer://cluster"

</VirtualHost>

Any headers on what am I missing?


Solution

  • Found the issue in memcache configuration where static resources were being skipped from session backup

    **MemcachedBackupSessionManager manager = new MemcachedBackupSessionManager();
    manager.setRequestUriIgnorePattern(".*\\.(ico|png|gif|jpg|css|js)$");**
    

    This was never setting session info in my httprequest