gpt4 book ai didi

java - Spring @WithMockUser 和 request.IsUserInRole()

转载 作者:行者123 更新时间:2023-11-30 02:46:22 27 4
gpt4 key购买 nike

我正在使用:

request.IsUserInRole("ADMIN")

在我的 Controller 之一中确定对请求的响应。我尝试在测试中模拟请求,例如:

@Mock
private HttpServletRequest httpRequest;

并使用Springs注释@WithMockUser:

@Test
@WithMockUser(roles={"USER, ADMIN"})
public void getAccountsTest() throws Exception {...}

两者都不起作用。

问题 1:如何在 JUnit 测试中模拟 request.IsUserInRole("ADMIN")

问题2:@WithMockUser对请求和request.IsUserInRole("ADMIN")有什么影响?

谢谢并致以诚挚的问候

---- 编辑 ----

“不起作用”意味着我有一个测试方法:

@Test
@WithMockUser(username = "user", roles={"USER"})
public void getAccountsReturnForbiddenTest() throws Exception {
mockMvc.perform(get("/accounts/"))
.andExpect(status().isForbidden());
}

应该返回 403,由于 Controller 的原因不允许:

@RequestMapping(method=RequestMethod.GET)
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<PagedResources<AccountResource>> getAccounts(...){...}

但是请求返回 200,好的。

我正在使用 Spring Boot 1.4.1。

---- 编辑 2 ----

我的 JUnit 测试类:

@Transactional
@ContextConfiguration
public class AccountControllerTestDoc extends AbstractControllerTest {

@InjectMocks
private AccountController accountController;

@Mock
private AccountService accountService;

private String uriBase = "";

@Before
public void setup() {
// Initialize Mockito annotated components
MockitoAnnotations.initMocks(this);
// Prepare the Spring MVC Mock components for standalone testing
setup(accountController);
}

@Test
@WithMockUser(username = "user", roles={"USER"})
public void getAccountsReturnForbiddenTest() throws Exception {
String uri = uriBase + "/accounts";
mockMvc.perform(get(uri))
.andExpect(status().isForbidden());
}

}

使用 AbstractControllerTest:

@WebAppConfiguration
public abstract class AbstractControllerTest extends AbstractTest {

protected MockMvc mockMvc;

@Autowired
protected WebApplicationContext wac;

protected void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}

protected void setup(BaseController controller) {
mockMvc = MockMvcBuilders.standaloneSetup(controller))
.build();
}
}

和抽象测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public abstract class AbstractTest {

protected Logger LOG = LoggerFactory.getLogger(this.getClass());

}

----解决方案----

MockMvc standaloneSetup 不知道 Web 应用程序上下文,因此不知道安全过滤器链。必须使用 Web 应用程序上下文和安全过滤器链设置 MockMvc 才能测试安全方面。

这是我现在正在工作的解决方案,我编辑了 AbstractControllerTest:

@WebAppConfiguration
public abstract class AbstractControllerTest extends AbstractTest {

protected MockMvc mockMvc;

@Autowired
protected WebApplicationContext wac;

@Autowired
FilterChainProxy springSecurityFilterChain;

protected void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.addFilters(springSecurityFilterChain)
.build();
}

protected void setup(BaseController controller) {
mockMvc = MockMvcBuilders.standaloneSetup(controller))
.build();
}
}

最佳答案

您应该在创建MockMvc时添加springSecurityFilterChain

protected MockMvc mockMvc;

@Autowired
protected WebApplicationContext wac;

@Autowired
protected Filter springSecurityFilterChain;

protected void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.addFilters(springSecurityFilterChain)
.build();
}

关于java - Spring @WithMockUser 和 request.IsUserInRole(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40082745/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com