基于SSM+MVC三层架构实现用户权限管理系统
【摘要】 基于SSM+MVC三层架构实现用户权限管理系统
基于SSM+MVC三层架构实现用户权限管理系统
前言
本周为大家带来一个简单的用户权限管理系统,该系统主要分为2个模块,用户管理与角色管理,角色可多选权限,用户可多选角色,逻辑稍微复杂,好了,下面步入主题
用户管理系统简介
系统支持防跳墙,用户不登录不可进入主页
- 角色模块,要求实现角色的增删改查,且角色可多选权限,实现复选框权限回显
- 用户模块,要求实现用户的增删改查,实现分页,分页要求实现前5后4,且用户可多选角色,实现复选框角色回显
效果图
数据表准备
t_user用户表
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户名称',
`userpwd` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户密码',
`realname` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户真实姓名',
PRIMARY KEY (`id`)
)
t_role角色表
CREATE TABLE `t_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rolename` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '角色名称',
PRIMARY KEY (`id`)
)
t_user_role用户角色关联表
CREATE TABLE `t_user_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL COMMENT '用户id',
`role_id` int(11) DEFAULT NULL COMMENT '角色id',
PRIMARY KEY (`id`)
)
t_permission权限表
CREATE TABLE `t_permission` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`perm_code` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '权限码',
`perm_name` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '权限名称',
PRIMARY KEY (`id`)
)
t_role_perm角色权限关联表
CREATE TABLE `t_role_perm` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) DEFAULT NULL COMMENT '角色id',
`perm_code` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '权限码',
PRIMARY KEY (`id`)
)
项目结构
Java源码结构
配置文件
前端结构
pom依赖文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SSM-UserPermissionManagerSystem</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- mybatis依赖的坐标 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- java连接mysql的驱动坐标 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
搭建项目
核心源码
RouteController,路由跳转类,主要用于跳转到指定jsp页面并携带数据进行渲染
package com.wanshi.controller;
import com.wanshi.bean.TPermission;
import com.wanshi.bean.TRole;
import com.wanshi.bean.TUser;
import com.wanshi.service.PermissionService;
import com.wanshi.service.RoleService;
import com.wanshi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/route")
public class RouteController {
@Autowired
private PermissionService permissionService;
@Autowired
private RoleService roleService;
@Autowired
private UserService userService;
@RequestMapping("/index")
public String index() {
return "index";
}
@RequestMapping("/addRole")
public String addRole(Model model) {
List<TPermission> list = permissionService.list();
model.addAttribute("permList", list);
return "role/add";
}
@RequestMapping("/addUser")
public String addUser(Model model) {
List<TRole> list = roleService.list();
model.addAttribute("roleList", list);
return "user/add";
}
@RequestMapping("/updateRole")
public String updateRole(HttpServletRequest request, Model model) {
String id = request.getParameter("id");
TRole role = roleService.getByRole(id);
List<TPermission> list = permissionService.list();
List<TPermission> byRolePermList = roleService.getByRolePermList(role.getId());
List<String> permCodes = new ArrayList<String>();
for (TPermission tPermission : byRolePermList) {
permCodes.add(tPermission.getPerm_code());
}
model.addAttribute("permCodes", permCodes);
model.addAttribute("permList", list);
model.addAttribute("role", role);
return "role/edit";
}
@RequestMapping("/updateUser")
public String updateUser(HttpServletRequest request, Model model) {
String id = request.getParameter("id");
TUser user = userService.getByUser(id);
List<TRole> userRoleList = userService.getByRoleList(user.getId());
List<TRole> roleList = roleService.list();
List<Integer> roleIds = new ArrayList<Integer>();
for (TRole tRole : userRoleList) {
roleIds.add(tRole.getId());
}
model.addAttribute("roleIds", roleIds);
model.addAttribute("roleList", roleList);
model.addAttribute("user", user);
return "user/edit";
}
}
RoleController,角色控制器类,用于编写角色的CRUD
package com.wanshi.controller;
import com.wanshi.bean.TRole;
import com.wanshi.service.PermissionService;
import com.wanshi.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleService roleService;
@RequestMapping("/list")
public String list(Model model) {
List<TRole> list = roleService.list();
model.addAttribute("roleList", list);
return "role/list";
}
@RequestMapping("addHandle")
public String addHandle(TRole role, HttpServletRequest request) {
String[] permCodes = request.getParameterValues("permCodes");
roleService.insert(role, permCodes);
return "redirect:/role/list";
}
@RequestMapping("/updateHandle")
public String updateHandle(TRole role, HttpServletRequest request) {
String[] permCodes = request.getParameterValues("permCodes");
roleService.update(role, permCodes);
return "redirect:/role/list";
}
@RequestMapping("/delete")
public String delete(TRole role) {
roleService.delete(role);
return "redirect:/role/list";
}
}
UserController,用户控制器类,用于编写用户的CRUD,分页,以及登录
package com.wanshi.controller;
import com.wanshi.bean.Page;
import com.wanshi.bean.TPermission;
import com.wanshi.bean.TRole;
import com.wanshi.bean.TUser;
import com.wanshi.service.PermissionService;
import com.wanshi.service.RoleService;
import com.wanshi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private PermissionService permissionService;
private static final Integer PageSize = 2;
@RequestMapping("/login")
public String login(TUser user, HttpServletRequest request) {
TUser user1 = userService.exam(user);
if (user1 != null) {
request.getSession().setAttribute("user", user1);
return "redirect:/route/index";
}
return "login";
}
@RequestMapping("/logout")
public String logout(HttpServletRequest request) {
request.getSession().invalidate();
return "login";
}
@RequestMapping("/page")
public String page(HttpServletRequest request, Model model) {
String pageNumb = request.getParameter("pageNumb");
Integer currPage = 1;
if (pageNumb != null && !pageNumb.equals("")) {
currPage = Integer.valueOf(pageNumb);
}
Page<TUser> page = userService.page(currPage, PageSize);
model.addAttribute("pager", page);
return "user/list";
}
@RequestMapping("/addHandle")
public String addHandle(TUser user, HttpServletRequest request) {
String[] roleIds = request.getParameterValues("roleIds");
userService.insert(user, roleIds);
return "redirect:/user/page";
}
@RequestMapping("/updateHandle")
public String updateHandle(TUser user, HttpServletRequest request) {
String[] roleIds = request.getParameterValues("roleIds");
userService.update(user, roleIds);
return "redirect:/user/page";
}
@RequestMapping("/delete")
public String delete(HttpServletRequest request){
String id = request.getParameter("id");
userService.delete(id);
return "redirect:/user/page";
}
@RequestMapping("/detail")
public String detail(HttpServletRequest request, Model model) {
String id = request.getParameter("id");
TUser user = userService.getByUser(id);
List<TRole> roleList = userService.getByRoleList(user.getId());
List<Integer> roleIds = new ArrayList<Integer>();
for (TRole tRole : roleList) {
roleIds.add(tRole.getId());
}
Set<TPermission> permList = permissionService.getDetail(roleIds);
model.addAttribute("user", user);
model.addAttribute("roleList", roleList);
model.addAttribute("permList", permList);
return "user/detail";
}
}
LoginInterceptor拦截器,用于拦截未登录的用户,若未登录,则不可访问主页,达到了安全的效果
package com.wanshi.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//1.对登录,注册,风格直接放行
//获取请求的uri地址
String uri = request.getRequestURI();
if (uri.contains("login")) {
return true;
}
//2.进行session验证
Object user = request.getSession().getAttribute("user");
if (user == null) {
request.getRequestDispatcher("/WEB-INF/pages/login.jsp")
.forward(request, response);
return false;
}
return true;
}
}
结语
项目就到此结束了,若对本项目感兴趣的老铁,可在下方获取完整项目
完整项目链接用户权限管理系统
提取码: hbpj
有任何问题均可在评论区留言或私信我
只求您能留下您宝贵的足迹,等啥呢,都看到这啦,还不来个一键三连嘛==(收藏+关注+评论)==
感谢您的支持,我们下篇见~
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)