【问题标题】:Testing WebSecurityConfigurerAdapter测试 WebSecurityConfigurerAdapter
【发布时间】:2015-08-05 21:15:45
【问题描述】:

这是我的测试,我正在添加额外的标题测试after testing my filter

@RunWith( SpringJUnit4ClassRunner.class )
@WebAppConfiguration
@EnableWebMvcSecurity
@SpringApplicationConfiguration( classes = {
        MockServletContext.class,
        HttpSessionConfig.class,
        WebSecurityConfig.class
} )
@SuppressWarnings( "PMD.TooManyStaticImports" )
public class HeadersTest {

    private MockMvc mockMvc = null;

    @Autowired
    private WebApplicationContext context;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup( context )
                .addFilter( new CORSFilter(), "/*" )
                .build();
    }

    @Test
    public void testOptions() throws Exception {
        mockMvc.perform( options( "/" ) )
                .andExpect( status().is2xxSuccessful() )
                .andExpect( header().string( "Access-Control-Allow-Origin", notNullValue() ) )
                .andExpect( header().string( "Access-Control-Allow-",
                        equalToIgnoringCase( "POST, GET, PUT, OPTIONS, DELETE" ) ) )
                .andExpect( header().string( "Access-Control-Allow-Headers",
                        equalToIgnoringCase( "content-type, x-auth-token, x-requested-with" ) ) )
                .andExpect( header().string( "Access-Control-Expose-Headers", equalToIgnoringCase( "Location" ) ) )
                .andExpect( header().string( "Access-Control-Max-Age", notNullValue() ) )
        ;
    }    
}

如果我从配置中删除 WebSecurityConfig.class,则测试有效,但是当我添加它时,我得到了这个异常

 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor]

这是WebSecurityConfig

@Configuration
@Order( SecurityProperties.BASIC_AUTH_ORDER - 1 )
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure( final HttpSecurity http ) throws Exception {
        http.httpBasic().and()
                .authorizeRequests()
                .antMatchers( "/" ).permitAll()
                .anyRequest().authenticated();
    }
}

如何更改我的测试以使WebSecurityConfig 正确?

【问题讨论】:

    标签: java spring spring-mvc spring-security spring-boot


    【解决方案1】:

    这似乎有效,但似乎更重,所以也许他们的答案更轻/更快?

    我加了

    @SpringBootApplication
    public class Application {
    }
    

    并加载它,以及添加@WebAppConfiguration 和我的WebSecurityConfig 类,

    @RunWith( SpringJUnit4ClassRunner.class )
    @WebAppConfiguration
    @SpringBootApplication
    @SpringApplicationConfiguration( classes = {
            MockServletContext.class,
            HttpSessionConfig.class,
            WebSecurityConfig.class,
            Application.class
    } )
    public class HeadersTest {
    

    现在测试再次通过,并添加了一个检查X-Frame-Options的测试

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 2017-08-01
      • 2023-03-25
      • 1970-01-01
      • 2021-12-04
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多