4

我有一个应用程序,我在其中使用 Spring Social Security 进行身份验证和授权。不幸的是,我在模拟 Spring Security 时遇到了一些问题。似乎它根本不起作用。

我有一个 REST 控制器,如果它应该返回的实体的标识符不可用,它会返回 404 Not Found。如果用户未登录,则任何页面都会重定向到我的应用程序的社交登录页面。

我在这里读到@WithUserDetails注释最适合我。

所以我的测试方法看起来像这样

@Test
@SqlGroup({
    @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, statements = "INSERT INTO UserAccount(id, creationtime, modificationtime, version, email, firstname, lastname, role, signinprovider) VALUES (1, '2008-08-08 20:08:08', '2008-08-08 20:08:08', 1, 'user', 'John', 'Doe', 'ROLE_USER', 'FACEBOOK')"), })
@Rollback
@WithUserDetails
public void ifNoTeamsInTheDatabaseThenTheRestControllerShouldReturnNotFoundHttpStatus() {
    ResponseEntity<String> response = restTemplate.getForEntity("/getTeamHistory/{team}", String.class, "Team");
    Assert.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}

但这似乎根本不起作用。看起来测试方法是用匿名用户执行的,因为我得到的状态是 200 OK。

我的测试类是这样注释的

@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Transactional
public class TeamRestControllerTest {
    //...
}

有没有人在模拟 Spring Social 提供的 Spring Security 时遇到过这样的问题?

4

2 回答 2

3

我目前无法对其进行测试,但这是一个可能的解决方案。

查看@WithUserDetails 实现:

@WithSecurityContext(factory = WithUserDetailsSecurityContextFactory.class)
public @interface WithUserDetails {
    ...
}

final class WithUserDetailsSecurityContextFactory implements
        WithSecurityContextFactory<WithUserDetails> {

    private BeanFactory beans;

    @Autowired
    public WithUserDetailsSecurityContextFactory(BeanFactory beans) {
        this.beans = beans;
    }

    public SecurityContext createSecurityContext(WithUserDetails withUser) {
        String beanName = withUser.userDetailsServiceBeanName();
        UserDetailsService userDetailsService = StringUtils.hasLength(beanName)
                ? this.beans.getBean(beanName, UserDetailsService.class)
                : this.beans.getBean(UserDetailsService.class);
        String username = withUser.value();
        Assert.hasLength(username, "value() must be non empty String");
        UserDetails principal = userDetailsService.loadUserByUsername(username);
        Authentication authentication = new UsernamePasswordAuthenticationToken(
                principal, principal.getPassword(), principal.getAuthorities());
        SecurityContext context = SecurityContextHolder.createEmptyContext();
        context.setAuthentication(authentication);
        return context;
    }
}

您可以按照相同的模式创建您选择的安全上下文:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@WithSecurityContext(factory = WithoutUserFactory.class)
public @interface WithoutUser {
}

public class WithoutUserFactory implements WithSecurityContextFactory<WithoutUser> {
    public SecurityContext createSecurityContext(WithoutUser withoutUser) {
        return SecurityContextHolder.createEmptyContext();
    }
}

其他可用的注解:WithAnonymousUser, WithMockUser, WithSecurityContext(and WithUserDetails)

于 2016-12-13T16:20:08.407 回答
0

添加我的解决方法,可能对其他人有帮助。

我想我遇到了同样的问题:

  • A @Testcontainers(用于 PostgreSQL 数据库仿真)+@SpringBootTest测试。
  • 用with mocking模拟了SecurityContextvia 注释。@WithSecurityContextfactory
  • 我需要对 Envers 进行这种模拟RevisionListener,在其中我从SecurityContextKeycloak 通常创建的用户名和用户名中获取。
  • 在测试中调用 Spring bean 时,模拟工作正常。
  • 但是当通过 , 调用 API 时TestRestTemplateSecurityContext不会被嘲笑,而是null为所有字段(主体等)返回 a。

原始类如下所示:

@SpringBootTest(
    classes = SpringBootInitializer.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = {"keycloak.enabled=false"}
)
@ContextConfiguration(
    classes = PersistenceConfiguration.class,
    initializers = MyTest.Initializer.class
)
// !!! the SecurityContext mocking will NOT work when calling the controller via REST
@MockKeycloakUser() // do not fail on getting Keycloak data in UserDataRevisionListener
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class }) // turn off Spring Security to avoid 401 and 302 responses
@Testcontainers // required to fill @Container fields with containers
@Log4j2
@ActiveProfiles("integration-test")
class MyTest {

    @Autowired
    private TestRestTemplate restTemplate;

    // ...

    // call via restTemplate looks like this
    private List<MyDTO> executeSearchQuery(String query) {
        String searchUrl = getSearchUrl(port, query, filter);
        MyDTO[] results = this.restTemplate.getForObject(searchUrl, MyDTO[].class);
        return List.of(results);
    }

    // ...
}

我曾经制作的SecurityContext作品是:

  • 将该字段添加MockMvc到测试类。
  • 添加@AutoConfigureMockMvc测试类。
  • !!!MockMvc通过而不是执行 APITestRestTemplate

看起来像这样:

// all other annotations on the test class stay the same

@AutoConfigureMockMvc // make MockMvc work
// ...
class MyTest {

    @Autowired
    private MockMvc mockMvc; // trick to make the mock SecurityContext work, which does not work when calling via TestRestTemplate

    // Execute the API via mockMvc looks like this:
    private String getApiResponse(MyRequest request, int expectedHttpStatus) {
        final String url = getRequestUrl();

        final String requestBody = JacksonUtils.serializeToString(request);

        try {
            final MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
                .post(url)
                .contentType(MediaType.APPLICATION_JSON)
                .content(requestBody)
            ;

            // use MockMvc instead of TestRestTemplate to successfully use the mock user emulation
            return mockMvc
                .perform(builder)
                .andExpect(status().is(expectedHttpStatus))
                .andReturn()
                .getResponse()
                .getContentAsString(StandardCharsets.UTF_8);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // ...
}

于 2022-02-02T14:28:00.807 回答