1

我有一个过滤器,我正在使用 Springs delegatingFilterProxy 来拦截 Web 服务的传入请求。根据是否设置了标志,我想允许请求继续,否则我想设置一个请求标头,当端点检查时将指示是否允许请求继续。我想将代码放在过滤器中以抽象检查标志的逻辑。

我如何修改过滤器中的请求标头(简单的代码示例会很棒),这是可以接受的做法,还是应该从过滤器发送响应而不是将决策下推到端点?

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(somecondition)
    set request header and forward to endpoint
else 
    forward to endpoint without extra header

谢谢

4

2 回答 2

2

您需要创建一个 FakeRequest 来解决它

FakeHeadersRequest

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class FakeHeadersRequest extends HttpServletRequestWrapper {

    private Map<String, List<String>> headers = new HashMap<String, List<String>>();

    public FakeHeadersRequest(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getHeader(String name) {
        if (headers.containsKey(name)) {
            return headers.get(name).get(0);
        }

        return super.getHeader(name);
    }

    public void addHeader(String header, String value) {
        List<String> list = headers.get(header);
        if (list == null) {
            list = new ArrayList<String>();
            headers.put(header, list);
        }
        list.add(value);
    }

    public void setHeader(String header, String value) {
        List<String> list = new ArrayList<String>();
        headers.put(header, list);
        list.add(value);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public Enumeration getHeaderNames() {
        List<String> list = new ArrayList<String>();

        HttpServletRequest request = (HttpServletRequest) getRequest();
        Enumeration<String> e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            list.add(e.nextElement());
        }

        list.addAll(headers.keySet());

        return Collections.enumeration(list);
    }
}

FakeHeaderFilter

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class FakeHeaderFilter implements Filter {
    public void init(FilterConfig filterConfig) {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        boolean condition = false;
        if (condition && request instanceof HttpServletRequest) {
            FakeHeadersRequest req = new FakeHeadersRequest(
                    (HttpServletRequest) request);
            req.setHeader("new-header", "some-value");
            chain.doFilter(req, response);
        } else {
            chain.doFilter(request, response);
        }

    }

    public void destroy() {
    }

}

参考这个

于 2013-03-23T11:11:42.553 回答
1

请求属性更适合此目的ServletRequest.get/setAttribute()。你有什么理由不能使用它们吗?

于 2013-03-23T12:18:45.470 回答