2017-10-05 137 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的更好。

相關問題