Springboot Jersey-具有ServletProperties.FILTER_STATIC_CONTENT_REGEX的自定义过滤器

时间:2019-06-10 16:24:56

标签: spring-boot

我有一个运行在Jersey配置行property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, "/mywebpage/.*")上的Springboot应用程序,以启用呈现该特定路径前缀的静态html / js / css / image ...内容。一切正常,mywebpage可以完美地加载到/mywebpage/index.html上。

现在,在运行mywebpage几个月后,我们希望将一定比例的用户(已启用Beta的用户)重定向到新网页(例如https://stackoverflow.com/)。因此,我尝试为路径Filter写一个/mywebpage/index.html,以将用户重定向到新页面(如果已为Beta启用)。现在让我感到困扰的部分是,由于某种原因,对于/mywebpage/.*的任何调用(浏览器为获取html / js / css / ...内容而进行的调用)都没有调用过滤器断点和虚拟日志)。我认为犯罪嫌疑人将是财产ServletProperties.FILTER_STATIC_CONTENT_REGEX

我已经有能力计算用户是否已启用Beta,只需要将其放入Filter中即可。

这就是我正在尝试的:

@Provider
public class RedirectionFilter implements ContainerRequestFilter {

    @Autowired
    private BetaAudienceService betaAudienceService;

    @Context
    private UriInfo info;

    private static final Logger log = LogManager.getLogger(getClass());

    @Override
    public void filter(ContainerRequestContext request) throws IOException {
        log.info("Test Log :: Path=" + request.getUriInfo().getAbsolutePath().getPath());
        if (request.getUriInfo().getAbsolutePath().getPath().equals("/mywebpage/index.html") && isBetaEnabled(request)) {
            try {
                request.abortWith(Response.temporaryRedirect(new URI("https://stackoverflow.com/")).build());
            } catch (URISyntaxException e) {
                throw new IOException("Failed to build or respond with Redirection URI", e);
            }
        }
    }

    private boolean isBetaEnabled(ContainerRequestContext request) { ... }
}

public class JerseyConfig extends ResourceConfig {

    @Autowired
    private ApplicationContext appCtx;

    @PostConstruct
    public void setup() {
        register(A.class);
        register(B.class);
        ...
    }

    public JerseyConfig() {
        property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, Constants.STATIC_CONTENT_PATH);
    }
}

关于如何解决此问题的任何建议?还是我的方法完全错误?

1 个答案:

答案 0 :(得分:0)

我能够弄清楚这一点。

在这种情况下,Jersey设置为以Filter运行(因为我们需要使用ServletProperties.FILTER_STATIC_CONTENT_REGEX),Jersey处理所有服务请求,但Jersey所在的特定路径/mywebpage/*除外配置为忽略由于静态内容筛选器属性的请求。因此,那些被忽略/过滤的请求将由Springboot直接处理,这意味着我们可以只使用Springboot过滤器。

以下是过滤器的代码:

public class RedirectionFilter extends OncePerRequestFilter /* can also just `implements Filter` instead */ {

    public static final String REDIRECT_PATH = "/mywebpage/index.html";
    private static final Logger log = LogManager.getLogger(RedirectionFilter.class);
    private final MyConfig myConfig;

    public UnifiedShellRedirectionFilter(MyConfig myConfig) {
        this.myConfig = myConfig;
    }

    @Override
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        log.info("Test Log :: Path: " + request.getPathInfo());
        if (isBetaEnabled(request)) {
            response.sendRedirect(myConfig.getRedirectionEndpoint() /* "https://stackoverflow.com/" */);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        log.info("Destructing Filter: " + this.getClass().getSimpleName());
    }

    private boolean isBetaEnabled(HttpServletRequest request) { ... }
}

并将以下内容放入您的@Configuration类中:

    @Bean
//    @Autowired
    public FilterRegistrationBean registerRedirectionFilter(MyConfig myConfig) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName(RedirectionFilter.class.getSimpleName());
        registrationBean.setFilter(new UnifiedShellRedirectionFilter(myConfig));
        registrationBean.addUrlPatterns(RedirectionFilter.REDIRECT_PATH);
        return registrationBean;
    }