2017-02-13 1 views
0

私たちのアクセス有権者の中には、認証のWebAuthenticationDetailsにアクセスする必要があります。WebAuthenticationDetailsのすぐに使用できるテストサポートはありますか?

Spring Securityは、WebAuthenticationDetailsをテストケースの認証で提供して関連付けるための、すぐにサポートを提供しますか?私は@WithMockUserや@WithUserDetailsがWebAuthenticationDetailsなどを埋め込んでいないことに気付きました。

この機能をテストに実装するには、どのような方法をお勧めしますか?

標準的な方法がない場合、私自身のRequestPostProcessorを提供する必要がありますか?

私はSpring Security 4.1.4を使用しています。春のセキュリティテストの

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; 
    } 
} 

おそらく、このようなものであるべき部分:私は、カスタムRequestPostProcessor、のようなものを使用し、今の

答えて

0

、自分の質問に答えますか?

関連する問題