【发布时间】:2016-01-23 21:45:21
【问题描述】:
我正在尝试使用 Java 代码在给定的 URL 中发送标头,如下所示:
URL u = new URL("http://domain:8081/App/mycall");
HttpURLConnection con= (HttpURLConnection) u.openConnection();
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Auth-Token", authToken);
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
然后在我的应用程序中,我正在检查Auth-Token,如下所示:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
if (SecurityContextHolder.getContext().getAuthentication() != null) {
String reqToken = req.getHeader("Auth-Token");
}
}
当我在带有标题的邮递员中调用上述 URL 时,它工作正常。但是当我在 Java 代码中运行 URL 时,SecurityContextHolder.getContext().getAuthentication() 总是返回 null。
我做错了什么?
【问题讨论】:
-
这个链接可能对你有帮助:stackoverflow.com/questions/11544256/…