基于SSM+Shiro+Bootstrap实现用户权限管理系统

简介: 基于SSM+Shiro+Bootstrap实现用户权限管理系统

基于SSM+Shiro+Bootstrap实现用户权限管理系统

引言

本篇博文基于SSM+Shiro实现用户权限管理系统,每位用户只可访问指定的页面,具体需求如下

需求

用户账号的增删改查功能

权限包括: 系统模块操作权限(system),财务模块操作权限(finance),考勤模块操作权限(checkon),

每个用户都可能拥有0或多个权限,在新增和编辑用户的时候,可以多选权限。

系统模块:包括用户账号管理功能。

财务模块:为了开发简化,只要做出静态的页面即可,不要真的有财务模块。

考勤模块:同财务模块。

效果图

在这里插入图片描述

功能细节

  • 技术栈: Maven+SSM+Shiro + Bootstarp
  • 操作者必须登录,且拥有system权限才可以访问system/* 下的所有页面和功能
  • 操作者必须登录,且拥有 finance 权限才可以访问 finance/* 下的所有页面和功能
  • 操作者必须登录,且拥有 checkon 权限才可以访问 checkon/* 下的所有页面和功能
  • 用户的新增、编辑、删除、列表功能,都属于 system/* 下面的页面和功能。
  • 操作者如果未登录,会被踢到登录页面
  • 操作者如果访问需要权限的页面,但没有权限,会被踢到 noauthor 页面
  • 用户新增和编辑页面,必须验证登录密码和二次验证密码相同
  • 用户名必须是唯一的
  • 用户新增的时候必须输入用户名
  • 用户编辑的时候,用户名为只读的
  • 用户编辑的时候,可以不输入密码
  • 用户编辑页面打开的时候,不要回显密码
  • 用户新增的时候必须输入登录密码
  • 用户编辑回显的时候必须回显昵称和拥有权限

分页采用PageHelper插件

数据表准备

用户表:t_shiro_user

CREATE TABLE `t_shiro_user` (
  `noid` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `userpwd` varchar(32) NOT NULL,
  `nickname` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`noid`),
  UNIQUE KEY `uniq_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

权限表:t_shiro_permission

CREATE TABLE `t_shiro_permission` (
  `noid` int(11) NOT NULL AUTO_INCREMENT,
  `permission_code` varchar(32) NOT NULL COMMENT '权限代号',
  `permission_describe` varchar(32) NOT NULL COMMENT '权限描述',
  PRIMARY KEY (`noid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

权限代号是开发的时候需要用的字符串,一般都是英文字符串,比如“system”。

权限描述是给最终软件使用方(操作者)看的,比如 “系统模块操作权限”

权限数据是固定的,这些数据是设计时的,即在开发前就确定的东西,不会在项目运行阶段再变化;如果需要变化就需要重新开发相关的代码。

权限表的数据如下:

INSERT INTO `t_shiro_permission` (`permission_code`, `permission_describe`) VALUES ('system', '系统模块操作权限'); 
INSERT INTO  `t_shiro_permission` (`permission_code`, `permission_describe`) VALUES ('finance', '财务模块操作权限');
INSERT INTO  `t_shiro_permission` (`permission_code`, `permission_describe`) VALUES ('checkon', '考勤模块操作权限');

pom文件

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.13</version>
    </dependency>

    <!-- mybatis 相关的依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.13</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.49</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.8</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
    </dependency>
    <!-- shiro依赖-->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>1.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring</artifactId>
        <version>1.8.0</version>
    </dependency>

    <!--        分页插件-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.1.11</version>
    </dependency>

</dependencies>

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>8087</port>
                <path>/ssm_shiro</path>
                <uriEncoding>UTF-8</uriEncoding>
            </configuration>
        </plugin>
    </plugins>
</build>

项目结构

在这里插入图片描述

核心源码

MyRealm自定义权限类,该类实现了用户认证与授权

@Component
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private ShiroUserMapper shiroUserMapper;
    @Autowired
    private UserPermissionMapper userPermissionMapper;

    /**
     * 自定义授权方法
     * 思路:根据PrincipalCollection对象获取用户名,根据用户名查询对象,
     * 查询该用户在数据表中所对应的权限,并转换为set集合,存入SimpleAuthorizationInfo对象并返回
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取用户名
        String username = (String) principalCollection.getPrimaryPrincipal();
        //根据用户名获取用户对象
        ShiroUser shiroUser = new ShiroUser();
        shiroUser.setUsername(username);
        ShiroUser user = shiroUserMapper.get(shiroUser);
        //用户非空判断
        if (user != null) {
            //获取用户所对应的权限
            UserPermission userPermission = new UserPermission();
            userPermission.setUser_id(user.getNoid());
            List<String> list = userPermissionMapper.list(userPermission);

            //List集合转为Set集合放入授权权限实例对象中
            Set<String> perms = new HashSet<>();
            for (String s : list) {
                perms.add(s);
            }
            SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
            authorizationInfo.addStringPermissions(perms);
            return authorizationInfo;
        }
        return null;
    }

    /**
     * 自定义认证方法,根据AuthenticationToken对象获取用户名,
     * 查询用户名对应的都系,并进行验证是否正确,最后返回AuthenticationInfo对象
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        ShiroUser shiroUser = new ShiroUser();
        shiroUser.setUsername(username);
        ShiroUser user = shiroUserMapper.get(shiroUser);
        if (user != null) {
            // 这一步就执行了对密码的验证
            AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(user.getUsername(),user.getUserpwd()
                    , getName());
            return authcInfo;
        } else {
            return null;
        }
    }
}

核心配置文件applicationContent.xml

<!-- 安全管理,将 myRealm嵌入 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="myRealm"/>
</bean>

<!-- Shiro过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <!-- Shiro的核心安全接口,这个属性是必须的 -->
    <property name="securityManager" ref="securityManager"/>
    <!-- 身份认证失败,则跳转到登录页面的配置 -->
    <property name="loginUrl" value="/user/login"/>
    <!-- 权限认证失败,则跳转到指定页面 -->
    <property name="unauthorizedUrl" value="/system/noauthor"/>
    <!-- Shiro连接约束配置,即过滤链的定义 -->
    <property name="filterChainDefinitions">
        <value>
            <!--                游客身份,表示登录就可以访问-->
            /loginPost=anon
            <!-- user赋值的不登录,无无法访问,直接就是认证失败。-->
            /system/*=user
            <!--   访问以finance打头的接口必须拥有finance权限-->
            /finance/*=perms[finance]
            <!--  访问以checkon打头的接口必须拥有checkon权限-->
            /checkon/*=perms[checkon]
            <!--   退出登录,无需写controller,只写配置及写对应的跳转即可实现退出登录-->
            /logout.action=logout
        </value>
    </property>
</bean>

<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- 开启Shiro注解 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean>

Shiro退出登录

applicationContent.xml

<!--    退出登录,无需写controller,只写配置及写对应的跳转即可实现退出登录-->
/logout.action=logout

通用jsp,inc.jsp

<%--点击该链接,applicationContent.xml中配置的退出登录进行拦截, shiro内部实现退出登录并清空缓存 --%>
<a href="${APP_PATH}/logout.action" ${user == null ? 'style="display: none"':'style="display: inline-block"'}  class="btn btn-danger">退出登录</a>

启动项目命令

mvn clean tomcat7:run

IDEA配置如图

在这里插入图片描述

建议采用DeBug方式启动

结语

至此,基于Shiro+SSM+Bootstrap实现用户权限管理系统到此结束,通过本次项目练习,巩固了Shiro技术的理解,加强对Shiro技术的熟练使用,代码略微有些多,只要自己有耐心,努力,坚持的把本项目做完,相信你的技术一定会有一个质的飞跃,好啦,本周技术分享到此结束

都看到这里啦,确定不点赞嘛

若在本项目中遇到技术难题,可在下方评论区留言或私信我,授人以鱼不如授人以渔

百度网盘地址:链接: https://pan.baidu.com/s/1roBMawtBVkD41KlZmVPrcA 提取码: pmfu

如果你觉得博主写的不错的话,不妨给个一键三连,点击下方小拳头即可一键三连。

感谢你的支持!

相关文章
|
1月前
|
存储 人工智能 Java
ssm637教材管理系统
ssm637教材管理系统
|
1月前
|
存储 安全 前端开发
ssm172旅行社管理系统的设计与实现
ssm172旅行社管理系统的设计与实现
|
15天前
|
前端开发 JavaScript Java
ssm+vue的汽车站车辆运营管理系统
【4月更文挑战第10天】这是一个展示汽车站车辆运营管理系统基本功能的示例,包括Spring Boot后端接口和Vue.js前端。后端接口`/api/vehicle/list`用于获取所有车辆信息,返回模拟数据如"车辆1"、"车辆2"、"车辆3"。前端使用Vue模板和Axios库调用该接口,显示车辆列表。实际项目需扩展登录、权限控制及车辆 CRUD 操作。
29 5
|
1月前
|
存储 安全 前端开发
ssm226基于jsp的快递管理系统的开发
ssm226基于jsp的快递管理系统的开发
|
1月前
|
存储 JavaScript Java
ssm703学生考勤管理系统的设计与实现
ssm703学生考勤管理系统的设计与实现
|
1月前
|
存储 安全 前端开发
ssm274办公自动化管理系统
ssm274办公自动化管理系统
|
1月前
|
存储 Java 关系型数据库
544基于SSM的蛋糕甜品店管理系统的设计与开发
544基于SSM的蛋糕甜品店管理系统的设计与开发
|
1月前
|
存储 安全 前端开发
基于SSM的高校疫情管理系统的设计与实现
基于SSM的高校疫情管理系统的设计与实现
|
1月前
|
Java 关系型数据库 MySQL
基于ssm鲜花销售管理系统
基于ssm鲜花销售管理系统
|
1月前
|
Java 关系型数据库 MySQL
基于ssm流浪动物救助及领养管理系统
基于ssm流浪动物救助及领养管理系统