私は私の1のアクセスをテストしようとしています@RestController
カスタム春のセキュリティ設定によって確保されています。私のユースケースは以下の通りである:A HTTP GET
の/someEndpoint
真偽で固定されていますが、HTTPのPOST
同じエンドポイントへのリクエストが確保されていません。私は私のフロントエンドまたは郵便配達で、アプリケーションとテスト、それを起動したときには正常に動作しています。
今、私はテストを記述しようとしていますMockMvc
セキュリティ設定を持ちます。私はすでにStackOverflowの上の回答の多くを通してそれを作ったが、何も私を助けてくれません。
私のテストのセットアップは、次のようになります。
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = MyController.class)
@WebAppConfiguration
@ContextConfiguration
public class AssessmentControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void init() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.alwaysDo(print())
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
}
// some test methods
}
この設定ですべての私のエンドポイントが固定され、さらにはHTTPをPOST
返す401
の代わりに201
。私はまた、セキュリティのためにデバッグログを有効にし、デバッグログには、テストで使用することを言うdefault configure(HttpSecurity)
と、私はログに私のAntMatchersのいずれかを見つけることができません。
2018-07-04 19:20:02.829 DEBUG 2237 --- [ main] s.s.c.a.w.c.WebSecurityConfigurerAdapter : Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).
2018-07-04 19:20:03.097 DEBUG 2237 --- [ main] edFilterInvocationSecurityMetadataSource : Adding web access control expression 'authenticated', for org.springframework.security.web.util.matcher.AnyRequestMatcher@1
2018-07-04 19:20:03.127 DEBUG 2237 --- [ main] o.s.s.w.a.i.FilterSecurityInterceptor : Validated configuration attributes
2018-07-04 19:20:03.130 DEBUG 2237 --- [ main] o.s.s.w.a.i.FilterSecurityInterceptor : Validated configuration attributes
2018-07-04 19:20:03.161 INFO 2237 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@5a75ec37, org.springframework.security.web.context.SecurityContextPersistenceFilter@3f736a16, org.springframework.security.web.header.HeaderWriterFilter@529c2a9a, org.springframework.security.web.csrf.CsrfFilter@7f93dd4e, org.springframework.security.web.authentication.logout.LogoutFilter@707b1a44, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@26c89563, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@1e0a864d, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@22ebccb9, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@53abfc07, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4aa21f9d, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2c05ff9d, org.springframework.security.web.session.SessionManagementFilter@26bbe604, org.springframework.security.web.access.ExceptionTranslationFilter@4375b013, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@a96d56c]
2018-07-04 19:20:03.236 INFO 2237 --- [ main] o.s.b.t.m.w.SpringBootMockServletContext : Initializing Spring FrameworkServlet ''
2018-07-04 19:20:03.237 INFO 2237 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : FrameworkServlet '': initialization started
それは中に私の具体的な春のセキュリティ設定を使用する一般的なことも可能であるMockMvc
テストまたは私はテスト中に全体Springコンテキストを起動しなければなりませんの@SpringBootTest
?私が使用しています(春ブーツ2.0.3.RELEASEのJava 1.8で)
前もって感謝します!
春ブート2.xでは、もうプロパティとセキュリティの切り替えることはできません。あなたは、あなたのテストのコンテキストに追加する必要がある独自のSecurityConfigurationを記述する必要があります。このセキュリティ設定は、認証なしですべての要求を許可する必要があります。
@Configuration
@EnableWebSecurity
public class TestSecurityConfiguration extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().anyRequest().permitAll();
}
@Override
public void configure(WebSecurity web) throws Exception{
web.debug(true);
}
}
テストクラス注釈:
@ContextConfiguration(classes = { ..., TestSecurityConfiguration.class })
public class MyTests {...