2017-01-16 64 views
1

我們想添加標題到我們的請求後發佈spring安全認證。附加請求標題後彈簧安全認證

但是,標題不會被追加。 我們可以通過Zuul過濾器來完成,但不能使用彈簧安全過濾器。

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
    httpServletRequestWrapper = new HttpServletRequestWrapper(request) { 
     @Override 
     public String getHeader(String name) { 
      if (name.equalsIgnoreCase(ENV - HEADER)) { 

       return active; 
      } else if (name.equalsIgnoreCase(USERID)) { 

       return (String) authentication.getPrincipal(); 
      } else { 
       return super.getHeader(name); 
      } 
     } 
    }; 

    filterChain.doFilter(httpServletRequestWrapper, servletResponse); 
} 

回答

0

在您的安全配置文件中運行過濾器以在org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter之後添加標題以請求。

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.exceptionHandling().and().anonymous().and().servletApi().and().headers().and().authorizeRequests() 

    // Your existing security configurations 

    // Assuming UsernamePasswordAuthenticationFilter is the filter 
    .addFilterAfter(new AddHeaderFilter(), UsernamePasswordAuthenticationFilter.class); 
} 

UsernamePasswordAuthenticationFilter是爲spring安全認證運行的enbuild彈簧過濾器。

0

添加您的陽明海運或屬性文件配置

zuul: 
    sensitiveHeaders: Cookie,Set-Cookie 
    add-proxy-headers: true 

然後zuul讓頭去其他終點。