gpt4 book ai didi

java - 当用户登录并想要用新密码更改旧密码时如何验证用户

转载 作者:行者123 更新时间:2023-11-30 01:40:49 25 4
gpt4 key购买 nike

我正在创建一个动态Web项目,用户首先注册,然后他可以登录
在LoginServlet中我已经创建了一个用于请求用户的 session
他也可以在这里更改密码我使用了一个文本文件在本地系统中存储用户数据
我的问题是我无法在更改密码servlet中应用逻辑,看看这个servlet,因为我提到无法应用逻辑,我如何验证文本文件中的用户数据,并与旧通行证新密码和重复密码进行比较并更新新密码。

HomePageHtml

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>HomePage</h1>
<a href="Registration.html">Click to Register</a><br>
<a href="LogIn.html">Click to Login</a>
</body>
</html>

changePasswordHtml

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>changePassword</h1>
<form action="changePassword" method="post">
Enter Your OldPassword<input type="password" name="pass"><br>
Enter Your NewPassword<input type="password" name="npass"><br>
Enter Your Password<input type="password" name="rpass"><br>
<input type="submit">
</form>
</body>
</html>

LogInServlet

package com.satateMng.com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
* Servlet implementation class LogInServlet
*/
public class LogInServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LogInServlet() {
super();
System.out.println("In no-arg constr() of LS");
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("In doGet() of LS");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("In doPost() of Ls");
/*
* access user inputs validate if validation succeed,create session and
* store email if validation failed,show error msg
*/
String email = request.getParameter("email");
String pass = request.getParameter("pass");

StringBuilder sb = new StringBuilder();
if (email == null || email.trim().equals(""))
sb.append("Enter Correct email<br/>");
if (pass == null || pass.trim().equals(""))
sb.append("Enter your password<br/>");

PrintWriter pw = response.getWriter();

String msg = sb.toString();
if (!msg.equals("")) {
// validation failed
pw.write("<html><body><h1><b>" + msg + "</b></h1></body></html>");
}
else
{
// success
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(Constants.USERFILE));
String line;
while ((line = br.readLine()) != null)
{
System.out.println("coming line=" + line);
String[] sa=line.split("=");
if (email.equals(sa[0]) && pass.equals(sa[1]))
{
// valid user
// create session
HttpSession session = request.getSession(true);
session.setAttribute(Constants.USER, email);

pw.write("<html><body><h1>Welcome " + email+ " </h1><a href='logout'>LogOut</a><br/><a href='ChangePassword.html'>Change Password</a></body></html>");
return;
}
}
pw.write(
"<html><body><h1><b>Your email/password combination is incorrect!try again..</b></h1></body></html>");
}
catch (IOException e)
{
// TODO: handle exception
e.printStackTrace();
pw.write("<html><body><h1>Error</h1><b>Oops something gone complex" + e.getMessage()
+ "</b></body></html>");
return;
}
finally
{
if (br != null)
br.close();
}
}
}

}

ChangePasswordServlet

package com.satateMng.com;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class ChangePasswordServlet
*/
public class ChangePasswordServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ChangePasswordServlet() {
super();
System.out.println("In no-arg constr() of CP..");
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("In doPost() of CP..");
/*
* get the session for client
* if session doesn't exist,then scold the user
* if session exist,open the file,validate if old password is correct and
* then update the file contents to reflect new password change for email id of user
*
*/
HttpSession session=request.getSession(false);
PrintWriter pw=response.getWriter();
if(session==null)
{
pw.write("<html><body><h1>we dont know who you are..</h1></body></html>");
}
else
{
String email=(String) session.getAttribute(Constants.USER);
String opass=request.getParameter("pass");
String npass=request.getParameter("npass");
String rpass=request.getParameter("rpass");


/*
* validate if email is correct by looking into file
* and then update
*/
BufferedReader br=null;
try
{
br=new BufferedReader(new FileReader(Constants.USERFILE));
String line=br.readLine();
while(line!=null)
{
System.out.println("coming line"+line);
String[] sa=line.split("=");
if(email.equals(sa[0]) || opass.equals(sa[1]))
{
//not able to apply logic what I should write code over here thank you
}

}

}
catch(IOException e)
{
// TODO: handle exception
e.printStackTrace();

}
finally
{
if(br!=null)
br.close();
}
pw.write("<html><body><h1><b>your password has changed successfully..</b></h1></body></html>");
}
}

}

最佳答案

检查用户的电子邮件和旧密码是否正确后,您可以检查npass是否等于rpass,如果是这样,则只需更新密码,否则如果两者不同,则向用户发送消息,密码不相同。即:

if(email.equals(sa[0]) && opass.equals(sa[1]))         
{
//if new password and retype password is same do below
if(npass.equals(rpass)){

//write your update code
BufferedWriter bw = null;
try
{
bw = new BufferedWriter(new FileWriter(Constants.USERFILE));
bw.write(rpass);
bw.newLine();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (bw != null)
bw.close();
}
}
}
else
{
//if new password and retype password is not same
if(!npass.equals(rpass)){
//setting some message
request.setAttribute("passerrormsg2", "Password Mismatch");
//redirect to changePassword.Html

}
//if old pass is not equal
if( !opass.equals(sa[1])){
//set some message
request.setAttribute("passerrormsg1", "Old Password is not correct");
//redirect to changePassword.html
}

}

关于java - 当用户登录并想要用新密码更改旧密码时如何验证用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60077344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com