【问题标题】:Append request header post spring security authentication在 spring 安全认证后附加请求标头
【发布时间】:2017-05-31 02:09:20
【问题描述】:

我们想在 Spring 安全认证发生后向我们的请求添加标头。

但是,没有附加标题。 我们能够通过 Zuul 过滤器做到这一点,但不能使用 spring 安全过滤器。

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

【问题讨论】:

    标签: spring spring-security microservices netflix-zuul


    【解决方案1】:

    运行过滤器以在您的安全配置文件中的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 spring 过滤器。

    【讨论】:

      【解决方案2】:

      将你的 yml 或属性文件配置添加到

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

      然后zuul允许header去其他端点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-30
        • 2016-05-14
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 2021-09-06
        • 1970-01-01
        相关资源
        最近更新 更多