Spring boot Http Security 配置单元测试

The*_*d77 4 java spring spring-security spring-boot

在我的 Spring boot 应用程序中,我有一个安全配置类,我试图为其编写单元测试。这是我第一次这样做,所以我需要一些帮助。这是下面的代码。请提供一些帮助,我们将不胜感激。谢谢

public class SecurityConfig extends WebSecurityConfigurerAdapter {

private String id;
private String pwd;
private String role;

@Autowired
private AuthenticationEntryPoint authEntryPoint;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
            .anyRequest().authenticated()
            .and().httpBasic()
            .authenticationEntryPoint(authEntryPoint).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    //This allows to view h2 console during development
    http.headers().frameOptions().sameOrigin();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().
            withUser(id).
            password(pwd).
            roles(role);
}}
Run Code Online (Sandbox Code Playgroud)

jzh*_*aux 6

我不建议为配置类本身编写单元测试。一般来说,证明应用程序功能的集成测试(例如使用Mock MVC )效果最好。

我意识到这不是你问的;但是,如果您查看 Spring Security 存储库,您会发现这也正是他们的做法。即,为了测试他们的配置器,他们使用集成测试