环境:junit-5、Spring5.0.x、Spring Boot 2.0.x

以下是用来权限测试的接口:


  1. @ApiOperation("[可接入]分页查询管理员")
  2. @ApiResponses({@ApiResponse(code = 200, message = "访问成功", response = APIResponse.class),
  3. @ApiResponse(code = 201, message = "data", response = BackPageManagerDTO.class)})
  4. @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true, defaultValue = "1"),
  5. @ApiImplicitParam(name = "size", value = "数目", required = true, defaultValue = "15")})
  6. @GetMapping("/page")
  7. @RequiresPermissions(PermissionConst.MANAGER)
  8. 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身份去测试接口。个人案例如下:


  1. @SpringBootTest(classes = BackendApplication.class)
  2. @AutoConfigureMockMvc
  3. @SpringJUnitConfig
  4. @PropertySource(value = "classpath:jdbc.properties", encoding = "UTF-8")
  5. @ImportResource(locations = {"classpath:*-config.xml"})
  6. @WebAppConfiguration
  7. class ManagerTest {
  8. @Resource
  9. private BackManagerController managerController;
  10. @Resource
  11. private SecurityManager securityManager;
  12. @Resource
  13. private WebApplicationContext webApplicationContext;
  14. @Resource
  15. private SessionDAO sessionDAO;
  16. private Subject subject;
  17. private MockMvc mockMvc;
  18. private MockHttpServletRequest mockHttpServletRequest;
  19. private MockHttpServletResponse mockHttpServletResponse;
  20. private void login(String username, String password) {
  21. subject = new WebSubject.Builder(mockHttpServletRequest, mockHttpServletResponse)
  22. .buildWebSubject();
  23. UsernamePasswordToken token = new UsernamePasswordToken(username, password, true);
  24. subject.login(token);
  25. ThreadContext.bind(subject);
  26. }
  27. @BeforeEach
  28. void before() {
  29. mockHttpServletRequest = new MockHttpServletRequest(webApplicationContext.getServletContext());
  30. mockHttpServletResponse = new MockHttpServletResponse();
  31. MockHttpSession mockHttpSession = new MockHttpSession(webApplicationContext.getServletContext());
  32. mockHttpServletRequest.setSession(mockHttpSession);
  33. SecurityUtils.setSecurityManager(securityManager);
  34. mockMvc = MockMvcBuilders
  35. .webAppContextSetup(webApplicationContext)
  36. .build();
  37. login("test112", "111111");
  38. }
  39. @Test
  40. void page() throws Exception {
  41. System.out.println("-------------shiro基本权限测试-------------");
  42. System.out.println("init page result:" +
  43. mockMvc.perform(MockMvcRequestBuilders.get("/back/manager/page?page=1&size=15"))
  44. .andExpect(MockMvcResultMatchers.status().isOk())
  45. .andReturn()
  46. .getResponse()
  47. .getContentAsString());
  48. System.err.println("all session id:" +
  49. sessionDAO.getActiveSessions().stream()
  50. .map(Session::getId)
  51. .reduce((x, y) -> x + "," + y)
  52. .orElse(""));
  53. System.out.println("-------------测试同一用户异地登录将另一session踢出,该过程在CredentialsMatcher进行处理-------------");
  54. login("test112", "111111");
  55. System.out.println("user login again page result:" +
  56. mockMvc.perform(MockMvcRequestBuilders.get("/back/manager/page?page=1&size=15"))
  57. .andExpect(MockMvcResultMatchers.status().isOk())
  58. .andReturn()
  59. .getResponse()
  60. .getContentAsString());
  61. System.err.println("all session id:" +
  62. sessionDAO.getActiveSessions().stream()
  63. .map(Session::getId)
  64. .reduce((x, y) -> x + "," + y)
  65. .orElse(""));
  66. System.out.println("-------------测试登出后权限-------------");
  67. subject.logout();
  68. System.out.println("logout page result:" + mockMvc.perform(MockMvcRequestBuilders.get("/back/manager/page?page=1&size=15"))
  69. .andExpect(MockMvcResultMatchers.status().isOk())
  70. .andReturn()
  71. .getResponse()
  72. .getContentAsString());
  73. }
  74. }

测试结果图(以下测试结果分别是测shiro登录后权限处理、同号只能单处登录、登出后权限处理功能的结果):

