【发布时间】:2017-04-27 16:50:03
【问题描述】:
我有一个 Spring Boot + Spring Security 应用程序,它有几个antMatchers 路径;一些fullyAuthenticated(),一些permitAll()。
我如何编写一个测试来验证SecurityConfiguration 下的我的端点在/api/**(以及最终其他)下是否得到正确保护?
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
//...
.antMatchers("/api/**").fullyAuthenticated()
}
}
使用spring-boot-1.5.2.RELEASE、spring-security-core-4.2.2-release。
澄清1:我想尽可能直接地测试SecurityConfiguration,而不是通过/api/** 端点之一进行传递测试,这些端点可能有自己的@PreAuthorize安全。
澄清2:我想要类似WebSecurityConfigurerAdapterTests的东西。
澄清3:我想在 Spring Security 层 @Autowire 进行测试,最好是 HttpSecurity。
【问题讨论】:
-
@dur:理想情况下是 Unit,但集成会很好。我想测试我的配置,而不是我的端点(我有
MockMvc测试来测试我的端点)。 -
WebSecurityConfigurerAdapterTests是 Spring Security 测试套件的一部分,用于测试框架代码是否有效。所以如果你写了.antMatchers("/api/**").fullyAuthenticated(),它可以验证一个匹配“/api/”的请求会被过滤器拦截。它不能用于一般检查 Spring Security 的配置方式。
标签: spring testing spring-security