Springboot整合Shiro

本文详细介绍了如何在SpringBoot项目中整合Shiro进行用户认证和权限管理,包括创建项目、引入依赖、配置文件修改、实体类、DAO层、Service层、Realm的编写、Controller层的构建、Shiro配置类、登录页面及权限不足时的处理。同时,通过MyHandler实现了未登录时返回JSON数据,并将Shiro权限对象缓存到Redis中。

目录

1.springboot整合shiro-----认证(登录)

1.1.创建springboot项目​编辑

 1.2.引依赖

1.3.修改application配置文件

1.4.创建实体类

1.5.创建dao层

开启扫描dao包

 1.6.创建service层

1.7.编写realm

1.8.创建vo层

1.9.创建Controller层 

1.10. 创建shiro的配置类

1.11.创建登录页面与登录成功页面

1.12.测试

  2.权限不足时跳转到一个页面

创建MyHandler

  2.1前后端分离,把Controller里的返回改成Result类型(200,"查询成功",null)

 2.2.未登录时---返回json数据

3.shiro权限对象缓存到redis中 

4 结构图


1.springboot整合shiro-----认证(登录)

1.1.创建springboot项目

 1.2.引依赖

 <dependencies>
        <!--shiro和redis整合的依赖-->
        <dependency>
            <groupId>org.crazycake</groupId>
            <artifactId>shiro-redis</artifactId>
            <version>3.3.1</version>
        </dependency>

        <!--shrio和thymeleaf集成的扩展依赖,为了能在页面上使用xsln:shrio的标签 -->
        <dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
        </dependency>
<!--        fastson,解决未登录时返回JSON数据-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>

        <!--shiro和springboot整合的依赖-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring-boot-starter</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

1.3.修改application配置文件

#mysql连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///aa?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=980412

#日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

mybatis-plus.mapper-locations=classpath:/mapper/*.xml

#thymeleaf   视图解析器前缀后缀 prefix suffix
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html


#hashAlgorithmName编码方式,hashIterations编码次数,loginUrl放行路径,map放行规则
shiro.hashAlgorithmName=MD5
shiro.hashIterations=1024
shiro.loginUrl=/login.html
shiro.map[/login]=anon
shiro.map[/doc.html]=anon
shiro.map[/webjars/**]=anon
shiro.map[/swagger-resources/**]=anon
shiro.map[/v2/**]=anon
shiro.map[/**]=authc

1.4.创建实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Permission {
    @TableId(type = IdType.AUTO)//名字不叫id的需要加这个注解

    private Integer perid;
    private String pername;
    private String percode;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(type = IdType.AUTO,value = "userid")//名字不叫id的需要加这个注解,数据库叫userid
    private Integer id;
    private String username;
    private String userpwd;
    private String sex;
    private String address;
    private String salt;
}

1.5.创建dao层

@Repository

public interface PermissionDao extends BaseMapper<Permission> {
    List<Permissi
课程简介:历经半个多月的时间,Debug亲自撸的 “企业员工角色权限管理平台” 终于完成了。正如字面意思,本课程讲解的是一个真正意义上的、企业级的项目实战,主要介绍了企业级应用系统中后端应用权限的管理,其中主要涵盖了六大核心业务模块、十几张数据库表。 其中的核心业务模块主要包括用户模块、部门模块、岗位模块、角色模块、菜单模块和系统日志模块;与此同时,Debug还亲自撸了额外的附属模块,包括字典管理模块、商品分类模块以及考勤管理模块等等,主要是为了更好地巩固相应的技术栈以及企业应用系统业务模块的开发流程! 核心技术栈列表: 值得介绍的是,本课程在技术栈层面涵盖了前端和后端的大部分常用技术,包括Spring BootSpring MVC、Mybatis、Mybatis-Plus、Shiro(身份认证与资源授权跟会话等等)、Spring AOP、防止XSS攻击、防止SQL注入攻击、过滤器Filter、验证码Kaptcha、热部署插件Devtools、POI、Vue、LayUI、ElementUI、JQuery、HTML、Bootstrap、Freemarker、一键打包部署运行工具Wagon等等,如下图所示: 课程内容与收益: 总的来说,本课程是一门具有很强实践性质的“项目实战”课程,即“企业应用员工角色权限管理平台”,主要介绍了当前企业级应用系统中员工、部门、岗位、角色、权限、菜单以及其他实体模块的管理;其中,还重点讲解了如何基于Shiro的资源授权实现员工-角色-操作权限、员工-角色-数据权限的管理;在课程的最后,还介绍了如何实现一键打包上传部署运行项目等等。如下图所示为本权限管理平台的数据库设计图: 以下为项目整体的运行效果截图: 值得一提的是,在本课程中,Debug也向各位小伙伴介绍了如何在企业级应用系统业务模块的开发中,前端到后端再到数据库,最后再到服务器的上线部署运行等流程,如下图所示:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值