原文地址:https://blog.csdn.net/z28126308/article/details/81034769

springboot对shiro进行mock单元测试的更多相关文章

  1. 补习系列(6)- springboot 整合 shiro 一指禅

    目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...

  2. [03] SpringBoot+MyBatis+Shiro搭建杂谈

    0.写在前面的话 一直想能仿公司框架的形式,着手做一个简单的脚手架,一来是带着目标性能更好地学习,接触新的技术,另外自己如果有什么想要实现的简单需求,就可以进行快速开发,主要还是希望能在权限上有所控制 ...

  3. 基于spring-boot的应用程序的单元测试方案

    概述 本文主要介绍如何对基于spring-boot的web应用编写单元测试.集成测试的代码. 此类应用的架构图一般如下所示: 我们项目的程序,对应到上图中的web应用部分.这部分一般分为Control ...

  4. SpringBoot 整合Shiro 一指禅

    目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...

  5. SpringBoot集成Shiro并用MongoDB做Session存储

    之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...

  6. springboot整合shiro应用

    1.Shiro是Apache下的一个开源项目,我们称之为Apache Shiro.它是一个很易用与Java项目的的安全框架,提供了认证.授权.加密.会话管理,与spring Security 一样都是 ...

  7. spring-boot+mybatisPlus+shiro的集成demo 我用了5天

    spring-boot + mybatis-plus + shiro 的集成demo我用了五天 关于shiro框架,我还是从飞机哪里听来的,就连小贱都知道,可我母鸡啊.简单百度了下,结论很好上手,比s ...

  8. springboot 与 shiro 整合 (简洁版)

    前言: 网上有很多springboot 与 shiro 整合的资料,有些确实写得很好, 对学习shiro和springboot 都有很大的帮助. 有些朋友比较省事, 直接转发或者复制粘贴.但是没有经过 ...

  9. SpringBoot项目Shiro的实现(一)

    一.Shiro的简单介绍 Shiro是Apache下的一个开源项目,我们称之谓Apache Shiro,它是一个易用与Java项目的安全框架,提供了认证.授权.加密.会话管理,与Spring Secu ...

随机推荐

  1. 安装springsource-tool-suite插件成功之后找不到spring的处理办法

    最近学习spring,安装springsource-tool-suite插件,成功之后,在help-installation details里面可以找到安装的spring插件,却在window-pre ...

  2. MyEclipse6.5安装SVN插件方法

    MyEclipse6.5安装SVN插件,掌握了几种方法,本节就像大家介绍一下MyEclipse6.5安装SVN插件的三种方法,看完本文你肯定有不少收获,希望本文能教会你更多东西. 一.安装方法: My ...

  3. Leetcode221. Maximal Square最大正方形

    在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 方法一 ...

  4. (5)连续非周期信号的傅里叶变换(频谱) & 周期信号的傅里叶变换

    参考资料:<信号与系统(第二版)> 杨晓非 何丰 从傅里叶级数到傅里叶变换 通过分析连续周期信号的周期与频谱的关系,当周期趋于无穷大的时候,周期信号变成非周期信号.从频谱分析观点来看,当T ...

  5. bzoj 3110 [Zjoi2013]K大数查询——线段树套线段树(标记永久化)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3110 第一道线段树套线段树! 第一道标记永久化! 为什么为什么写了两个半小时啊…… 本想线段 ...

  6. PHP的安全性问题,你能说得上几个?

    一.SQL注入 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序,将(恶意)的SQL命令 ...

  7. Python 爬取高清桌面壁纸

    今天写了一个脚本用来爬取ZOL桌面壁纸网站的高清图片: 链接:http://desk.zol.com.cn/1920x1080/ 本程序只爬了美女板块的图片,若要下载其他板块,只需修改程序中的&quo ...

  8. git图形化

    在windows下安装git中文版客户端并连接gitlab 转载自:https://blog.whsir.com/post-1801.html 下载git Windows客户端 git客户端下载地址: ...

  9. python 发送邮件短信封装

    发送邮件 需要开启163的smtp服务 import smtplib from email.mime.text import MIMEText class MailSender(): def __in ...

  10. [jnhs]hibernate只能创建一张/表不创建表com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'kaihu.t_client_info' doesn't exist和org.hibernate.exception.SQLGrammarException: could not execute statement

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'kaihu.t_client_info' doesn't exist ...