快点帮我用spring boot做一个好看完整的学生宿舍管理系统登录功能,在同一用户表中实现三个不同角色的登录,登录成功后跳转到对应不同页面
时间: 2024-02-01 14:14:10 浏览: 60
基于SpringBoot的高校宿舍管理系统,分为系统管理员、宿舍管理员、学生三个角色.zip
好的,以下是一个简单的实现:
1. 创建一个Spring Boot项目,添加依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
```
2. 创建一个User实体类,包含id、username、password和role属性。
```
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private String role;
// getters and setters
}
```
3. 创建一个UserRepository接口,继承自CrudRepository,用于操作User实体类。
```
public interface UserRepository extends CrudRepository<User, Long> {
User findByUsername(String username);
}
```
4. 创建一个SecurityConfiguration类,用于配置Spring Security,包含三个角色的登录页面和登录成功后的跳转页面。
```
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/teacher/**").hasRole("TEACHER")
.antMatchers("/student/**").hasRole("STUDENT")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessURL("/", true)
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
```
5. 创建一个UserService类,实现UserDetailsService接口,用于从数据库中获取用户信息。
```
@Service
public class UserService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
AuthorityUtils.createAuthorityList(user.getRole())
);
}
}
```
6. 创建三个Controller类,分别用于不同角色的页面访问。
```
@Controller
@RequestMapping("/admin")
public class AdminController {
@GetMapping("/")
public String index() {
return "admin/index";
}
}
@Controller
@RequestMapping("/teacher")
public class TeacherController {
@GetMapping("/")
public String index() {
return "teacher/index";
}
}
@Controller
@RequestMapping("/student")
public class StudentController {
@GetMapping("/")
public String index() {
return "student/index";
}
}
```
7. 创建一个LoginController类,用于登录页面访问和表单提交。
```
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
}
```
8. 在resources/templates文件夹下创建登录页面(login.html)、管理员页面(admin/index.html)、教师页面(teacher/index.html)和学生页面(student/index.html)。
登录页面:
```
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="/login" method="post">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username" />
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
</div>
<div>
<button type="submit">Login</button>
</div>
</form>
</body>
</html>
```
管理员页面:
```
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Admin Page</title>
</head>
<body>
<h1>Welcome to Admin Page</h1>
<p>You have successfully logged in as an admin.</p>
<a href="/logout">Logout</a>
</body>
</html>
```
教师页面:
```
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Teacher Page</title>
</head>
<body>
<h1>Welcome to Teacher Page</h1>
<p>You have successfully logged in as a teacher.</p>
<a href="/logout">Logout</a>
</body>
</html>
```
学生页面:
```
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Student Page</title>
</head>
<body>
<h1>Welcome to Student Page</h1>
<p>You have successfully logged in as a student.</p>
<a href="/logout">Logout</a>
</body>
</html>
```
9. 运行项目,访问http://localhost:8080/login,可以看到登录页面。输入数据库中存在的用户名和密码,可以登录到不同角色的页面。
以上是一个简单的实现,你可以根据具体需求进行修改和完善。
阅读全文