如何使用Unirest在Java中发送cookie进行身份验证

时间:2016-09-19 09:21:18

标签: java spring-mvc cookies unirest

正如标题所说,我正在尝试使用我的get请求发送包含用户ID的cookie。这样的事情:

    BasicCookieStore cookieStore =  new org.apache.http.impl.client.BasicCookieStore();

    cookieStore.addCookie(new BasicClientCookie("UserID",userId));

    Unirest.setHttpClient(org.apache.http.impl.client.HttpClients.custom()
            .setDefaultCookieStore(cookieStore)
            .build());
    com.mashape.unirest.http.HttpResponse<InputStream> responseGet = Unirest.get(someUrl)
            .header("content-type", "*/*")
            .asBinary();

问题在于我的spring控制器处理请求。它没有收到cookie。 任何帮助将不胜感激。谢谢你

EDIT 这是控制器。

@RestController
public class ExcelTest {

@Autowired
private RequestUtils requestUtils;

@RequestMapping(value= "/excelUnmatched", method = RequestMethod.GET)
public void getExcel(HttpServletResponse response, HttpServletRequest request) throws IOException {
    String userId=request.getHeader("UserID");
    Optional<User> user = requestUtils.getUserFromRequest(request);
    Workbook workbook = exportExcel();
    response.setHeader("Text send", "attachment; filename=test.xlsx");
    workbook.write(response.getOutputStream());

}

问题是我可以将userID放在标头中,但由于设计限制,我需要cookie中的userID,所以我可以使用getUserFromRequest方法。 再次感谢您

0 个答案:

没有答案