【问题标题】:Retrieving HTTP Headers in Custom Filter在自定义过滤器中检索 HTTP 标头
【发布时间】:2014-06-27 21:54:07
【问题描述】:

我实现了一个自定义过滤器来检索我的 HTTP 请求中的 X-Auth-Token HTTP 标头属性。

public class XAuthToken implements Filter {

    final private static String X_AUTH_TOKEN = "X-Auth-Token";

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws IOException, ServletException {

        System.out.println(">>>>>>>>> CUSTOM FILTER!");

        HttpServletRequest request = (HttpServletRequest) req;

        Object xAuthToken = request.getSession().getAttribute(X_AUTH_TOKEN);

        System.out.println("Printing attribute names");

        Enumeration<String> attributeNames = request.getSession().getAttributeNames();
        while(attributeNames.hasMoreElements()) {
            System.out.println(attributeNames.nextElement());
        }

我使用此命令执行带有 X-Auth-Token 的 HTTP 请求

curl -H "X-Auth-Token:234234" http://localhost:8090/SpringSecurityHelloWorld/login

但是,执行自定义过滤器代码时没有打印出属性名称:

>>>>>>>>> CUSTOM FILTER!
Printing attribute names

【问题讨论】:

    标签: curl spring-security servlet-filters


    【解决方案1】:

    我在这里可能错了,但我认为标头不被视为会话数据。查看 HttpServletRequest 实例上的 getHeaderNames。

    我认为这应该打印所有 http 标头

    Enumeration<String> headers = request.getHeaderNames();
    while (headers.hasMoreElements()) {
        System.out.println(headers.nextElement);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2012-09-22
      • 2014-08-23
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      相关资源
      最近更新 更多