2017-02-13 3 views
0

Некоторым из наших избирателей, получающих доступ, нужен доступ к WebAuthenticationDetails для проверки подлинности.Есть ли готовая тестовая поддержка для WebAuthenticationDetails?

Обеспечивает ли Spring Security бесплатную поддержку для предоставления и связывания WebAuthenticationDetails с аутентификацией в тестовых случаях? Я заметил, что @WithMockUser или @WithUserDetails не заполняют, например, WebAuthenticationDetails.

Каков рекомендуемый и/или лучший способ реализовать эту функциональность в тестах?

Если стандартного способа нет, должен ли я предоставить свой собственный RequestPostProcessor?

Я использую Spring Security 4.1.4.

ответ

0

Отвечая на мой собственный вопрос, потому что теперь я использую обычай RequestPostProcessor, что-то вроде:

static RequestPostProcessor webAuthenticationDetails() { 
    return new WebAuthenticationDetailsPostProcessor(); 
} 

static RequestPostProcessor webAuthenticationDetails(String remoteIpAddress) { 
    return new WebAuthenticationDetailsPostProcessor(remoteIpAddress); 
} 

private static class WebAuthenticationDetailsPostProcessor implements RequestPostProcessor { 

    private final String remoteIpAddress; 

    public WebAuthenticationDetailsPostProcessor() { 
     this(null); 
    } 

    public WebAuthenticationDetailsPostProcessor(String remoteIpAddress) { 
     this.remoteIpAddress = remoteIpAddress; 
    } 

    @Override 
    public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { 
     SecurityContext context = SecurityContextHolder.getContext(); 
     if (context != null) { 
      Authentication authentication = context.getAuthentication(); 
      if (authentication instanceof AbstractAuthenticationToken) { 
       if (remoteIpAddress != null) { 
        request.setRemoteAddr(remoteIpAddress); 
       } 
       ((AbstractAuthenticationToken) authentication).setDetails(new WebAuthenticationDetails(request)); 
      } 
     } 
     return request; 
    } 
} 

Возможно, что-то, как это должно быть частью Spring Теста безопасности?

Смежные вопросы