[英]Download secure page from controller, Spring security
我正在運行Spring Web and Security 3.1。 我需要從安全區域內的安全區域下載另一個本地頁面。 給出以下代碼:
@Controller
@RequestMapping("/secure")
public class SecureArea {
@RequestMapping("/downloadMe.xhtml")
public String downloadMe(HttpServletRequest request, HttpServletResponse response) throws Exception {
// do stuff
return "myJsp";
}
@RequestMapping("/viewStuff")
public void viewStuff(HttpServletRequest request, HttpServletResponse response) throws Exception {
InputStream in = (new URL("http://"+request.getServerName()+":"+request.getServerPort()+"/secure/downloadMe.xhtml").openStream());
// read the input stream and do stuff with it, obviously returns my 401 page
}
}
由於春季的安全性,viewStuff方法無法看到/downloadMe.xhtml頁面。 有什么方法可以將請求中的安全憑據放入新請求中,然后下載downloadMe.xhtml。
* 必須以這種方式或具有相同結果的類似方式進行。 我不能只調用downloadMe(request,response)。 我需要從myJsp返回的數據及其附帶的所有邏輯。
解決了我自己的問題! 我可以通過在請求中將JSESSIONID作為cookie傳遞來使其工作。 所以從我的問題中的代碼來看,它看起來像這樣:
@Controller
@RequestMapping("/secure")
public class SecureArea {
@RequestMapping("/downloadMe.xhtml")
public String downloadMe(HttpServletRequest request, HttpServletResponse response) throws Exception {
// do stuff
return "myJsp";
}
@RequestMapping("/viewStuff")
public void viewStuff(HttpServletRequest request, HttpServletResponse response) throws Exception {
URL url = new URL("http://"+request.getServerName()+":"+request.getServerPort()+"/secure/downloadMe.xhtml");
URLConnection con = url.openConnection();
con.setDoOutput(true);
// attach the session ID in the request
con.setRequestProperty("Cookie", "JSESSIONID="+request.getSession().getId());
con.connect();
InputStream in = con.getInputStream();
// read the input stream and do stuff with it, obviously returns my 401 page
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.