Java servlet使用过滤器重定向

时间:2015-04-22 20:22:27

标签: java servlets servlet-filters

我有以下问题:

我有一个index.html页面,其中包含登录表单:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="css/css.css" type="text/css" rel="stylesheet"/>
    </head>
    <body>
        <nav>
            <form action="">
                <label for="username">User: </label><input name ="username" type="text">
                <label for="password">Password: </label><input name ="password" type="password">
                <input type="submit" value="Vai">
            </form>
        </nav>

        <section id ="page">

        </section>
    </body>
</html>

我创建了一个名为f2的过滤器,该过滤器应检查用户名是否为“admin”,如果是,则将用户重定向到页面payroll/private/stipendi.html或如果不重定向到页面payroll/public/dipendenti.html

这是我的项目的层次结构(使用netbeans 8.02制作):

Hierarchy

这是我的web.xml文件:

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>f2</filter-name>
        <filter-class>f2</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>f2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

这是f2过滤器:

public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        if (debug) {
            log("f2:doFilter()");
        }

        doBeforeProcessing(request, response);
        HttpServletRequest req = (HttpServletRequest) request;
        if(req.getSession().getAttribute("username") == null)
            System.out.println("Attributo username = NULL");
        if(!req.getParameter("username").equals("admin")){
            System.out.println("Username is not ADMIN");
            req.getRequestDispatcher("/payroll/public/dipendenti.html").forward(request, response);
        }
        else{
            System.out.println("Username is ADMIN");
            req.getRequestDispatcher("/payroll/private/stipendi.html").forward(request, response);
        }
        Throwable problem = null;
        try {
            chain.doFilter(request, response); return;
        } catch (Throwable t) {
        // If an exception is thrown somewhere down the filter chain,
            // we still want to execute our after processing, and then
            // rethrow the problem after that.
            problem = t;
            t.printStackTrace();
        }

        doAfterProcessing(request, response);

    // If there was a problem, we want to rethrow it if it is
        // a known type, otherwise log it.
        if (problem != null) {
            if (problem instanceof ServletException) {
                throw (ServletException) problem;
            }
            if (problem instanceof IOException) {
                throw (IOException) problem;
            }
            sendProcessingError(problem, response);
        }
    }

我重温了一些事情:

我有一个无限循环,因为我的过滤器f2url-pattern = /*所以它捕获每个请求,详细说明,发送它并重新捕获刚刚发送的相同请求。一遍又一遍地。

这个男士我必须把我的url-pattern换成其他东西。但是什么?如果我在不创建任何servlet的情况下创建一个名为...的servlet,让我们说myRedirectServlet.javaindex.htmlaction = "myRedirectServlet",那该怎么办? 我道歉但我很困惑。

请帮帮我

1 个答案:

答案 0 :(得分:0)

您所做的不是为了安全。你应该使用principals的概念 - 但是让我们将其保存一天。

  1. 您的HTML中的表单操作缺失 - 因此当您点击提交时,浏览器会尝试重定向到同一个HTML页面。
  2. 假设您很好 - 将过滤器映射更改为仅映射到该HTML页面。
  3. 在过滤器内部 - 如果用户ID为EMPTY - 只需doChain(不要重定向),如果用户ID是必要的管理员重定向,因为您的过滤器现在是特定的 - 您将无法获得进入你的无限循环。