基于spring boot学生宿舍管理系统登录功能之不同角色判断验证
时间: 2023-07-29 10:10:33 浏览: 63
在学生宿舍管理系统中,不同的用户角色可能有不同的权限和访问限制,因此需要在登录时对用户进行角色判断验证。下面是基于Spring Boot的实现方式:
1. 创建一个User实体类,用于存储用户信息,包括用户名、密码和角色等字段。
2. 创建一个UserService类,用于实现用户登录验证功能。该类需要依赖一个UserRepository接口,用于从数据库中获取用户信息。
3. 在Spring Security配置类中,使用AuthenticationProvider接口实现身份验证逻辑。在该接口的实现类中,通过调用UserService类的方法来验证用户的身份和角色。
4. 在登录页面中,根据用户输入的用户名和密码,调用UserService类的登录验证方法,验证用户身份和角色是否正确。如果验证通过,则将用户信息存储在Session中,并跳转到对应角色的主页面;否则返回登录页面并提示错误信息。
示例代码:
User实体类:
```
@Entity
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String role;
// 省略getter和setter方法
}
```
UserRepository接口:
```
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
```
UserService类:
```
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("用户不存在");
}
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(user.getRole()));
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorities);
}
public User login(String username, String password) {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new RuntimeException("用户不存在");
}
if (!user.getPassword().equals(password)) {
throw new RuntimeException("密码不正确");
}
return user;
}
}
```
Spring Security配置类:
```
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new AuthenticationProvider() {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();
User user = userService.login(username, password);
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(user.getRole()));
return new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), authorities);
}
@Override
public boolean supports(Class<?> aClass) {
return true;
}
});
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("admin")
.antMatchers("/user/**").hasRole("user")
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/index")
.permitAll()
.and()
.logout()
.permitAll();
}
}
```
在登录页面中,根据用户输入的用户名和密码,调用UserService类的login方法进行验证:
```
@Controller
public class LoginController {
@Autowired
private UserService userService;
@GetMapping("/login")
public String login() {
return "login";
}
@PostMapping("/login")
public String doLogin(@RequestParam String username, @RequestParam String password, HttpSession session) {
User user = userService.login(username, password);
session.setAttribute("user", user);
if (user.getRole().equals("admin")) {
return "redirect:/admin/index";
} else {
return "redirect:/user/index";
}
}
}
```
以上是一个基于Spring Boot实现的学生宿舍管理系统登录功能之不同角色判断验证的示例代码,仅供参考。