如何在MockHttpServletRequest中设置角色?

时间:2015-09-24 22:45:31

标签: java spring-mvc spring-security spring-test

我读过以下主题:

https://stackoverflow.com/a/18487953/2674303

但这有点不是我的变种。

在我需要测试的方法控制器中,我有以下行:

httpServletRequest.isUserInRole("ROLE_OWNER");

如何在MockHttpServletRequest中设置角色?

1 个答案:

答案 0 :(得分:1)

 mockMvc.perform(post("/owner/terminals/edit").principal(principal).with(new RequestPostProcessor() {
            public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
                request.addUserRole("ROLE_OWNER");
                return request;
            }