Spring 集成测试不使用安全配置
Spring Integration testing doesn't use security configuration
我是集成测试的新手,并且按照教程中的方式进行所有操作。
我的安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/ticket/create")
.access("hasAnyAuthority('MANAGER','EMPLOYEE')")
.and().authorizeRequests().antMatchers("/api/**","/ticket/*")
.access("isAuthenticated()")
.and().formLogin().loginPage("/login").failureUrl("/login?error")
.and().formLogin().defaultSuccessUrl("/ticket/all", true)
.usernameParameter("email")
.passwordParameter("password")
.and().csrf().disable();
}
我的所有应用程序都需要登录。
现在我正在尝试 运行 一个简单的测试来查看我的应用程序的主页:/ticket/all
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebAppInit.class, WebMvcConfig.class, WebSecurityConfig.class, HibernateConfig.class, SecurityWebApplicationInitializer.class})
@WebAppConfiguration
public class TicketLoginControllerTest extends TestCase {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testTicketOverviewAll() throws Exception {
mockMvc.perform(get("/ticket/all"))
.andExpect(status().isOk());
}
}
测试通过,即使我没有通过身份验证。
现在我的 url: /ticket/create 被禁止用于角色 ENGINEER
但是当我尝试
@Test
public void testTicketOverviewAll() throws Exception {
mockMvc.perform(get("/ticket/create"))
.andExpect(status().isOk());
}
还可以,登陆了也应该是401 forbidden
我可能在测试配置方面做错了什么,但我不知道具体是什么。任何帮助表示赞赏。
如果我只是 运行 我的服务器并尝试以上所有操作,它就会像我期望的那样工作。
首先我需要说 mockMvc 在@Before 中使用安全配置 .apply(springSecurity())
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
.apply(springSecurity())
.build();
}
然后你的所有请求都需要身份验证,你可以通过添加
.with(user("username").password("pass").roles("list of your roles") 到 get/post 或任何其他方法。
我是集成测试的新手,并且按照教程中的方式进行所有操作。 我的安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/ticket/create")
.access("hasAnyAuthority('MANAGER','EMPLOYEE')")
.and().authorizeRequests().antMatchers("/api/**","/ticket/*")
.access("isAuthenticated()")
.and().formLogin().loginPage("/login").failureUrl("/login?error")
.and().formLogin().defaultSuccessUrl("/ticket/all", true)
.usernameParameter("email")
.passwordParameter("password")
.and().csrf().disable();
}
我的所有应用程序都需要登录。
现在我正在尝试 运行 一个简单的测试来查看我的应用程序的主页:/ticket/all
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebAppInit.class, WebMvcConfig.class, WebSecurityConfig.class, HibernateConfig.class, SecurityWebApplicationInitializer.class})
@WebAppConfiguration
public class TicketLoginControllerTest extends TestCase {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testTicketOverviewAll() throws Exception {
mockMvc.perform(get("/ticket/all"))
.andExpect(status().isOk());
}
}
测试通过,即使我没有通过身份验证。
现在我的 url: /ticket/create 被禁止用于角色 ENGINEER
但是当我尝试
@Test
public void testTicketOverviewAll() throws Exception {
mockMvc.perform(get("/ticket/create"))
.andExpect(status().isOk());
}
还可以,登陆了也应该是401 forbidden
我可能在测试配置方面做错了什么,但我不知道具体是什么。任何帮助表示赞赏。 如果我只是 运行 我的服务器并尝试以上所有操作,它就会像我期望的那样工作。
首先我需要说 mockMvc 在@Before 中使用安全配置 .apply(springSecurity())
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
.apply(springSecurity())
.build();
}
然后你的所有请求都需要身份验证,你可以通过添加 .with(user("username").password("pass").roles("list of your roles") 到 get/post 或任何其他方法。