Skip to main content
added 2 characters in body
Source Link
LexSav
  • 123
  • 5

I'm a newbie in servlet API. I have a test task to create a very simple web app with one home page and an authorization form. The requirement is to use filter somewhere in my app.

Given that it's a super simple app, I implemented basic login and registration functions right in my filter (it was a much simpler way for me to do it for me).

Here is the question: can I do this at all according to the best practices? Or should I only leave the cookie check in the filter and move all other functions to separate servlets? Is it necessary?

I think almost all code after line 47 can be moved to separate servlets, just need a confirmation.

P.S. Logic is awful, I know. I'm newbie after all :)

I'm a newbie in servlet API. I have a test task to create a very simple web app with one home page and an authorization form. The requirement is to use filter somewhere in my app.

Given that it's a super simple app, I implemented basic login and registration functions right in my filter (it was much simpler way to do it for me).

Here is the question: can I do this at all according to the best practices? Or should I only leave the cookie check in the filter and move all other functions to separate servlets? Is it necessary?

I think almost all code after line 47 can be moved to separate servlets, just need a confirmation.

I'm a newbie in servlet API. I have a test task to create a very simple web app with one home page and an authorization form. The requirement is to use filter somewhere in my app.

Given that it's a super simple app, I implemented basic login and registration functions right in my filter (it was a much simpler way for me to do it).

Here is the question: can I do this at all according to the best practices? Or should I only leave the cookie check in the filter and move all other functions to separate servlets? Is it necessary?

I think almost all code after line 47 can be moved to separate servlets, just need a confirmation.

P.S. Logic is awful, I know. I'm newbie after all :)

added 432 characters in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
@WebFilter("/") 
public class Filter implements javax.servlet.Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(final ServletRequest request,
                         final ServletResponse response,
                         final FilterChain filterChain)

            throws IOException, ServletException {

        final HttpServletRequest req = (HttpServletRequest) request;
        final HttpServletResponse resp = (HttpServletResponse) response;

        @SuppressWarnings("unchecked")
        final AtomicReference<UserDAO> dao = (AtomicReference<UserDAO>) req.getServletContext().getAttribute("dao");

        final String username = req.getParameter("username");
        final String password = req.getParameter("password");
        final String action = req.getParameter("action");
        final String remember = req.getParameter("remember");

        //Just in case
        final String path = req.getRequestURI().substring(req.getContextPath().length());

        if (path.startsWith("/resources/")) {
            filterChain.doFilter(request, response); // Goes to default servlet.
        }

        final Cookie[] cookies = req.getCookies();

        if (cookies != null) {
            for (Cookie cookie : cookies) {

                if (dao.get().checkUserById(cookie.getName()) &&
                    !cookie.getValue().isEmpty())
                {
                    req.setAttribute("username", dao.get().getUsernameById(cookie.getName()));
                    req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);
                }
            }
        }
    }

        if ((username != null) && (password != null) && (action != null)) {

            req.setAttribute("username", username);

            if (action.equals("registration")) {

                if (!dao.get().checkUserByUsername(username)) {

                    final String id = Integer.toString(dao.get().getSize() + 1);
                    final User user = new User(id, username, password);

                    dao.get().add(user);

                    if (remember != null && remember.equals("on")) {
                        Cookie cookie = getRememberMeCookie(id, username, password);
                        resp.addCookie(cookie);
                    }

                    req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);

                } else {
                    req.getRequestDispatcher("/WEB-INF/view/login-form.jsp?registration").forward(req, resp);
                }

            } else if (action.equals("login")) {

                if (dao.get().checkUserByUsernameAndPassword(username, password)) {

                    String id = dao.get().getIdByUsername(username);

                    if (remember != null && remember.equals("on")) {
                        Cookie cookie = getRememberMeCookie(id, username, password);
                        resp.addCookie(cookie);
                    }

                    req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);

                } else {
                    req.getRequestDispatcher("/WEB-INF/view/login-form.jsp?login").forward(req, resp);
                }
            }

        } else {
            req.getRequestDispatcher("/WEB-INF/view/login-form.jsp").forward(req, resp);
        }
    }

    } else {@Override
      public void req.getRequestDispatcherdestroy("/WEB-INF/view/login-form.jsp").forward(req, resp);{
    }
}

