springboot对shiro进行mock单元测试
环境:junit-5、Spring5.0.x、Spring Boot 2.0.x
以下是用来权限测试的接口:
-
@ApiOperation("[可接入]分页查询管理员")
-
@ApiResponses({@ApiResponse(code = 200, message = "访问成功", response = APIResponse.class),
-
@ApiResponse(code = 201, message = "data", response = BackPageManagerDTO.class)})
-
@ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true, defaultValue = "1"),
-
@ApiImplicitParam(name = "size", value = "数目", required = true, defaultValue = "15")})
-
@GetMapping("/page")
-
@RequiresPermissions(PermissionConst.MANAGER)
-
APIResponse page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15") Integer size);
百度shiro的单元测试,发现没有一个是可以在测试时以指定Subject运行的,最接近的是ThreadContext.bind(securityManager),但这只是绑定了所有SecurityManger,而SecurityManager下还有很多Subject,将ThreadContext.bind(securityManager)改为ThreadContext.bind(subject)即可以指定subject身份去测试接口。个人案例如下:
-
@SpringBootTest(classes = BackendApplication.class)
-
@AutoConfigureMockMvc
-
@SpringJUnitConfig
-
@PropertySource(value = "classpath:jdbc.properties", encoding = "UTF-8")
-
@ImportResource(locations = {"classpath:*-config.xml"})
-
@WebAppConfiguration
-
class ManagerTest {
-
@Resource
-
private BackManagerController managerController;
-
@Resource
-
private SecurityManager securityManager;
-
@Resource
-
private WebApplicationContext webApplicationContext;
-
@Resource
-
private SessionDAO sessionDAO;
-
private Subject subject;
-
private MockMvc mockMvc;
-
private MockHttpServletRequest mockHttpServletRequest;
-
private MockHttpServletResponse mockHttpServletResponse;
-
-
-
private void login(String username, String password) {
-
subject = new WebSubject.Builder(mockHttpServletRequest, mockHttpServletResponse)
-
.buildWebSubject();
-
UsernamePasswordToken token = new UsernamePasswordToken(username, password, true);
-
subject.login(token);
-
ThreadContext.bind(subject);
-
}
-
-
@BeforeEach
-
void before() {
-
mockHttpServletRequest = new MockHttpServletRequest(webApplicationContext.getServletContext());
-
mockHttpServletResponse = new MockHttpServletResponse();
-
MockHttpSession mockHttpSession = new MockHttpSession(webApplicationContext.getServletContext());
-
mockHttpServletRequest.setSession(mockHttpSession);
-
SecurityUtils.setSecurityManager(securityManager);
-
mockMvc = MockMvcBuilders
-
.webAppContextSetup(webApplicationContext)
-
.build();
-
login("test112", "111111");
-
}
-
-
@Test
-
void page() throws Exception {
-
System.out.println("-------------shiro基本权限测试-------------");
-
System.out.println("init page result:" +
-
mockMvc.perform(MockMvcRequestBuilders.get("/back/manager/page?page=1&size=15"))
-
.andExpect(MockMvcResultMatchers.status().isOk())
-
.andReturn()
-
.getResponse()
-
.getContentAsString());
-
System.err.println("all session id:" +
-
sessionDAO.getActiveSessions().stream()
-
.map(Session::getId)
-
.reduce((x, y) -> x + "," + y)
-
.orElse(""));
-
System.out.println("-------------测试同一用户异地登录将另一session踢出,该过程在CredentialsMatcher进行处理-------------");
-
login("test112", "111111");
-
System.out.println("user login again page result:" +
-
mockMvc.perform(MockMvcRequestBuilders.get("/back/manager/page?page=1&size=15"))
-
.andExpect(MockMvcResultMatchers.status().isOk())
-
.andReturn()
-
.getResponse()
-
.getContentAsString());
-
System.err.println("all session id:" +
-
sessionDAO.getActiveSessions().stream()
-
.map(Session::getId)
-
.reduce((x, y) -> x + "," + y)
-
.orElse(""));
-
System.out.println("-------------测试登出后权限-------------");
-
subject.logout();
-
System.out.println("logout page result:" + mockMvc.perform(MockMvcRequestBuilders.get("/back/manager/page?page=1&size=15"))
-
.andExpect(MockMvcResultMatchers.status().isOk())
-
.andReturn()
-
.getResponse()
-
.getContentAsString());
-
}
-
}
测试结果图(以下测试结果分别是测shiro登录后权限处理、同号只能单处登录、登出后权限处理功能的结果):
原文地址:https://blog.csdn.net/z28126308/article/details/81034769
springboot对shiro进行mock单元测试的更多相关文章
- 补习系列(6)- springboot 整合 shiro 一指禅
目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...
- [03] SpringBoot+MyBatis+Shiro搭建杂谈
0.写在前面的话 一直想能仿公司框架的形式,着手做一个简单的脚手架,一来是带着目标性能更好地学习,接触新的技术,另外自己如果有什么想要实现的简单需求,就可以进行快速开发,主要还是希望能在权限上有所控制 ...
- 基于spring-boot的应用程序的单元测试方案
概述 本文主要介绍如何对基于spring-boot的web应用编写单元测试.集成测试的代码. 此类应用的架构图一般如下所示: 我们项目的程序,对应到上图中的web应用部分.这部分一般分为Control ...
- SpringBoot 整合Shiro 一指禅
目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...
- SpringBoot集成Shiro并用MongoDB做Session存储
之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...
- springboot整合shiro应用
1.Shiro是Apache下的一个开源项目,我们称之为Apache Shiro.它是一个很易用与Java项目的的安全框架,提供了认证.授权.加密.会话管理,与spring Security 一样都是 ...
- spring-boot+mybatisPlus+shiro的集成demo 我用了5天
spring-boot + mybatis-plus + shiro 的集成demo我用了五天 关于shiro框架,我还是从飞机哪里听来的,就连小贱都知道,可我母鸡啊.简单百度了下,结论很好上手,比s ...
- springboot 与 shiro 整合 (简洁版)
前言: 网上有很多springboot 与 shiro 整合的资料,有些确实写得很好, 对学习shiro和springboot 都有很大的帮助. 有些朋友比较省事, 直接转发或者复制粘贴.但是没有经过 ...
- SpringBoot项目Shiro的实现(一)
一.Shiro的简单介绍 Shiro是Apache下的一个开源项目,我们称之谓Apache Shiro,它是一个易用与Java项目的安全框架,提供了认证.授权.加密.会话管理,与Spring Secu ...
随机推荐
- Gym - 102163M
Gym - 102163M https://vjudge.net/problem/2356949/origin取对数,然后特判特殊情况,就是0的那些情况 #include<iostream> ...
- Django项目:CRM(客户关系管理系统)--27--19PerfectCRM实现King_admin数据修改
登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html {#table_data_list.html#} {## ————————08PerfectCR ...
- bzoj 1059 [ZJOI2007]矩阵游戏——匈牙利算法
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1059 发现不管怎么换,同一列的元素还是在同一列,同一行的元素还是在同一行. 所以必要条件是每 ...
- MAC中已有的虚拟环境在pycharm 中进行调用
首先确定虚拟环境的路径: 打开工作环境配置文件,找到工作目录: 在pycharm中解释器中找到工作目录中对应的虚拟环境进行配置就可以了
- TZ_11_Spring-Boot的入门
1.SpringBoot的特点 为基于Spring的开发提供更快的入门体验 开箱即用,没有代码生成,也无需XML配置.同时也可以修改默认值来满足特定的需求 提供了一些大型项目中常见的非功能性特性,如嵌 ...
- c#多线程通信之委托(事件)
在研究c# 线程之间通信时,发现传统的方法大概有三种 ①全局变量,由于同一进程下的多个进程之间共享数据空间,所以使用全局变量是最简单的方法,但要记住使用volatile进行限制. ②线程之间发送消息( ...
- ThinkPHP中实现微信支付(jsapi支付)流程
https://blog.csdn.net/sinat_35861727/article/details/72783988 之前写过一篇文章讲了 PHP实现微信支付(jsapi支付)流程 ,详见文章: ...
- LintCode_41 最大子数组
题目 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1] ...
- 笔记本最小安装centos7 连接WiFi的方法
1.首先下载iw工具. yum -y install iw 2.获取无线网卡的名称 执行iw dev,假设获得名称为 wlp3s0(示例) 3.激活无线网络接口 执行ip link set wlp3s ...
- hystrix熔断器
在一个具有多服务的应用中,假如由于其中某一个服务出现问题,导致响应速度变慢,或是根本没有响应返回,会导致它的服务消费者由于长时间的等待,消耗尽线程,进而影响到对其他服务的线程调用,进而会转变为整个应用 ...