gpt4 book ai didi

java - 测试 WebSecurityConfigurerAdapter

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:50 27 4
gpt4 key购买 nike

这是我的测试,我正在添加额外的 header 测试 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 正确?

最佳答案

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

我添加了

@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

的测试

关于java - 测试 WebSecurityConfigurerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420576/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com