基于SSM+MVC三层架构实现用户权限管理系统

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 基于SSM+MVC三层架构实现用户权限管理系统

基于SSM+MVC三层架构实现用户权限管理系统

前言

本周为大家带来一个简单的用户权限管理系统,该系统主要分为2个模块,用户管理与角色管理,角色可多选权限,用户可多选角色,逻辑稍微复杂,好了,下面步入主题

用户管理系统简介

系统支持防跳墙,用户不登录不可进入主页

  1. 角色模块,要求实现角色的增删改查,且角色可多选权限,实现复选框权限回显
  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>

搭建项目

以Maven方式创建Web项目

整合SSM

核心源码

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

有任何问题均可在评论区留言或私信我

只求您能留下您宝贵的足迹,等啥呢,都看到这啦,还不来个一键三连嘛==(收藏+关注+评论)==

感谢您的支持,我们下篇见~

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
11 0
|
2月前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
10 0
|
6天前
|
前端开发 JavaScript Java
基于SSM家政预约管理系统的设计与实现
基于SSM家政预约管理系统的设计与实现
13 2
|
6天前
|
JavaScript Java 项目管理
基于SSM大创项目申报管理系统的设计与实现
基于SSM大创项目申报管理系统的设计与实现
16 2
|
6天前
|
JavaScript 小程序 Java
基于SSM大学生宿舍管理系统的设计与实现
基于SSM大学生宿舍管理系统的设计与实现
15 1
|
6天前
|
JavaScript Java 数据库
基于SSM的计算机课程实验管理系统的设计与实现
基于SSM的计算机课程实验管理系统的设计与实现
11 1
|
6天前
|
Java 关系型数据库 MySQL
基于SSM的商品分类管理系统
基于SSM的商品分类管理系统
14 1
|
22天前
|
前端开发 Java PHP
信息系统架构模型(1) MVC
信息系统架构模型(1) MVC
22 0
|
1月前
|
前端开发 JavaScript Java
ssm+vue的汽车站车辆运营管理系统
【4月更文挑战第10天】这是一个展示汽车站车辆运营管理系统基本功能的示例,包括Spring Boot后端接口和Vue.js前端。后端接口`/api/vehicle/list`用于获取所有车辆信息,返回模拟数据如"车辆1"、"车辆2"、"车辆3"。前端使用Vue模板和Axios库调用该接口,显示车辆列表。实际项目需扩展登录、权限控制及车辆 CRUD 操作。
33 5
|
2月前
ssm(Spring+Spring mvc+mybatis)——web.xml
ssm(Spring+Spring mvc+mybatis)——web.xml
13 0