如何使用@Conditional

时间:2019-12-03 16:19:20

标签: java spring

我正在尝试使用@Conditional批注编写针对WebSecurityAdapterConfig类的测试,该测试允许我们根据运行应用程序的环境来强制实施安全约束。...最初,我无法从({ {3}}) @Conditional(ProdStagingCondition.class)注释阻止了测试的运行

代码:

@Configuration
@EnableWebSecurity
@Conditional(ProdStagingCondition.class)
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityAdapterConfig extends WebSecurityConfigurerAdapter {

  private JWT jwt;
  private RequiredEnvironmentVariables requiredEnvironmentVariables;
  private JWTAccessDeniedHandler jwtAccessDeniedHandler;
  private JWTAuthenticationEntryPoint jwtAuthenticationEntryPoint;

  @Autowired
  public WebSecurityAdapterConfig() {
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf()
        .disable()
        // make sure we use stateless session; session won't be used to store user's state.
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // authorization requests config
        .and()
        .authorizeRequests()

        // Any other request must be authenticated
        .anyRequest()
        .authenticated()
        .and()
        // handle an authorized attempts
        .exceptionHandling()
        .accessDeniedHandler(jwtAccessDeniedHandler)
        .authenticationEntryPoint(jwtAuthenticationEntryPoint)
        .and()
        // Add a filter to validate the tokens with every request
        .addFilterAfter(
            new JWTokenAuthenticationFilter(
                jwt, RequiredEnvironmentVariables.supportedPaymentClients),
            UsernamePasswordAuthenticationFilter.class);
  }
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WebSecurityAdapterConfig.class)
public class WebSecurityAdapterConfigUnitTest {

  @Autowired
  private WebApplicationContext context;

  private MockMvc mockMvc;

  @Before
  public void setUp() {
    this.mockMvc =MockMvcBuilders
            .webAppContextSetup(context)
            .alwaysDo(print())
            .apply(springSecurity())
            .build();
  }

  @Test
  public void config() throws Exception {
    this.mockMvc
        .perform(get("/docs"))
        .andExpect(status().is4xxClientError());
  }
}

错误:

java.lang.IllegalStateException:springSecurityFilterChain不能为null。确保存在名为springSecurityFilterChain的实现Bean的Bean,或注入要使用的Filter。

at org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurer.beforeMockMvcCreated(SecurityMockMvcConfigurer.java:64)
at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:136)
at com.connectar.wallet.config.WebSecurityAdapterConfigUnitTest.setUp(WebSecurityAdapterConfigUnitTest.java:72)
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.RunBefores.evaluate(RunBefores.java:24)
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.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

1 个答案:

答案 0 :(得分:0)

我通过找到一种为不同的测试设置不同的环境属性的方法解决了这个问题,因此我添加了@TestPropertySource(properties = {“ SPRING_ENV = production”}),如下所示:


    @RunWith(SpringRunner.class)
@TestPropertySource(properties = {"SPRING_ENV= production"})
@SpringBootTest()
public class WebSecurityAdapterConfigUnitTest {

然后我更改了ProdStagingCondition类

来自:

public class ProdStagingCondition implements Condition {

  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String springEnviroment = System.getProperty("SPRING_ENV", "development");
    return springEnviroment.equalsIgnoreCase("staging")
        || springEnviroment.equalsIgnoreCase("production");
  }
}

收件人:

public class ProdStagingCondition implements Condition {

  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String springEnviroment = System.getProperty("SPRING_ENV", "development");
    return System.getProperty("SPRING_ENV") != "development";


  }
}