springboot shiro -jwt 单元测试

测试开始

  private MockMvc mvc;
    @Autowired
    private WebApplicationContext context;
    @Resource
    private SecurityManager securityManager;
    private Subject subject;
    private MockHttpServletRequest mockHttpServletRequest;
    private MockHttpServletResponse mockHttpServletResponse;
    private String authentication = "cbc857f882cf8a8a55b6fa5d19c5fbf2b82b1b930f0fda2de5491e54a9c189ea09bc229196ebfa00e9fe759b7acd071756d45d27caf35ac64f0c7b911209d7852eaf93d7d092b059cc6d6fd90ef5e77c1aadb934ee42613f07b17ff716b379641de2ded18ac058e540f48d490867cbc519fe4fead66fdf8be44e473089e939a6db429251f00a81db";


    @Before
    public void setUp() {
        mockHttpServletRequest = new MockHttpServletRequest(context.getServletContext());
        mockHttpServletResponse = new MockHttpServletResponse();
        MockHttpSession mockHttpSession = new MockHttpSession(context.getServletContext());
        mockHttpServletRequest.setSession(mockHttpSession);
        SecurityUtils.setSecurityManager(securityManager);

        mvc = MockMvcBuilders.webAppContextSetup(context).build();
        
        subject = new WebSubject.Builder(mockHttpServletRequest, mockHttpServletResponse)
                .buildWebSubject();
        String decryptToken = EncryptUtil.decryptToken(authentication);
        JwtToken token = new JwtToken(decryptToken);
        subject.login(token);
        ThreadContext.bind(subject);

    }


测试

    @Test
    public void getCurrentUser() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/api/v1/user/getCurrentUser"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(ResultCode.FAIL.code()))
                .andExpect(jsonPath("$.payLoad").isEmpty())
                .andReturn().getResponse().getContentAsString();
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值