1. Quesetion: I want to use FilterRegistrationBean register a filter for modify some Attributes。The filter can get value from application.properties。
  2. Code: TeslaFilterConfig.class:
@Configuration
public class TeslaFilterConfig {
    @Value("${filter.biz-app:}")
    private String bizAppEnv;

    @Bean
    public FilterRegistrationBean regBizAppFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new BizAppFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("bizAppEnv", bizAppEnv);
        filterRegistrationBean.setOrder(-1);
        filterRegistrationBean.setEnabled(true);
        return filterRegistrationBean;
    }
}

BizAppFilter.class:

public class BizAppFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest,
                                    HttpServletResponse httpServletResponse, FilterChain filterChain)
            throws ServletException, IOException {
        String bizAppEnv = getFilterConfig().getInitParameter("bizAppEnv");
        HttpServletRequestWrapper httpServletRequestWrapper = new HttpServletRequestWrapper(httpServletRequest) {
            @Override
            public String getHeader(String name) {
                if (StringUtils.isNotEmpty(name) && name.equalsIgnoreCase(HttpHeaderNames.X_BIZ_APP)) {
                    if (StringUtils.isNotEmpty(bizAppEnv) && bizAppEnv.equalsIgnoreCase("env-group")) {
                        String bizApp = super.getHeader(HttpHeaderNames.X_BIZ_APP);
                        if(StringUtils.isNotEmpty(bizApp)){
                            StringBuilder resBizApp = new StringBuilder();
                            int flag = 0;
                            for (Character c : bizApp.toCharArray()) {
                                if (c == '|' && flag == 1) {
                                    return resBizApp.toString();
                                } else if (c == '|') {
                                    flag += 1;
                                }
                                resBizApp.append(c);
                            }
                            return resBizApp.toString();
                        }
                    }
                    return super.getHeader(name);
                } else {
                    return super.getHeader(name);
                }
            }
        };
        filterChain.doFilter(httpServletRequestWrapper, httpServletResponse);
    }
}

Controller:

@RequestMapping(value = "/filter1", method = {RequestMethod.GET})
    @ResponseBody
    public TeslaBaseResult getAdminApplications(
        @RequestHeader(value = "X-Biz-App") String bizApp
    ) {
        // return buildSucceedResult(applicationService.getAdminApplications(userEmpid));
        return buildSucceedResult(bizApp);
    }

    @RequestMapping(value = "/filter2")
    @ResponseBody
    public TeslaBaseResult testFilter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String res = httpServletRequest.getHeader("X-Biz-App");
        return buildSucceedResult(res);
    }
  1. test: define Attribute:X-Biz-App: platform|group|test|111 the result of filter1:
{
  "code": 200,
  "message": "OK",
  "data": "platform|group|test|111"
}

the result of filter2:

{
  "code": 200,
  "message": "OK",
  "data": "platform|group"
}

we can found that the result of filter1 is not apply the filter, but the filter2 worked well! what happened in annotation(@RequestHeader)? are all requests enter filters first? why the Annotation didn't work like the later?

Comment From: wilkinsona

You haven’t overridden the right methods to provide a consistent view of the request’s headers. For example, the code that gets the value for @RequestHeader calls getHeaders(String) and you have only overridden getHeader(String).

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.