【问题标题】:Spring boot Http Security config unit testSpring Boot Http Security 配置单元测试
【发布时间】:2019-01-20 13:16:01
【问题描述】:

在我的 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);
}}

【问题讨论】:

  • 你有什么问题?
  • 我正在尝试对这个类进行单元测试,但不知道如何去做

标签: java spring spring-boot spring-security


【解决方案1】:

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

我知道这不是你问的;然而,如果你看一下 spring security repo,你会发现他们也是这样做的。即,测试他们的配置器,they use integration tests。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2017-10-21
    • 2018-12-13
    • 2017-02-12
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多