gpt4 book ai didi

java - Controller 类中的全局变量被打开的最新 session 覆盖

转载 作者:行者123 更新时间:2023-12-02 06:04:49 25 4
gpt4 key购买 nike

我使用 spring-MVC 和 hibernate 开发了一个应用程序,它有注册页面。当用户尝试注册时,应用程序会向用户邮件发送一个 OTP,并且我在 Controller 类中将应用程序发送的这个 OTP 作为全局变量进行维护。因此,这里的问题是,当两个用户同时访问时,最新请求的用户 otp 会覆盖旧的用户,因此第一个用户无法注册。

1 > Spring 是否为每个访问应用程序的用户维护单独的 session ?如果没有?如何解决这个问题?。

请找到下面的代码。

Controller 类:

package com.uday;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ControllerSignUp_Login {

private final Login_DetailsDao dao;
private Login_Details ld = new Login_Details();
private String OtpMailed = "";
private MailSendTest mailSender;
private int chances = 4;
private String emailAdd;

public ControllerSignUp_Login(Login_DetailsDao login_DetailsDao, MailSendTest mailSender) {
this.dao = login_DetailsDao;
this.mailSender = mailSender;
}

@RequestMapping("/hello")
@Transactional
public String diaplay(@RequestParam("name") String name, @RequestParam("pass") String pass, Model m) {
if (dao.isLogoinSuccessfull(name, pass)) {
m.addAttribute("message", "Hello " + name + " You are successfully logged in");
return "Success";
} else {
m.addAttribute("message", "Cannot validate given details.Please try again");
return "login";
}
}

@RequestMapping("/SignUp")
public String redirect() {
System.out.println("ControllerSignUp_Login.display()");
chances = 4;
return "signup";
}

@RequestMapping("/login")
public String display() {
System.out.println("ControllerSignUp_Login.display()");
return "login";
}

@RequestMapping("/updateDetails")
@Transactional
public String display(HttpServletRequest req, Model M) {
String firstName = req.getParameter("firstName");
String lastName = req.getParameter("lastName");
String mobileNo = req.getParameter("mobileNo");
String address = req.getParameter("address");
String email = req.getParameter("email");
String password = req.getParameter("password");
if (checkLength(firstName) && checkLength(lastName) && checkLength(mobileNo) && checkLength(address)
&& checkLength(email) && checkLength(password)) {

ld.setFirstName(firstName);
ld.setLastName(lastName);
ld.setEmail(email);
ld.setAddress(address);
ld.setMobileNo(mobileNo);
ld.setPassword(password);
if (dao.validateMobileAndEmail(mobileNo, email)) {
doSendEmail(email);
M.addAttribute("cMessage", false);
return "ValidationPage";
} else {
M.addAttribute("message", "MobileNo/Email is already registered");
return "signup";
}

} else {
M.addAttribute("message", "SignUp Failed !! All details are mandatory.");
return "signup";

}
}

@RequestMapping("/Home")
public String displayy() {
return "Home";
}

@RequestMapping("/")
public String display1() {
return "login";
}

public boolean checkLength(String s) {
if (s != null && s.length() > 0) {
return true;
}
return false;

}

@Transactional
@RequestMapping("/submitToDB")
public String submitToDataBase(HttpServletRequest req, Model M) {

String otp = req.getParameter("otp");
System.out.println("ControllerSignUp_Login.submitToDataBase()" + otp);
if (OtpMailed.equals(otp)) {
dao.saveEmployee(ld);
chances = 4;
M.addAttribute("message", "SignUp Successfull !! Thank You");
M.addAttribute("displayLogin", true);
return "Success";
} else {
if (chances != 1) {
chances = chances - 1;
M.addAttribute("message", chances + " Chances Left");
return "ValidationPage";
} else {
chances = 4;
M.addAttribute("message", "Authorization failed");
return "signup";
}

}

}

@RequestMapping("/validate")
public String validateOtp() {
return "Success";
}

public String generateOtp() {
String otp = "";
for (int i = 0; i < 4; i++) {
Double d = Math.ceil(Math.random() * 10);
int value = d.intValue();
if (value == 10) {
otp = otp + 1;
} else {
otp = otp + value;
}
}
return otp;

}

public void doSendEmail(String mail) {
try {
this.emailAdd = mail;
String recipientAddress = mail;
String subject = "One Time Verification <Uday>";
String otpGenerated = generateOtp();
this.OtpMailed = otpGenerated;
String message = "Please use this OTP " + otpGenerated + " to signup. ";
mailSender.Send("xxxxxxxxx@gmail.com", "lxrxnxnhmyclvzxs", recipientAddress, subject, message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@RequestMapping("/resend")
public String resend(Model m) {
doSendEmail(this.emailAdd);
m.addAttribute("message", chances + " Chances Left");
return "ValidationPage";
}

}

最佳答案

Spring REST Controller 的作用域始终为单例(@Controller 注释暗示了这一点)。您不应该在方法调用中重复使用私有(private)类级别变量/字段。

如果您有全局问题,需要在单个请求的范围之外进行管理/访问,请务必将它们分成不同的类。

否则,在 @RequestMapping 注解的方法中发生的整个修改范围应该是方法/函数本地的。

关于java - Controller 类中的全局变量被打开的最新 session 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55949044/

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