Spring boot 设置安全控制器集成测试的身份验证
我无法为rest控制器的集成测试设置身份验证。控制器的方法如下所示:Spring boot 设置安全控制器集成测试的身份验证,spring-boot,spring-security,spring-test,spring-oauth2,spring-mvc-test,Spring Boot,Spring Security,Spring Test,Spring Oauth2,Spring Mvc Test,我无法为rest控制器的集成测试设置身份验证。控制器的方法如下所示: @RestController @RequestMapping(BASE_URL) @RequiredArgsConstructor public class EventController { public static final String BASE_URL = "/api/event"; @PostMapping @PreAuthorize("hasRole('USER')") p
@RestController
@RequestMapping(BASE_URL)
@RequiredArgsConstructor
public class EventController {
public static final String BASE_URL = "/api/event";
@PostMapping
@PreAuthorize("hasRole('USER')")
public void createEvent() {
System.out.println("I am in controller");
}
}
这是我的测试:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class EventControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@BeforeEach
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
@Test
void create() throws Exception {
this.mockMvc.perform(post(EventController.BASE_URL)
.with(authentication(new UsernamePasswordAuthenticationToken(
new MyPrincipal(100, "denis"),
null,
Collections.singletonList(new SimpleGrantedAuthority("USER"))
)))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"));
}
由于状态
401
,我的测试总是失败,因此我的模拟身份验证不起作用。你能告诉我怎么修吗?谢谢您的建议。测试安全请求的最简单方法是使用@WithMockUser(A_角色)
所以你的测试看起来像
import org.junit.jupiter.api.Test;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
导入org.springframework.boot.test.context.SpringBootTest;
导入org.springframework.http.MediaType;
导入org.springframework.security.test.context.support.WithMockUser;
导入org.springframework.test.web.servlet.MockMvc;
导入静态org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
导入静态org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@春靴测试
@AutoConfigureMockMvc
类EventControllerTest{
@自动连线
私有MockMvc-MockMvc;
@试验
@WithMockUser(“用户”)
void create()引发异常{
this.mockMvc.perform(post(EventController.BASE\uURL)
.accept(MediaType.APPLICATION_JSON))
.andExpect(状态().isOk())
.andExpect(content().contentType(“应用程序/json”);
}
}
一些评论:
- 您的测试预期会有结果,因此请采用控制器或测试
@PostMapping
@预授权(“hasRole('ROLE_USER'))
公共响应性createEvent(){
String result=“我在控制器中”;
系统输出打印项次(结果);
返回ResponseEntity.ok().body(结果);
}
您正在执行/测试POST
,因此请确保在安全配置中执行http.csrf().disable()…
或者在测试中提供csrf令牌
this.mockMvc.perform(post(EventController.BASE\uURL)
.with(SecurityMockMvcRequestPostProcessors.csrf())//提供一个csrf令牌
....
“测试安全请求的最简单方法是使用
@WithMockUser(A_角色)
所以你的测试看起来像
import org.junit.jupiter.api.Test;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
导入org.springframework.boot.test.context.SpringBootTest;
导入org.springframework.http.MediaType;
导入org.springframework.security.test.context.support.WithMockUser;
导入org.springframework.test.web.servlet.MockMvc;
导入静态org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
导入静态org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@春靴测试
@AutoConfigureMockMvc
类EventControllerTest{
@自动连线
私有MockMvc-MockMvc;
@试验
@WithMockUser(“用户”)
void create()引发异常{
this.mockMvc.perform(post(EventController.BASE\uURL)
.accept(MediaType.APPLICATION_JSON))
.andExpect(状态().isOk())
.andExpect(content().contentType(“应用程序/json”);
}
}
一些评论:
- 您的测试预期会有结果,因此请采用控制器或测试
@PostMapping
@预授权(“hasRole('ROLE_USER'))
公共响应性createEvent(){
String result=“我在控制器中”;
系统输出打印项次(结果);
返回ResponseEntity.ok().body(结果);
}
您正在执行/测试POST
,因此请确保在安全配置中执行http.csrf().disable()…
或者在测试中提供csrf令牌
this.mockMvc.perform(post(EventController.BASE\uURL)
.with(SecurityMockMvcRequestPostProcessors.csrf())//提供一个csrf令牌
....
“这总是让我感到不安unauthorized@DenisStephanov改进了我的回答相同:/我们正在使用oauth令牌进行身份验证,这会导致此问题吗?@DenisStephanov这确实会产生影响…不完全是我的域,也许这会有帮助这会给我带来永远的问题unauthorized@DenisStephanov改进了我的回答相同:/我们正在使用对于身份验证oauth token,这会导致此问题吗?@DenisStephanov这确实会产生影响…不是我的领域,也许这可以帮助您的应用程序如何进行身份验证?@supungjerathne有基于jwt token的身份验证。身份验证在您的应用程序中如何工作?@supungjerathne有基于jwt token的authentication。