基于springboot的图书管理系统权限控制模块: 根据用户角色控制对应功能的访问权限,如管理员可以进行图书和用户管理,普通用户只能进行图书借阅操作
时间: 2024-04-29 12:24:23 浏览: 15
。实现步骤:
1. 定义用户角色和对应权限:管理员和普通用户,管理员有图书和用户管理权限,普通用户只有图书借阅权限。
2. 在数据库中存储用户信息和角色信息,对用户进行角色分配。
3. 使用Spring Security框架进行权限控制,配置角色和对应的访问路径。
4. 根据用户角色进行访问控制,只有具有相应角色的用户才能访问对应的功能页面。
5. 在前端页面中根据用户角色动态显示对应的功能模块,实现权限控制。
6. 对于未经授权的访问,返回相应的错误信息。
示例代码:
1. 定义用户角色和对应权限
```
public enum Role {
ROLE_ADMIN, //管理员
ROLE_USER //普通用户
}
```
2. 在数据库中存储用户信息和角色信息
```
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(100) NOT NULL,
`enabled` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `user_roles` (
`user_id` int(11) NOT NULL,
`role` varchar(50) NOT NULL,
PRIMARY KEY (`user_id`,`role`),
CONSTRAINT `user_roles_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
);
```
3. 使用Spring Security框架进行权限控制
```
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.logout()
.logoutSuccessUrl("/");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username, password, enabled from user where username=?")
.authoritiesByUsernameQuery("select user_id, role from user_roles where user_id=(select id from user where username=?)")
.passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
```
4. 根据用户角色进行访问控制
```
@Controller
@RequestMapping("/admin")
@PreAuthorize("hasRole('ADMIN')")
public class AdminController {
//管理员功能
}
@Controller
@RequestMapping("/user")
@PreAuthorize("hasRole('USER')")
public class UserController {
//普通用户功能
}
```
5. 在前端页面中根据用户角色动态显示对应的功能模块
```
<c:if test="${(pageContext.request.userPrincipal.authorities)!={}} && pageContext.request.userPrincipal.authorities.contains('ROLE_ADMIN')}">
<li><a href="/admin">管理图书</a></li>
<li><a href="/admin/user">管理用户</a></li>
</c:if>
<c:if test="${(pageContext.request.userPrincipal.authorities)!={}} && pageContext.request.userPrincipal.authorities.contains('ROLE_USER')}">
<li><a href="/user">借阅图书</a></li>
</c:if>
```
6. 对于未经授权的访问,返回相应的错误信息
```
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(AccessDeniedException.class)
public String handleAccessDeniedException(AccessDeniedException ex) {
return "error/access-denied";
}
}
```
相关推荐
![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)