私は現在、Javaプロジェクトでパスワードを忘れた機能を実装しています。私の方法論は、パスワードのリンクをJavaでリセット
ユーザは「パスワードを忘れました」リンクをクリックします。
パスワードを忘れたページで、システムに登録したメールアドレス
を入力するように求められます。パスワードをリセットしたページリンク付きのメールアドレスを含むメールアドレス。
ユーザーはリンクをクリックし、ページにリダイレクトされます(パスワードをリセットします) ユーザーは新しいパスワードを入力できます。
「パスワードのリセット」ページでは、フィールド「電子メールアドレス」が自動的に入力されます。
無効になっているため変更できません。ユーザーが新しいパスワードを入力すると、データベースのアドレス のメールに関連するフィールドが更新されます。
私のコードでこれを試しましたが、リセットしたパスワードページでパスワードを変更したいユーザーの電子メールIDを取得できませんでした。
MailUtil.java
package com.example.controller;
import java.io.IOException;
import java.security.Security;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import com.example.util.Database;
public class MailUtil {
private static final String USERNAME = "[email protected]";
private static final String PASSWORD = "test";
private static final String SUBJECT = "Reset Password link";
private static final String HOST = "smtp.gmail.com";
private static final String PORT = "465";
String email;
public MailUtil() {
// TODO Auto-generated constructor stub
email = this.email;
}
public boolean sendMail(String to, HttpServletRequest request) throws SQLException, ServletException, IOException{
Connection conn = Database.getConnection();
Statement st = conn.createStatement();
String sql = "select * from login where email = '" + to + "' ";
ResultSet rs = st.executeQuery(sql);
String pass = null;
String firstName = null;
while(rs.next()){
pass = rs.getString("pass");
firstName = rs.getString("firstName");
}
if(pass != null){
setEmailId(to);
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.stmp.user", USERNAME);
// If you want you use TLS
//props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", PASSWORD);
// If you want to use SSL
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String username = USERNAME;
String password = PASSWORD;
return new PasswordAuthentication(username, password);
}
});
String from = USERNAME;
String subject = SUBJECT;
MimeMessage msg = new MimeMessage(session);
try{
msg.setFrom(new InternetAddress(from));
InternetAddress addressTo = new InternetAddress(to);
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
String vmFileContent = "Hello User, <br><br> Please Click <a href='http://192.168.15.159:8080/SampleLogin/new-password.jsp><strong>here</strong></a> to reset your password. <br><br><br> Thanks,<br>ProAmbi Team";
// Send the complete message parts
msg.setContent(vmFileContent,"text/html");
Transport transport = session.getTransport("smtp");
transport.send(msg);
System.out.println("Sent Successfully");
return true;
}catch (Exception exc){
System.out.println(exc);
return false;
}
}else{
//System.out.println("Email is not registered.");
request.setAttribute("errorMessage", "User with this email id doesn't exist.");
return false;
}
}
public String getEmailID() {
return email;
}
public void setEmailId(String email) {
this.email = email;
}
}
そして、私の新しい-password.jsp。
<%
MailUtil mail = new MailUtil();
String email = mail.getEmailID();
System.out.println("---> "+email);
%>
しかし、私は電子メールIDではなくヌル値を取得しました。
私はこの問題を解決したり、これを行うための他の選択肢を手伝ってください。
ですから、インスタンス化新しいMailUtilオブジェクトを作成し、それが魔法のように電子メールIDを含むことを期待しますか? – Gimby
あなたのutilクラスはデフォルトコンストラクタではないので、基本的にインスタンス変数はnullです。値を設定せずにsendMailメソッドを呼び出すと、このインスタンスに値があるとは思えません。 – AxelH
@newuserua_ext、 'to'がnullの場合、データベースは有用なものを返すのではないかと思う;)By方法... SQLインジェクションを見る! – AxelH