java - 为什么 TestRestTemplate 允许在 Spring Boot IT 测试中进行未经身份验证的请求?

标签 java spring-boot spring-security integration-testing

在我的 springBoot(RELEASE 1.5.20)应用程序中,启用了基本身份验证。 我使用以下代码创建了完整的 IT 测试

@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = "securedIT")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MYtestIT{
@LocalServerPort
     private int port;

   private String getRootUrl() {
        return "http://localhost:" + port;
   }

   @Autowired
   private TestRestTemplate restTemplate;

    @Test
    public void testAdmincachWitheWrongAuthentication() {
        String baseUri = getRootUrl() + CONTEXT_ROOT;
         HttpEntity<String> entity = new HttpEntity<>(null,  new HttpHeaders());
         URI url = URI.create(baseUri + "/ref/cache/task");

       ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.DELETE, entity, String.class);
       //ResponseEntity<String> response = restTemplate.withBasicAuth("user", "myPwd").exchange(url, HttpMethod.DELETE, entity, String.class);

     assertEquals(ReferenceWSIT.MSG_WRON_STATUS,401, response.getStatusCode().value());
    }
}

在应用程序中的配置如下:

@Configuration
public class GlobalWebSecurityConfigurerAdapter extends 
WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .authorizeRequests()
            .antMatchers("ref/v1/**").permitAll()
            .antMatchers("ref/cache/**").authenticated().and().httpBasic();
    }   
}

当我运行应用程序时,身份验证过滤器工作正常,当我运行 Junit 集成测试时出现问题。 如果我调用restTemplate.withBasicAuth(),测试会根据好或坏的凭据正确失败或成功。 但是如果如果直接调用restTemplate而没有BasicAuth,则所有请求都被允许(所以我的测试断言失败)。

作为使用完整配置的 IT 测试,我希望身份验证是强制性的,为什么事实并非如此?

最佳答案

[编辑]我的第一个解决方案是错误的,正确的配置是:

@Configuration
public class GlobalWebSecurityConfigurerAdapter extends 
WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                .anonymous().disable()
                //following will disable cookie session to force the browser to Authenticate on each request      
               .sessionManagement()
               .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
               .and()
               .authorizeRequests()
               .antMatchers("/ref/cache/**")
               .authenticated().and().httpBasic()
               .and()
               .addFilterAfter(new HmacSecurityFilter(), BasicAuthenticationFilter.class)
               ;
    }   
}

第一个错误:antMatcher 必须以“/”开头:“/ref/cache/**” 而不是 “ref/cache/**”

其次,在我的第二个过滤器 (HmacSecurityFilter) 中,我检查任何请求(之前为 .antMatchers("ref/v1/**").permitAll() ),并执行自定义代码以避免检查经过身份验证的 uri (/ref/cache/**) 。

关于java - 为什么 TestRestTemplate 允许在 Spring Boot IT 测试中进行未经身份验证的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56689259/

相关文章:

java - 错误 401 - 浏览器中的 Spring Boot 执行器登录名/密码

grails - Spring Security Grails插件授权

spring - 如何在 Spring Security 中为 REST API 调用返回 401?

java - 使用Android QR Scanner从MySQL数据库检索数据

java - 在android中运行方法onclick崩溃

spring-boot - Swagger 为 REST 端点生成的主机名无效 REQUEST url

spring-boot - 如何在 Spring Boot 2.0 中使用 Redis 作为数据库映射两个实体之间的关系?

Java/SWT/如何从按钮中删除框架

java - MongoDB Panache 更新文档中的嵌套对象

spring-boot - 不能在 Spring Boot 2(版本 2.0.0.M7)中包含 Prometheus 指标