Spring 安全 x509 测试未按预期运行
Spring security x509 tests are not working as I expected
我有一个简单的应用程序在执行相互 TLS。实际上,当 运行 应用程序时,一切都如我所料。
但是下面的 test 不起作用,我想了解为什么,因为它似乎进入了安全链,但是 truststore 配置似乎完全被忽略了。
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@ContextConfiguration
@WebMvcTest
public class ConfigurationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@Before
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
public void untrustedClientShouldBeForbidden() throws Exception {
this.mockMvc.perform(get("/v1/load")
.with(x509(getCertificateFromFile("src/test/resources/untrusted-cert.pem")))
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("{\"foo\":\"bar\"}"))
.andDo(print())
.andExpect(status().is(HttpStatus.FORBIDDEN.value()));
}
我的安全配置很简单,如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
//disable crsf and sessions for this web app
http.httpBasic().disable().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and().x509()
.subjectPrincipalRegex("CN=(.*?)(?:,|$)")
.userDetailsService(userDetailsService());
}
我的测试配置(在进行手动测试时有效)如下所示(应用程序-test.properties):
server.ssl.trust-store=src/test/resources/test.truststore
server.ssl.trust-store-password=changeit
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
编辑:重命名单元测试函数以更好地传达测试的意图。
原来我的测试 class 没有正确注释。
在我的主要配置上 class 我更改了:
@SpringBootApplication
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:application-${spring.profiles.active}.properties")})
@ComponentScan("foo.bar.blah")
public class Application {
收件人:
@SpringBootApplication(scanBasePackages="foo.bar.blah")
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:application-${spring.profiles.active}.properties")})
public class Application {
并且在我的测试中 class 我停止使用 @MockWebMvc 以支持此配置:
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@ContextConfiguration
@SpringBootTest
@AutoConfigureMockMvc
public class ConfigurationTest {
现在配置正确,单元测试都按我预期的方式运行。
我有一个简单的应用程序在执行相互 TLS。实际上,当 运行 应用程序时,一切都如我所料。
但是下面的 test 不起作用,我想了解为什么,因为它似乎进入了安全链,但是 truststore 配置似乎完全被忽略了。
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@ContextConfiguration
@WebMvcTest
public class ConfigurationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@Before
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
public void untrustedClientShouldBeForbidden() throws Exception {
this.mockMvc.perform(get("/v1/load")
.with(x509(getCertificateFromFile("src/test/resources/untrusted-cert.pem")))
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("{\"foo\":\"bar\"}"))
.andDo(print())
.andExpect(status().is(HttpStatus.FORBIDDEN.value()));
}
我的安全配置很简单,如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
//disable crsf and sessions for this web app
http.httpBasic().disable().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and().x509()
.subjectPrincipalRegex("CN=(.*?)(?:,|$)")
.userDetailsService(userDetailsService());
}
我的测试配置(在进行手动测试时有效)如下所示(应用程序-test.properties):
server.ssl.trust-store=src/test/resources/test.truststore
server.ssl.trust-store-password=changeit
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
编辑:重命名单元测试函数以更好地传达测试的意图。
原来我的测试 class 没有正确注释。
在我的主要配置上 class 我更改了:
@SpringBootApplication
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:application-${spring.profiles.active}.properties")})
@ComponentScan("foo.bar.blah")
public class Application {
收件人:
@SpringBootApplication(scanBasePackages="foo.bar.blah")
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:application-${spring.profiles.active}.properties")})
public class Application {
并且在我的测试中 class 我停止使用 @MockWebMvc 以支持此配置:
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@ContextConfiguration
@SpringBootTest
@AutoConfigureMockMvc
public class ConfigurationTest {
现在配置正确,单元测试都按我预期的方式运行。