Question

I implemented a custom filter to retrieve the X-Auth-Token HTTP Header attribute in my HTTP request.

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());
        }

I used this command to perform an HTTP request with an X-Auth-Token

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

However, no attribute name printed out when the custom filter code executed:

>>>>>>>>> CUSTOM FILTER!
Printing attribute names
Was it helpful?

Solution

I might be wrong here but I think headers is not considered as session data. Take a look at getHeaderNames on the HttpServletRequest instance.

I think this should print all http headers

Enumeration<String> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
    System.out.println(headers.nextElement);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top