2017-10-05 69 views
0

我正在研究具有vue.js代码的spring启动应用程序。我如何在Spring Boot中的index.html上设置cookie或标头

现在在初始上下文“/”中,我想在index.html页面上设置一个cookie或一个标头。

我已经尝试了下面的代码,我一直得到404。关于如何解决这个问题的任何想法?

@Controller 
public class TestController { 

@RequestMapping(path = "/", method = RequestMethod.GET) 
public String index(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 
     throws IOException { 

    String reqUrl = httpServletRequest.getRequestURL().toString(); 
    String displayName = httpServletRequest.getHeader("displayName"); 

    if (StringUtils.isBlank(displayName)) { 

     String url = "http://example.com/sso/?targetUrl=" + reqUrl; 

     URL obj = new URL(url); 

     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
     con.setRequestMethod("POST"); 
     System.out.println("Header from SSO" + con.getHeaderField("displayName")); 
     httpServletResponse.setHeader("displayName", con.getHeaderField("displayName")); 
    } 

    return "index"; 
} 

} 

这就是我的项目结构。

Project Structure of Spring Boot App

回答

0

我想通了,我的问题,我有我的return语句改为

return "index.html"; 
0

它看起来像你想从您刚刚创建的HttpURLConnection的头?

如果你想传递给了“” http://example.com/sso“URL你使用的HttpClient的更好。

相关问题