首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebFilter与RewriteConfiguration冲突

WebFilter与RewriteConfiguration冲突
EN

Stack Overflow用户
提问于 2015-09-14 08:25:05
回答 1查看 362关注 0票数 2

所以我有一个RewriteConfiguration:

代码语言:javascript
复制
@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;
    }

}

这个过滤器:

代码语言:javascript
复制
@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“配置文件是一个安全的文件时,就会有一个页面”/rewriteConfiguration/profile.xhtml“,但是由于rewriteConfiguration,只是" profile”也能工作。但问题是,WebFiler不捕获“配置文件”--它只捕获"http://localhost:8080/webapp/secure/profile.xhtml“。

有没有一种方法,使重新编写的页面的“安全”也被捕获的过滤器?因此,当我访问页面"profile“时,它的处理方式与"/secure/profile.xhtml”相同。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-14 08:54:02

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

过滤器默认情况下只侦听直接请求。您还需要显式地添加FORWARD调度程序,让过滤器侦听转发的请求。

代码语言:javascript
复制
@WebFilter(
    filterName = "authorizationFilter", 
    urlPatterns = {"/secure/*"},
    dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}
)

或者是web.xml口味的:

代码语言:javascript
复制
<filter-mapping>
    <filter-name>authorizationFilter</filter-name>
    <url-pattern>/secure/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32560255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档