简体   繁体   English

如何在 Spring Boot 测试中跳过 TestRestTemplate 的身份验证?

[英]How to skip authentication for TestRestTemplate in Spring Boot Tests?

Below is my test class.下面是我的测试课。 The hello-world endpoint simply returns an HTML page containing text ie Hello Stranger! hello-world 端点仅返回一个包含文本的 HTML 页面,即 Hello Stranger!

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloWorldTest {

    @Autowired
    private HelloWorldController controller;

    @Autowired
    private TestRestTemplate restTemplate;

    @LocalServerPort
    private int port;

    @Test
    public void contextLoads() throws Exception {
        assertThat(controller).isNotNull();
    }

    @Test
    public void greetingShouldReturnDefaultMessage() throws Exception {

        String baseUrl = "http://localhost:" + port;

        assertThat(this.restTemplate.getForObject(baseUrl+"/hello-world", String.class))
                .contains("Hello Stranger!");
    }
}

This is my Security Config:这是我的安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll();
    }
}

It simply redirects all authenticated users to the login page它只是将所有经过身份验证的用户重定向到登录页面

I have tried adding @WithMockUser annotation or adding another security config class in my test directory to override the default config.我尝试在我的测试目录中添加 @WithMockUser 注释或添加另一个安全配置类来覆盖默认配置。 But so far nothing has seemed to work.但到目前为止,似乎没有任何效果。

Any help or suggestions on documentation to read is appreciated!对阅读文档的任何帮助或建议表示赞赏!

I have managed to solve this issue by first creating another web security config without requiring login/authorization, then by adding @Profile to my config class and production/dev/test profile via application.properties in my test directory (ie adding "spring.profiles.active=test" ).我设法解决了这个问题,方法是首先创建另一个不需要登录/授权的 Web 安全配置,然后通过我的测试目录中的application.properties添加@Profile到我的配置类和production/dev/test配置文件(即添加"spring.profiles.active=test" )。

Not sure if this is the best way to solve this issue, but it works for now.不确定这是否是解决此问题的最佳方法,但目前有效。

Another way to do it that worked for me was to override the normal security configation for running the integration test like so:另一种对我有用的方法是覆盖运行集成测试的正常安全配置,如下所示:

@TestConfiguration
@Order(-101)
@EnableWebSecurity
class TestSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity security) throws Exception {
        security.httpBasic().and().formLogin().disable();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM