Spring mvc Spring安全测试和MockMvc向REST控制器提供空的自定义UserDetails参数
我正在尝试编写一个集成测试,该测试将访问REST端点并获取特定身份验证用户(我在测试中设置的用户)的数据。我最初使用Spring mvc Spring安全测试和MockMvc向REST控制器提供空的自定义UserDetails参数,spring-mvc,spring-security,spring-test-mvc,spring-security-test,Spring Mvc,Spring Security,Spring Test Mvc,Spring Security Test,我正在尝试编写一个集成测试,该测试将访问REST端点并获取特定身份验证用户(我在测试中设置的用户)的数据。我最初使用mockMvc=webAppContextSetup(wac).apply(springSecurity()).build()尝试我的设置,但由于BeanInstantiationException而一直失败。在的帮助下,我能够通过切换设置来克服这个问题。现在,我已经将设置切换到使用standaloneSetup,我可以调用我的控制器了。但是,无论我做什么,我都无法将我在测试中创建
mockMvc=webAppContextSetup(wac).apply(springSecurity()).build()
尝试我的设置,但由于BeanInstantiationException
而一直失败。在的帮助下,我能够通过切换设置来克服这个问题。现在,我已经将设置切换到使用standaloneSetup
,我可以调用我的控制器了。但是,无论我做什么,我都无法将我在测试中创建的自定义UserDetails对象获取到控制器中调用MockMvc的方法中
我正在使用Spring4.2.2和SpringSecurity 4.0.1
我的测试代码如下所示:
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { RestControllerITConfig.class })
@WebAppConfiguration
public class ControllerIT {
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = standaloneSetup(new Controller())
.setCustomArgumentResolvers(new AuthenticationPrincipalArgumentResolver())
.build();
}
@Test
public void testGetEndpoint() throws Exception {
CustomUserDetails userDetails = new CustomUserDetails("John","Smith", "abc123");
assertNotNull(userDetails);
MvcResult result = mockMvc.perform(
get("/somepath").with(user(userDetails)))
.andExpect(status().isOk())
.andExpect(content().contentType("text/json")));
}
}
@Configuration
@ComponentScan(basePackageClasses = { AuthenticationConfig.class })
public class RestControllerITConfig {
}
@Configuration
@EnableWebSecurity
@ComponentScan("com.my.authentication.package")
public class AuthenticationConfig extends WebSecurityConfigurerAdapter {
// snip
}
@Target({ ElementType.PARAMETER, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AuthenticationPrincipal
public @interface MyUser {
}
@RestController
@RequestMapping("/somepath")
public class Controller {
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<Object> getSomePath(@MyUser CustomUserDetails customUser) {
customUser.getName(); // <== Causes NPE
}
}
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
导入静态org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
导入静态org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
导入静态org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(类={RestControllerITConfig.class})
@WebAppConfiguration
公共类控制器{
私有MockMvc-MockMvc;
@以前
公共作废设置(){
mockMvc=standaloneSetup(新控制器())
.SetCustomArgumentResolver(新的AuthenticationPrincipalArgumentResolver())
.build();
}
@试验
public void testGetEndpoint()引发异常{
CustomUserDetails userDetails=新的CustomUserDetails(“约翰”、“史密斯”、“abc123”);
assertNotNull(用户详细信息);
MVC结果=mockMvc.perform(
获取(“/somepath”)。使用(用户(userDetails)))
.andExpect(状态().isOk())
.andExpect(content().contentType(“text/json”));
}
}
@配置
@ComponentScan(basePackageClasses={AuthenticationConfig.class})
公共类restcontrolleriterconfig{
}
@配置
@启用Web安全性
@ComponentScan(“com.my.authentication.package”)
公共类AuthenticationConfig扩展了WebSecurity配置适配器{
//剪断
}
@目标({ElementType.PARAMETER,ElementType.TYPE})
@保留(RetentionPolicy.RUNTIME)
@记录
@认证主体
公共@interface MyUser{
}
@RestController
@请求映射(“/somepath”)
公共类控制器{
@RequestMapping(value=”“,method=RequestMethod.GET)
公共响应属性getSomePath(@MyUser CustomUserDetails customUser){
customUser.getName();//我终于能够通过将AuthenticationConfig的过滤器显式添加到StandaloneMockMvcBuilder
中来实现这一点。因此,我的设置现在看起来像:
private MockMvc mockMvc;
@Autowired
private MyController controller;
@Autowired
private AuthenticationConfig authConfig;
@Before
public void setUp() {
mockMvc = standaloneSetup(controller)
.setCustomArgumentResolvers(new AuthenticationPrincipalArgumentResolver())
.addFilters(authConfig.getCustomFilter())
.build();
}