WebFilter和RewriteConfiguration冲突

时间:2015-09-14 08:25:05

标签: jsf servlet-filters ocpsoft-rewrite

所以我有这个RewriteConfiguration:

@RewriteConfiguration
public class ApplicationConfigurationProvider extends HttpConfigurationProvider {

    /**
     * Set the forwarding rules
     * @param context
     * @return The forwarding rules
     */
    @Override
    public Configuration getConfiguration(ServletContext context) {
        return ConfigurationBuilder.begin()
                .addRule()
                .when(Path.matches("/secure/{path}.xhtml?"))
                .perform(Log.message(Level.INFO, "Server requested path: /secure/{path}"))
                .addRule(Join.path("/login").to("/public/login.xhtml"))
                .perform(Log.message(Level.INFO, "Forwarded: login"))
                .addRule()
                .when(Path.matches("/{path}").andNot(Path.matches("/login")))
                .perform(Log.message(Level.INFO, "Forwarded': {path}"))
                .addRule()
                .when(Path.matches("/{path}"))
                .perform(Forward.to("/secure/{path}.xhtml"))
                ;
    }

    /**
     *
     * @return
     */
    @Override
    public int priority() {
        return 0;
    }

}

这个过滤器:

@WebFilter(filterName = "AuthorizationFilter", urlPatterns = {"/secure/*"})
public class AuthorizationFilter implements Filter {

    /**
     * Function that filters out unauthorized users and returns them to the login page
     * when they try to visit secured pages
     * @param request
     * @param response
     * @param chain
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        AuthorizationBean auth = (AuthorizationBean) req.getSession().getAttribute("authBean");        
        if (auth == null || !auth.isLoggedIn()) {
            // User is not logged in, so redirect to login page.
            HttpServletResponse res = (HttpServletResponse) response;
            res.sendRedirect(req.getContextPath() + "/public/login.xhtml");
        } else {
            // User is logged in, so just continue request.
            chain.doFilter(request, response);
        }
    }

    /**
     *
     */
    @Override
    public void destroy() {
    }

    /**
     *
     * @param fc
     */
    @Override
    public void init(FilterConfig fc) {
    }

}

我遇到的问题是,当我例如访问页面“http://localhost:8080/webapp/profile”时,配置文件是安全的文件,因此有一个页面“/secure/profile.xhtml”,但由于rewriteConfiguration,只是“个人资料”也有效。但问题是WebFiler没有捕获“profile”,它只捕获“http://localhost:8080/webapp/secure/profile.xhtml”。

是否有一种方法可以通过过滤器捕获“安全”中的重写页面?因此,当我访问页面“profile”时,它的处理方式与“/secure/profile.xhtml”相同。

1 个答案:

答案 0 :(得分:4)

当重写过滤器在身份验证过滤器之前运行时,此构造确实会失败,并且重写过滤器会在内部对目标源执行RequestDispatcher#forward()调用。

默认情况下,过滤器仅针对直接请求进行侦听。您需要明确添加FORWARD调度程序,以便让过滤器监听转发的请求。

@WebFilter(
    filterName = "authorizationFilter", 
    urlPatterns = {"/secure/*"},
    dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}
)

web.xml味道:

<filter-mapping>
    <filter-name>authorizationFilter</filter-name>
    <url-pattern>/secure/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>