Невозможно получить пользовательский заголовок, добавленный через HttpServletRequestWrapper в Spring Boot Controller - PullRequest
0 голосов
/ 10 февраля 2020

Я пытаюсь установить несколько customHeader в дополнение к заголовку, отправляемому клиентом с помощью HttpServletRequestWrapper путем перехвата запроса в фильтре. Однако я не могу увидеть customHeader специально. Ниже мой подкласс класса HttpServletRequestWrapper, который я реализовал

@Component
public class MutableHttpServletRequest extends HttpServletRequestWrapper {

    private ByteArrayOutputStream cachedBytes = null;
    private final Map<String, String> customHeaders;

    public MutableHttpServletRequest (HttpServletRequest request) {
        super(request);
        this.customHeaders = new HashMap<String, String>();
    }

    public void putHeader(String name, String value){
        this.customHeaders.put(name, value);
    }

    @Override
    public String getHeader(String name) {
        // check the custom headers first
        String headerValue = customHeaders.get(name);

        if (headerValue != null){
            return headerValue;
        }
        // else return from into the original wrapped object
        return ((HttpServletRequest) getRequest()).getHeader(name);
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        // create a set of the custom header names
        Set<String> set = new HashSet<String>(customHeaders.keySet());

        // now add the headers from the wrapped request object
        @SuppressWarnings("unchecked")
        Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames();
        while (e.hasMoreElements()) {
            // add the names of the request headers into the list
            String n = e.nextElement();
            set.add(n);
        }

        // create an enumeration from the set and return
        return Collections.enumeration(set);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (cachedBytes == null)
            cacheInputStream();

        return new CachedServletInputStream();
    }

    @Override
    public BufferedReader getReader() throws IOException{
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    private void cacheInputStream() throws IOException {
        cachedBytes = new ByteArrayOutputStream();
        IOUtils.copy(super.getInputStream(), cachedBytes);
    }

    public class CachedServletInputStream extends ServletInputStream {
        private ByteArrayInputStream input;

        public CachedServletInputStream() {
            input = new ByteArrayInputStream(cachedBytes.toByteArray());
        }

        @Override
        public boolean isFinished () {
            return input.available() == 0;
        }

        @Override
        public boolean isReady () {
            return true;
        }

        @Override
        public void setReadListener (ReadListener readListener) {
            throw new RuntimeException("Not implemented");
        }

        @Override
        public int read() throws IOException {
            return input.read();
        }
    }
}

Можете ли вы помочь мне понять, что не так с моим кодом

...