@Override
public void destroy  private String md5Hash(String username, String password) throws NoSuchAlgorithmException {
}

private String md5Hash(      String entryData = username, String+ password)"md5Cookie" throws+ NoSuchAlgorithmExceptionpassword;
 {       MessageDigest m = MessageDigest.getInstance("MD5");

    String entryData = username +byte[] "md5Cookie"data += password;entryData.getBytes();
    MessageDigest    m.update(data,0,data.length);
        BigInteger i = MessageDigestnew BigInteger(1,m.getInstancedigest("MD5"));

    byte[] data = entryData.getBytes();
   return mString.updateformat(data,0"%1$032X",data.length i);
    BigInteger i = new BigInteger(1,m.digest());}

    returnprivate String.formatCookie getRememberMeCookie("%1$032X"String id, iString username, String password);
} {

private Cookie getRememberMeCookie(String id, String username,   String password)md5CookieValue {= null;

    String    try {
            md5CookieValue = null;md5Hash(username, password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

    try {
      final Cookie md5CookieValuecookie = md5Hashnew Cookie(usernameid, passwordmd5CookieValue);
    } catch (NoSuchAlgorithmException e) {
  cookie.setMaxAge(60 * 60 * 24 * e.printStackTrace(15);
    }

    final Cookie cookie = new Cookie(id,return md5CookieValue);cookie;
    cookie.setMaxAge(60 * 60 * 24 * 15);}

    return cookie;
}

}


I I think almost all code after line 47 can be moved to separate servlets, just need a confirmation.

P.S. I know that logic there is awful, I'm only learning :)

@WebFilter("/") 
public class Filter implements javax.servlet.Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(final ServletRequest request,
                     final ServletResponse response,
                     final FilterChain filterChain)

        throws IOException, ServletException {

    final HttpServletRequest req = (HttpServletRequest) request;
    final HttpServletResponse resp = (HttpServletResponse) response;

    @SuppressWarnings("unchecked")
    final AtomicReference<UserDAO> dao = (AtomicReference<UserDAO>) req.getServletContext().getAttribute("dao");

    final String username = req.getParameter("username");
    final String password = req.getParameter("password");
    final String action = req.getParameter("action");
    final String remember = req.getParameter("remember");

    //Just in case
    final String path = req.getRequestURI().substring(req.getContextPath().length());

    if (path.startsWith("/resources/")) {
        filterChain.doFilter(request, response); // Goes to default servlet.
    }

    final Cookie[] cookies = req.getCookies();

    if (cookies != null) {
        for (Cookie cookie : cookies) {

            if (dao.get().checkUserById(cookie.getName()) &&
                !cookie.getValue().isEmpty())
            {
                req.setAttribute("username", dao.get().getUsernameById(cookie.getName()));
                req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);
            }
        }
    }

    if ((username != null) && (password != null) && (action != null)) {

        req.setAttribute("username", username);

        if (action.equals("registration")) {

            if (!dao.get().checkUserByUsername(username)) {

                final String id = Integer.toString(dao.get().getSize() + 1);
                final User user = new User(id, username, password);

                dao.get().add(user);

                if (remember != null && remember.equals("on")) {
                    Cookie cookie = getRememberMeCookie(id, username, password);
                    resp.addCookie(cookie);
                }

                req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);

            } else {
                req.getRequestDispatcher("/WEB-INF/view/login-form.jsp?registration").forward(req, resp);
            }

        } else if (action.equals("login")) {

            if (dao.get().checkUserByUsernameAndPassword(username, password)) {

                String id = dao.get().getIdByUsername(username);

                if (remember != null && remember.equals("on")) {
                    Cookie cookie = getRememberMeCookie(id, username, password);
                    resp.addCookie(cookie);
                }

                req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);

            } else {
                req.getRequestDispatcher("/WEB-INF/view/login-form.jsp?login").forward(req, resp);
            }
        }

    } else {
        req.getRequestDispatcher("/WEB-INF/view/login-form.jsp").forward(req, resp);
    }
}

