2016-10-26 93 views
2

我希望每當我通過假裝客戶端發出請求時,都要使用經過身份驗證的用戶設置特定標頭。如何將請求攔截器添加到假客戶端?

這是我的過濾器我從中得到了驗證,並將其設置到彈簧安全上下文:

@EnableEurekaClient 
@SpringBootApplication 
@EnableFeignClients 
public class PerformanceApplication { 
    @Bean 
    public Filter requestDetailsFilter() { 
     return new RequestDetailsFilter(); 
    } 

    public static void main(String[] args) { 
     SpringApplication.run(PerformanceApplication.class, args); 
    } 

    private class RequestDetailsFilter implements Filter { 
     @Override 
     public void init(FilterConfig filterConfig) throws ServletException { 

     } 

     @Override 
     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
      String userName = ((HttpServletRequest)servletRequest).getHeader("Z-User-Details"); 
      String pass = ((HttpServletRequest)servletRequest).getHeader("X-User-Details"); 
      if (pass != null) 
       pass = decrypt(pass); 
      SecurityContext secure = new SecurityContextImpl(); 
      org.springframework.security.core.Authentication token = new UsernamePasswordAuthenticationToken(userName, pass); 
      secure. setAuthentication(token); 
      SecurityContextHolder.setContext(secure); 
      filterChain.doFilter(servletRequest, servletResponse); 
     } 

     @Override 
     public void destroy() { 

     } 
    } 
    private String decrypt(String str) { 
     try { 
      Cipher dcipher = new NullCipher(); 

      // Decode base64 to get bytes 
      byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); 

      // Decrypt 
      byte[] utf8 = dcipher.doFinal(dec); 

      // Decode using utf-8 
      return new String(utf8, "UTF8"); 
     } catch (javax.crypto.BadPaddingException e) { 
     } catch (IllegalBlockSizeException e) { 
     } catch (UnsupportedEncodingException e) { 
     } catch (java.io.IOException e) { 
     } 
     return null; 
    } 
} 

這是我的假死客戶端:

@FeignClient("holiday-client") 
public interface EmailClient { 
    @RequestMapping(value = "/api/email/send", method = RequestMethod.POST) 
    void sendEmail(@RequestBody Email email); 
} 

在這裏,我有一個請求攔截器:

@Component 
public class FeignRequestInterceptor implements RequestInterceptor { 
    private String headerValue; 

    public FeignRequestInterceptor() { 
    } 

    public FeignRequestInterceptor(String username, String password) { 
     this(username, password, ISO_8859_1); 
    } 

    public FeignRequestInterceptor(String username, String password, Charset charset) { 
     checkNotNull(username, "username"); 
     checkNotNull(password, "password"); 
     this.headerValue = "Basic " + base64encode((username + ":" + password).getBytes(charset)); 
    } 

    private static String base64encode(byte[] bytes) { 
     BASE64Encoder encoder = new BASE64Encoder(); 
     return encoder.encode(bytes); 
    } 

    @Override 
    public void apply(RequestTemplate requestTemplate) { 
     requestTemplate.header("Authorization", headerValue); 
    } 
} 

我不知道如何配置這個攔截器到我的客戶端以及如何用用戶名和密碼標題。我怎麼能做到這一點?

回答

3

您確實不需要自己實現FeignRequestInterceptor,因爲feign.auth包中的BasicAuthRequestInterceptor完全相同。

這樣說,你基本上已經設置了幾乎所有的東西。剩下要做的就是用特定的用戶名和密碼定義basicAuthRequestInterceptor bean:

@Bean 
public RequestInterceptor basicAuthRequestInterceptor { 
    return new BasicAuthRequestInterceptor("username", "password"); 
} 
相關問題