在我的 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);
}}
最佳答案
我不建议为配置类本身编写单元测试。一般来说,集成测试可以证明您的应用程序的功能,例如使用 Mock MVC ,效果最佳。
我意识到这不是你问的;但是,如果您查看 Spring Security 存储库,您会发现这也正是他们的做法。即,测试其配置器,they use integration tests .
关于java - Spring boot Http Security 配置单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51828579/