无法在Spring Security自定义筛选器中访问POST数据

时间:2017-05-05 18:26:12

标签: java spring spring-boot spring-security

我正在开发一个需要访问POST数据JSON的应用程序。我的代码是

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.web.filter.GenericFilterBean;

public class LoginFilter extends GenericFilterBean {

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
      FilterChain filterChain) throws IOException, ServletException {

    System.out.println("filtered /login request: " +  servletRequest.getParameter("idToken"));
    filterChain.doFilter(servletRequest, servletResponse);
  }
}

当我运行应用程序时

$ curl -v -X POST -H "Content-Type: application/json" -d '{"idToken" : "EWRE@#$RWER@#23434W$"}' http://localhost:8080/login

我在日志上得到null

2017-05-06 06:23:31.153  INFO 89727 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 2 ms
filtered /login request: null

我错过了什么?

1 个答案:

答案 0 :(得分:0)

您将json作为请求主体传递而不是param。试试这种方式,

servletRequest.getReader().lines().collect(Collectors.joining(System.lineSeparator()));

但是servletRequest.getReader()返回一个只能读取一次的阅读器。