@Override
public void destroy() {
}

private String md5Hash(String username, String password) throws NoSuchAlgorithmException {

    String entryData = username + "md5Cookie" + password;
    MessageDigest m = MessageDigest.getInstance("MD5");

    byte[] data = entryData.getBytes();
    m.update(data,0,data.length);
    BigInteger i = new BigInteger(1,m.digest());

    return String.format("%1$032X", i);
}

private Cookie getRememberMeCookie(String id, String username, String password) {

    String md5CookieValue = null;

    try {
        md5CookieValue = md5Hash(username, password);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    final Cookie cookie = new Cookie(id, md5CookieValue);
    cookie.setMaxAge(60 * 60 * 24 * 15);

    return cookie;
}

}


I think almost all code after line 47 can be moved to separate servlets, just need a confirmation.

P.S. I know that logic there is awful, I'm only learning :)

@WebFilter("/") 
public class Filter implements javax.servlet.Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(final ServletRequest request,
                         final ServletResponse response,
                         final FilterChain filterChain)

            throws IOException, ServletException {

        final HttpServletRequest req = (HttpServletRequest) request;
        final HttpServletResponse resp = (HttpServletResponse) response;

        @SuppressWarnings("unchecked")
        final AtomicReference<UserDAO> dao = (AtomicReference<UserDAO>) req.getServletContext().getAttribute("dao");

        final String username = req.getParameter("username");
        final String password = req.getParameter("password");
        final String action = req.getParameter("action");
        final String remember = req.getParameter("remember");

        //Just in case
        final String path = req.getRequestURI().substring(req.getContextPath().length());

        if (path.startsWith("/resources/")) {
            filterChain.doFilter(request, response); // Goes to default servlet.
        }

        final Cookie[] cookies = req.getCookies();

        if (cookies != null) {
            for (Cookie cookie : cookies) {

                if (dao.get().checkUserById(cookie.getName()) &&
                    !cookie.getValue().isEmpty())
                {
                    req.setAttribute("username", dao.get().getUsernameById(cookie.getName()));
                    req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);
                }
            }
        }

        if ((username != null) && (password != null) && (action != null)) {

            req.setAttribute("username", username);

            if (action.equals("registration")) {

                if (!dao.get().checkUserByUsername(username)) {

                    final String id = Integer.toString(dao.get().getSize() + 1);
                    final User user = new User(id, username, password);

                    dao.get().add(user);

                    if (remember != null && remember.equals("on")) {
                        Cookie cookie = getRememberMeCookie(id, username, password);
                        resp.addCookie(cookie);
                    }

                    req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);

                } else {
                    req.getRequestDispatcher("/WEB-INF/view/login-form.jsp?registration").forward(req, resp);
                }

            } else if (action.equals("login")) {

                if (dao.get().checkUserByUsernameAndPassword(username, password)) {

                    String id = dao.get().getIdByUsername(username);

                    if (remember != null && remember.equals("on")) {
                        Cookie cookie = getRememberMeCookie(id, username, password);
                        resp.addCookie(cookie);
                    }

                    req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);

                } else {
                    req.getRequestDispatcher("/WEB-INF/view/login-form.jsp?login").forward(req, resp);
                }
            }

        } else {
            req.getRequestDispatcher("/WEB-INF/view/login-form.jsp").forward(req, resp);
        }
    }

    @Override
    public void destroy() {
    }

    private String md5Hash(String username, String password) throws NoSuchAlgorithmException {

        String entryData = username + "md5Cookie" + password;
        MessageDigest m = MessageDigest.getInstance("MD5");

        byte[] data = entryData.getBytes();
        m.update(data,0,data.length);
        BigInteger i = new BigInteger(1,m.digest());

        return String.format("%1$032X", i);
    }

    private Cookie getRememberMeCookie(String id, String username, String password) {

        String md5CookieValue = null;

        try {
            md5CookieValue = md5Hash(username, password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        final Cookie cookie = new Cookie(id, md5CookieValue);
        cookie.setMaxAge(60 * 60 * 24 * 15);

        return cookie;
    }

}

I think almost all code after line 47 can be moved to separate servlets, just need a confirmation.

Source Link
LexSav
  • 123
  • 5

Implementation of login and registration functions in servlet filter

I'm a newbie in servlet API. I have a test task to create a very simple web app with one home page and an authorization form. The requirement is to use filter somewhere in my app.

Given that it's a super simple app, I implemented basic login and registration functions right in my filter (it was much simpler way to do it for me).

Here is the question: can I do this at all according to the best practices? Or should I only leave the cookie check in the filter and move all other functions to separate servlets? Is it necessary?

The filter itself:

@WebFilter("/") 
public class Filter implements javax.servlet.Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(final ServletRequest request,
                     final ServletResponse response,
                     final FilterChain filterChain)

        throws IOException, ServletException {

    final HttpServletRequest req = (HttpServletRequest) request;
    final HttpServletResponse resp = (HttpServletResponse) response;

    @SuppressWarnings("unchecked")
    final AtomicReference<UserDAO> dao = (AtomicReference<UserDAO>) req.getServletContext().getAttribute("dao");

    final String username = req.getParameter("username");
    final String password = req.getParameter("password");
    final String action = req.getParameter("action");
    final String remember = req.getParameter("remember");

    //Just in case
    final String path = req.getRequestURI().substring(req.getContextPath().length());

    if (path.startsWith("/resources/")) {
        filterChain.doFilter(request, response); // Goes to default servlet.
    }

    final Cookie[] cookies = req.getCookies();

    if (cookies != null) {
        for (Cookie cookie : cookies) {

            if (dao.get().checkUserById(cookie.getName()) &&
                !cookie.getValue().isEmpty())
            {
                req.setAttribute("username", dao.get().getUsernameById(cookie.getName()));
                req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);
            }
        }
    }

    if ((username != null) && (password != null) && (action != null)) {

        req.setAttribute("username", username);

        if (action.equals("registration")) {

            if (!dao.get().checkUserByUsername(username)) {

                final String id = Integer.toString(dao.get().getSize() + 1);
                final User user = new User(id, username, password);

                dao.get().add(user);

                if (remember != null && remember.equals("on")) {
                    Cookie cookie = getRememberMeCookie(id, username, password);
                    resp.addCookie(cookie);
                }

                req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);

            } else {
                req.getRequestDispatcher("/WEB-INF/view/login-form.jsp?registration").forward(req, resp);
            }

        } else if (action.equals("login")) {

            if (dao.get().checkUserByUsernameAndPassword(username, password)) {

                String id = dao.get().getIdByUsername(username);

                if (remember != null && remember.equals("on")) {
                    Cookie cookie = getRememberMeCookie(id, username, password);
                    resp.addCookie(cookie);
                }

                req.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(req, resp);

            } else {
                req.getRequestDispatcher("/WEB-INF/view/login-form.jsp?login").forward(req, resp);
            }
        }

    } else {
        req.getRequestDispatcher("/WEB-INF/view/login-form.jsp").forward(req, resp);
    }
}

@Override
public void destroy() {
}

private String md5Hash(String username, String password) throws NoSuchAlgorithmException {

    String entryData = username + "md5Cookie" + password;
    MessageDigest m = MessageDigest.getInstance("MD5");

    byte[] data = entryData.getBytes();
    m.update(data,0,data.length);
    BigInteger i = new BigInteger(1,m.digest());

    return String.format("%1$032X", i);
}

private Cookie getRememberMeCookie(String id, String username, String password) {

    String md5CookieValue = null;

    try {
        md5CookieValue = md5Hash(username, password);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    final Cookie cookie = new Cookie(id, md5CookieValue);
    cookie.setMaxAge(60 * 60 * 24 * 15);

    return cookie;
}

}


I think almost all code after line 47 can be moved to separate servlets, just need a confirmation.

P.S. I know that logic there is awful, I'm only learning :)