Spring Test & Security: How to mock authentication?

后端 未结 7 1537
野性不改
野性不改 2020-11-28 01:27

I was trying to figure out how to unit test if my the URLs of my controllers are properly secured. Just in case someone changes things around and accidentally removes securi

7条回答
  •  孤独总比滥情好
    2020-11-28 02:04

    Here is an example for those who want to Test Spring MockMvc Security Config using Base64 basic authentication.

    String basicDigestHeaderValue = "Basic " + new String(Base64.encodeBase64((":").getBytes()));
    this.mockMvc.perform(get("").header("Authorization", basicDigestHeaderValue).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    

    Maven Dependency

        
            commons-codec
            commons-codec
            1.3
        
    

提交回复
热议问题