在控制器测试中找不到自定义安全配置

时间:2019-04-30 08:37:37

标签: unit-testing spring-security

我为控制器的get方法编写测试,并创建用于身份验证的自定义配置,但是在运行test时,生成默认密码并出现此错误:

MockHttpServletRequest:
  HTTP Method = GET
  Request URI = /android-guard-infos/1
   Parameters = {}
      Headers = {}
         Body = <no character encoding set>
Session Attrs = {SPRING_SECURITY_SAVED_REQUEST=DefaultSavedRequest[http://localhost/android-guard-infos/1]}

Handler:
         Type = null

Async:
Async started = false
 Async result = null

Resolved Exception:
         Type = null

ModelAndView:
    View name = null
         View = null
        Model = null

FlashMap:
   Attributes = null

MockHttpServletResponse:
       Status = 401
Error message = Unauthorized
      Headers = {WWW-Authenticate=[Basic realm="Realm"], X-Content-    Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
 Content type = null
         Body = 
Forwarded URL = null
  Redirected URL = null
      Cookies = []

 java.lang.AssertionError: Status 
 Expected :200   
 Actual   :401

at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:82)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:619)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:178)
at ir.fidar.seclab.controllersTest.AndroidGuardInfoControllerTest.getOneTest(AndroidGuardInfoControllerTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我对测试和安全性的依赖是:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
  <version>2.1.1.RELEASE</version>
</dependency>

和我的安全配置类:

@Configuration
@EnableWebSecurity
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private AppUserDetailsService appUserDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(getProvider());

}

@Override
protected void configure(HttpSecurity http) throws Exception {


        http.httpBasic().and().authorizeRequests()
            .antMatchers("/users/**","/roles/**","/privileges/**").hasAuthority("manager")
            .antMatchers("/android-guard-infos/**","process-queues/**",
                    "/risk-android-infos/**","/summery-infos/**",
                    "/virus-total-infos/**","/vt-process-queues/**").permitAll();
}


@Bean
public AppAuthenticationProvider getProvider(){
    AppAuthenticationProvider provider = new AppAuthenticationProvider(appUserDetailsService);
    return provider;
}

}

我的考试班:

@RunWith(SpringRunner.class)
@WebMvcTest(AndroidGuardInfoController.class)
//@Import(AppSecurityConfig.class)
public class AndroidGuardInfoControllerTest  {


@Autowired
private MockMvc mvc;

@MockBean
private AndroidGuardInfoService controller;

@Test
public void getOneTest() throws Exception {
    AndroidGuardInfo data = new AndroidGuardInfo();


    Mockito.when(controller.getOne(1L)).thenReturn(data);
    mvc.perform(MockMvcRequestBuilders.get("/android-guard-infos/1")).andExpect(status().isOk());
}

}

我尝试通过添加@Import(AppSecurityConfig.class)解决此错误,但是 将错误更改为java.lang.IllegalStateException: Failed to load ApplicationContextError creating bean引起。

你能指导我吗?

1 个答案:

答案 0 :(得分:0)

您的@Import(AppSecurityConfig.class)在以下bean依赖项注入失败。预期的。

@Autowired
private AppUserDetailsService appUserDetailsService;

这是因为您尝试访问默认情况下指向src软件包的配置。

为了在test目录(超出/ src范围)中使用相同的配置,则您可以明确指定包扫描 AppSecurityConfig.java中的组件扫描定义,或尝试在使用包定义定义测试级别配置的地方添加特定于测试的配置。

@ComponentScan(basePackages = {
    "YOUR_PACKAGES"
})
@Configuration
@EnableWebSecurity
public class TestAppSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private AppUserDetailsService appUserDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(getProvider());

}

@Override
protected void configure(HttpSecurity http) throws Exception {


        http.httpBasic().and().authorizeRequests()
            .antMatchers("/users/**","/roles/**","/privileges/**").hasAuthority("manager")
            .antMatchers("/android-guard-infos/**","process-queues/**",
                    "/risk-android-infos/**","/summery-infos/**",
                    "/virus-total-infos/**","/vt-process-queues/**").permitAll();
}


@Bean
public AppAuthenticationProvider getProvider(){
    AppAuthenticationProvider provider = new AppAuthenticationProvider(appUserDetailsService);
    return provider;
}
}

然后尝试将其导入测试类中。

@RunWith(SpringRunner.class)
@WebMvcTest(AndroidGuardInfoController.class)
@Import(TestAppSecurityConfig.class)
public class AndroidGuardInfoControllerTest  {