在自定义筛选器中检索HTTP标头

时间:2014-05-10 03:29:02

标签: curl spring-security servlet-filters

我实现了一个自定义过滤器来检索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

1 个答案:

答案 0 :(得分:2)

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

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

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