在SpringBoot框架中,实现菜单权限管理是确保系统安全性和易用性的关键步骤。以下,我将详细讲解如何在SpringBoot项目中设置菜单权限管理。
一、准备环境
在开始之前,请确保您已经安装了以下环境:
- Java Development Kit (JDK) 1.8及以上版本
- Spring Boot 2.x
- Maven 3.x
- MySQL数据库
二、创建SpringBoot项目
- 使用Spring Initializr(https://start.spring.io/)创建一个新的Spring Boot项目。
- 选择所需的依赖项,包括Spring Web、Spring Security、MyBatis、MySQL驱动等。
- 下载生成的项目,并使用IDE(如IntelliJ IDEA或Eclipse)导入项目。
三、数据库设计
- 创建一个名为
menu的数据库,并创建以下表:
CREATE TABLE `menu` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`url` VARCHAR(100) DEFAULT NULL,
`parent_id` INT(11) DEFAULT NULL,
`level` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 插入一些示例数据:
INSERT INTO `menu` (`id`, `name`, `url`, `parent_id`, `level`) VALUES
(1, '系统管理', '', NULL, 1),
(2, '用户管理', '/user', 1, 2),
(3, '角色管理', '/role', 1, 2),
(4, '菜单管理', '/menu', 1, 2);
四、配置数据库连接
在application.properties文件中配置数据库连接信息:
spring.datasource.url=jdbc:mysql://localhost:3306/menu?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.model
五、创建实体类
在com.example.demo.model包下创建Menu实体类:
package com.example.demo.model;
public class Menu {
private Integer id;
private String name;
private String url;
private Integer parentId;
private Integer level;
// getter和setter方法
}
六、创建Mapper接口
在com.example.demo.mapper包下创建MenuMapper接口:
package com.example.demo.mapper;
import com.example.demo.model.Menu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface MenuMapper {
List<Menu> selectAll();
}
七、创建Service层
在com.example.demo.service包下创建MenuService接口和实现类:
package com.example.demo.service;
import com.example.demo.model.Menu;
import java.util.List;
public interface MenuService {
List<Menu> selectAll();
}
package com.example.demo.service.impl;
import com.example.demo.mapper.MenuMapper;
import com.example.demo.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MenuServiceImpl implements MenuService {
@Autowired
private MenuMapper menuMapper;
@Override
public List<Menu> selectAll() {
return menuMapper.selectAll();
}
}
八、创建Controller层
在com.example.demo.controller包下创建MenuController类:
package com.example.demo.controller;
import com.example.demo.model.Menu;
import com.example.demo.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class MenuController {
@Autowired
private MenuService menuService;
@GetMapping("/menu")
public List<Menu> getAllMenus() {
return menuService.selectAll();
}
}
九、配置Spring Security
在com.example.demo.config包下创建WebSecurityConfig类,继承WebSecurityConfigurerAdapter:
package com.example.demo.config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/menu").hasAuthority("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("{noop}admin").roles("ADMIN");
}
}
十、启动项目
运行com.example.demo.DemoApplication类,启动SpringBoot项目。
十一、测试
- 访问
http://localhost:8080/menu,您将看到所有菜单信息。 - 在浏览器中输入用户名
admin和密码admin,访问http://localhost:8080/login进行登录。
总结
以上就是在SpringBoot项目中设置菜单权限管理的基本步骤。通过以上配置,您可以实现基本的菜单权限管理功能,确保系统的安全性和易用性。在实际项目中,您可以根据需求进行扩展和优化。
