Java用户登录改密注销界面
时间: 2023-07-07 15:17:54 浏览: 55
好的,以下是一个简单的 Java 用户登录、改密、注销界面的示例:
```java
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class UserController {
Map<String, String> users = new HashMap<String, String>();
{
users.put("john", "password123");
users.put("jane", "qwertyuiop");
}
private String currentUser;
@GetMapping("/")
public String index() {
return "index";
}
@PostMapping("/login")
public String login(@RequestParam String username, @RequestParam String password, Model model) {
if (users.containsKey(username) && users.get(username).equals(password)) {
currentUser = username;
model.addAttribute("username", currentUser);
return "home";
} else {
model.addAttribute("error", "Invalid username or password!");
return "index";
}
}
@GetMapping("/logout")
public String logout(Model model) {
currentUser = null;
model.addAttribute("username", currentUser);
return "index";
}
@GetMapping("/change-password")
public String changePassword(Model model) {
model.addAttribute("username", currentUser);
return "change-password";
}
@PostMapping("/change-password")
public String changePassword(@RequestParam String oldPassword, @RequestParam String newPassword, Model model) {
if (users.containsKey(currentUser) && users.get(currentUser).equals(oldPassword)) {
users.put(currentUser, newPassword);
model.addAttribute("success", "Password changed successfully!");
return "change-password";
} else {
model.addAttribute("error", "Invalid password!");
return "change-password";
}
}
}
```
该示例使用 Spring Boot 框架编写,包含了以下功能:
1. 用户登录界面:让用户输入用户名和密码进行登录。
2. 用户登录逻辑:检查用户输入的用户名和密码是否与预定义的用户名和密码匹配。
3. 用户注销功能:清除当前用户信息并返回登录界面。
4. 修改密码界面:让用户输入旧密码和新密码进行修改。
5. 修改密码逻辑:检查用户输入的旧密码是否正确,如果正确则更新用户密码并返回修改密码界面。
对应的 HTML 模板代码需要自行编写,可以使用 Thymeleaf 等模板引擎。