import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.*;
import com.insecure.model.UserDAO;
import com.insecure.databeans.UserBean;
public class InsecureServlet extends HttpServlet {
private UserDAO userDAO;
// ...
private String login(HttpServletRequest request, HttpServletResponse response) {
List<String> errors = new ArrayList<String>();
request.setAttribute("errors", errors);
      Â
String username = request.getParameter("username");
Stringchar[] password = request.getParameter("password").toCharArray();
     Â
// Basic input validation
if(!username.matches("[\\w]*") || !password.toString().matches("[\\w]*")) {
errors.add("Incorrect user name or password format.");
return "error.jsp";
}
    Â
UserBean dbUser = this.userDAO.lookup(username);
if(!dbUser.checkPassword(password)) {
errors.add("Passwords do not match.");
return "error.jsp";
}
  Â
// Create a cookie that contains the username
Cookie userCookie = new Cookie("username", username);
// Create a cookie that contains the password
Cookie passCookie = new Cookie("password", password);
// Add the cookie information to the response that the client will receive
response.addCookie(userCookie);
response.addCookie(passCookie);
// Clear password char array
Arrays.fill(password, ' ');
return "welcome.jsp";
}
}
|