@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 :)