Spring启动:拦截静态托管资源文件的请求

时间:2017-02-07 16:41:56

标签: spring-boot static-resource

从我的Spring启动应用程序中,我正在托管应用程序外部的文件。通过addResourceHandler()方法完成。

另外,我需要通过检查几个会话属性来验证对这些文件的访问。为此,我添加了一个拦截器。

问题:拦截时,在拦截器的preHandle中,当通过代理路由器apache设置部署在LINUX上时,HttpServletRequest没有任何会话信息(无会话)。但是,当直接从我的IDE在Windows上运行时,HttpServletRequest确实具有正确的会话。

以下是代码:

@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;
    }
}

以下是我的apache conf设置:

<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>

关于我遗失的任何标题?

1 个答案:

答案 0 :(得分:0)

在memcache配置中发现了从会话备份跳过静态资源的问题

action

这从未在我的httprequest

中设置会话信息