springboot学生宿舍管理系统登录功能多角色登录时验证用户名与密码代码步骤
时间: 2023-09-04 19:08:37 浏览: 55
实现多角色登录时验证用户名与密码的代码步骤如下:
1. 创建一个登录页面,包含用户名和密码输入框以及选择角色的下拉框。
2. 在后端编写一个Controller处理登录请求,获取用户输入的用户名、密码和选择的角色。
3. 在Controller中对用户名和密码进行验证,可以使用数据库、缓存或其他方式进行验证。同时,根据角色类型进行不同的登录验证。
4. 如果用户名和密码正确,且角色类型也正确,则将用户信息保存到Session中,表示用户已登录。
5. 如果用户名和密码不正确,或者角色类型不正确,则返回登录页面并提示错误信息。
下面是一个基于Spring Boot的简单示例代码:
```java
@Controller
public class LoginController {
@Autowired
private UserService userService;
@GetMapping("/login")
public String login() {
return "login"; // 返回登录页面
}
@PostMapping("/login")
public String doLogin(String username, String password, String role, HttpSession session) {
User user = userService.findByUsername(username);
if (user != null && user.getPassword().equals(password)) {
if ("admin".equals(role) && user.getRole() == Role.ADMIN) {
session.setAttribute("user", user); // 保存用户信息到Session中
return "redirect:/admin/index"; // 登录成功,跳转到管理员首页
} else if ("student".equals(role) && user.getRole() == Role.STUDENT) {
session.setAttribute("user", user); // 保存用户信息到Session中
return "redirect:/student/index"; // 登录成功,跳转到学生首页
}
}
return "login"; // 登录失败,返回登录页面
}
@GetMapping("/admin/index")
public String adminIndex(HttpSession session) {
User user = (User) session.getAttribute("user");
if (user != null && user.getRole() == Role.ADMIN) {
return "admin/index"; // 已登录,返回管理员首页
} else {
return "redirect:/login"; // 未登录,跳转到登录页面
}
}
@GetMapping("/student/index")
public String studentIndex(HttpSession session) {
User user = (User) session.getAttribute("user");
if (user != null && user.getRole() == Role.STUDENT) {
return "student/index"; // 已登录,返回学生首页
} else {
return "redirect:/login"; // 未登录,跳转到登录页面
}
}
}
```
在上面的示例中,我们使用了Spring Boot提供的依赖注入功能,注入了一个UserService的实例,来访问数据库中的用户信息。在处理登录请求的方法中,我们先根据用户名从数据库中获取用户信息,然后对用户输入的密码进行验证。如果用户名和密码正确,且角色类型也正确,则将用户信息保存到Session中,并根据角色类型跳转到不同的首页。在首页的处理方法中,我们通过Session中的用户信息来判断用户是否已登录,如果已登录且角色正确,则返回对应的首页,否则跳转到登录页面。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)