/src/java/ForgotPassword.java
https://github.com/aaronmgross/Heinz-Disaster-Response-Mobile-Application · Java · 132 lines · 105 code · 22 blank · 5 comment · 6 complexity · af61a53c9f0e2c75fc8929e31d7491a9 MD5 · raw file
- import com.mysql.jdbc.Connection;
- import java.io.IOException;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import DataDAO.*;
- import java.util.Properties;
- import java.util.Random;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.servlet.RequestDispatcher;
- /**
- *
- * @author Yao
- */
- public class ForgotPassword extends HttpServlet {
- private static Connection con;
- private String user = null;
- private String pw_con = null;
- /*Get the database user and password from config file*/
- @Override
- public void init() throws ServletException {
- user = getInitParameter("dbUser");
- pw_con = getInitParameter("dbPassword");
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String email = request.getParameter("email");
- User u = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- throw new AssertionError(e);
- }
- String connectionStr = "jdbc:mysql://localhost/DisasterAssessment";
- try {
- con = (Connection) DriverManager.getConnection(connectionStr, user, pw_con);
- u = User.lookup(email, con);
- } catch (SQLException ex) {
- Logger.getLogger(ForgotPassword.class.getName()).log(Level.SEVERE, null, ex);
- }
- String message;
- if (u != null) {
- String newPassword = genNewPassword();
- u.setPassword(newPassword);
- try {
- u.update(con, u.getUserId());
- } catch (SQLException ex) {
- System.out.println("failed to update the new password.");
- }
- sendEmail(email, newPassword);
- message = "Your new password has been sent to your email.";
- } else {
- message = "Your email doesn't exsit.";
- }
- request.setAttribute("RegisterMessage", message);
- RequestDispatcher red = getServletContext().getRequestDispatcher("/index.jsp");
- red.forward(request, response);
- }
- private void sendEmail(String toAddress, String newPassword) {
- Properties props = new Properties();
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.socketFactory.port", "465");
- props.put("mail.smtp.socketFactory.class",
- "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.port", "465");
- Session session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("disasterassessment", "disasterassessment");
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress("account_update@redcross.org"));
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(toAddress));
- message.setSubject("Disaster Assessment App Password Assistance");
- message.setText("We received a request to reset the password associated with this e-mail address"
- + "\n\n Your new password is: " + newPassword
- + "\n\n Please log in using the new password and reset it as soon as possible. ");
- Transport.send(message);
- System.out.println("Done");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- private String genNewPassword() {
- char[] symbols = new char[36];
- char[] buf = new char[8];
- Random random = new Random();
- for (int idx = 0; idx < 10; ++idx) {
- symbols[idx] = (char) ('0' + idx);
- }
- for (int idx = 10; idx < 36; ++idx) {
- symbols[idx] = (char) ('a' + idx - 10);
- }
- for (int idx = 0; idx < buf.length; ++idx) {
- buf[idx] = symbols[random.nextInt(symbols.length)];
- }
- return new String(buf);
- }
- }