【问题标题】:Download secure page from controller, Spring security从控制器下载安全页面,Spring security
【发布时间】:2013-08-16 16:38:31
【问题描述】:

我正在运行 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
    }
}

由于 Spring Security,viewStuff 方法看不到 /downloadMe.xhtml 页面。有什么方法可以将我的请求中的安全凭据放入新请求中并下载 downloadMe.xhtml。

*必须以这种方式或具有相同结果的类似方式完成。我不能只调用 downloadMe(request, response)。我需要从 myJsp 返回的数据以及它附带的所有逻辑。

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    解决了我自己的问题!通过在我的请求中将 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 2010-12-21
      • 2017-05-27
      相关资源
      最近更新 更多