gpt4 book ai didi

java - 处理索引 0 超出长度 0 的范围以验证用户方法

转载 作者:行者123 更新时间:2023-12-01 16:50:47 25 4
gpt4 key购买 nike

我有这种方法可以验证用户尝试登录我在 Tomcat 上托管的 Web 应用程序,如果我输入不存在的用户名,它会给我一个 Index 0 out of bounds for length 0而不是返回 null 并显示无效的登录凭据页面。如果用户名不存在,如何确保它返回 null?

验证用户方法:

 public User validateUser(Login login) {

// SQL statement
String sql = "select * from users where username='" + login.getUsername() + "'";
// Running query
List<User> users = jdbcTemplate.query(sql, new UserMapper());
User user = users.get(0);
// Getting encrypted password
String encodedPassword = user.getPassword();
// Getting login password
String rawPassword = login.getPassword();
// Validating them
boolean isPasswordMatch = passwordEncoder.matches(rawPassword, encodedPassword);
if(isPasswordMatch){
return user;
}
else {
return null;
}
}

Controller :
@RequestMapping(value = "/loginProcess", method = RequestMethod.POST)
public ModelAndView loginProcess(HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("login") Login login) {
ModelAndView mav = null;
// Validating user
User user = userService.validateUser(login);
if (null != user) {
// Setting view
mav = new ModelAndView("loginProcess", "firstname", user.getFirstname());
// Creating session and saving user
HttpSession httpSession = request.getSession(true);
httpSession.setAttribute("User", user);
}
// Returning invalid credentials
return mav = new ModelAndView("loginIncorrect");
}

最佳答案

如果用户的长度为零,则可以返回 null

关于java - 处理索引 0 超出长度 0 的范围以验证用户方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61694061/

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