java - Spring boot Http Security 配置单元测试

标签 java spring spring-boot spring-security

在我的 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/

相关文章:

java - 用于 Spring Boot 应用程序的 Logback 自定义数据库附加程序

java - 从 log4j 迁移到 lof4j2 时的 SpringBoot 问题

java - 自定义合并排序算法不起作用

java - For 循环和 LinkedList 的索引错误

java - Spring TaskExecutor 中的 ActiveMQ 消息组等效项

java - 如何在 Spring-Boot 中启动主类?

java - Neo4j 在节点/关系创建时嵌入内存不足

java - 在 Eclipse Luna 上打开 Java 导入类时找不到源

java - 为什么 INSERT 查询无限期挂起并锁定 PostgreSQL 数据库和 pgAdmin

java - Spring Boot中方法调用的标准输出重定向的另一种方式