java 獲得HttpRequest Header的幾種辦法(必看篇)。本站提示廣大學習愛好者:(java 獲得HttpRequest Header的幾種辦法(必看篇))文章只能為提供參考,不一定能成為您想要的結果。以下是java 獲得HttpRequest Header的幾種辦法(必看篇)正文
在開辟運用法式的進程中,假如有多個運用,平日會經由過程一個portal 門戶來集成,這個portal 是一切運用法式的進口,用戶一旦在portal 登錄以後,進入別的一個體系,就須要相似的單點登錄(SSO). 進入各個子體系的時刻,就不須要再次登錄, 固然相似的功效,你可以經由過程專業的單點登錄軟件來完成,也能夠本身寫數據庫token 等方法來完成。其實還有一個比擬簡略的辦法,就是經由過程 portal 封裝曾經登錄過的用戶的新聞,寫到http header 當中,然後把要求forward 到各個子體系中去,而各子體系從 http header 中獲得用戶名,作為能否登錄過的校驗或許正當的校驗。
總結了幾種處置http Header 的辦法:
應用 HttpServletRequest
import javax.servlet.http.HttpServletRequest; //... private HttpServletRequest request; //get request headers private Map<String, String> getHeadersInfo() { Map<String, String> map = new HashMap<String, String>(); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String key = (String) headerNames.nextElement(); String value = request.getHeader(key); map.put(key, value); } return map; }
一個典范的例子以下:
"headers" : { "Host" : "yihaomen.com", "Accept-Encoding" : "gzip,deflate", "X-Forwarded-For" : "66.249.x.x", "X-Forwarded-Proto" : "http", "User-Agent" : "Mozilla/5.0 (compatible; Googlebot/2.1; + http://www.谷歌.com/bot.html )", "X-Request-Start" : "1389158003923", "Accept" : "*/*", "Connection" : "close", "X-Forwarded-Port" : "80", "From" : "谷歌bot(at)谷歌bot.com" }
獲得 user-agent
import javax.servlet.http.HttpServletRequest; //... private HttpServletRequest request; private String getUserAgent() { return request.getHeader("user-agent"); }
一個典范的例子以下:
Mozilla/5.0 (compatible; Googlebot/2.1; + http://www.谷歌.com/bot.html )
應用 spring mvc 獲得 HttpRequest Header 的例子
import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/site") public class SiteController { @Autowired private HttpServletRequest request; @RequestMapping(value = "/{input:.+}", method = RequestMethod.GET) public ModelAndView getDomain(@PathVariable("input") String input) { ModelAndView modelandView = new ModelAndView("result"); modelandView.addObject("user-agent", getUserAgent()); modelandView.addObject("headers", getHeadersInfo()); return modelandView; } //get user agent private String getUserAgent() { return request.getHeader("user-agent"); } //get request headers private Map<String, String> getHeadersInfo() { Map<String, String> map = new HashMap<String, String>(); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String key = (String) headerNames.nextElement(); String value = request.getHeader(key); map.put(key, value); } return map; } }
或許有人會說,Http Header 是可以模仿的,那末本身可以結構一個用來誘騙這些體系, 是的,切實其實是如許,所以在用Http Header 來傳值得時刻,必定要記得,一切的要求都必需經由 portal 來處置,然後 forward 到各子體系,就不會湧現這個成績了。由於portal 起首攔阻用戶提議的一切的要求,假如是結構的用戶,在portal 的sessiion 也是沒有記載的,依然會跳轉到登錄頁面,假如在protal 的 session 中記載,並且 Http Header 中也有記載,那末在子體系就是正當的用戶,然後本身可以依據一些請求處置營業邏輯了
JSP/Java獲得HTTP header信息(request)例子
<% //header.jsp out.println("Protocol: " + request.getProtocol() + "<br>"); out.println("Scheme: " + request.getScheme() + "<br>"); out.println("Server Name: " + request.getServerName() + "<br>" ); out.println("Server Port: " + request.getServerPort() + "<br>"); out.println("Protocol: " + request.getProtocol() + "<br>"); out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo() + "<br>"); out.println("Remote Addr: " + request.getRemoteAddr() + "<br>"); out.println("Remote Host: " + request.getRemoteHost() + "<br>"); out.println("Character Encoding: " + request.getCharacterEncoding() + "<br>"); out.println("Content Length: " + request.getContentLength() + "<br>"); out.println("Content Type: "+ request.getContentType() + "<br>"); out.println("Auth Type: " + request.getAuthType() + "<br>"); out.println("HTTP Method: " + request.getMethod() + "<br>"); out.println("Path Info: " + request.getPathInfo() + "<br>"); out.println("Path Trans: " + request.getPathTranslated() + "<br>"); out.println("Query String: " + request.getQueryString() + "<br>"); out.println("Remote User: " + request.getRemoteUser() + "<br>"); out.println("Session Id: " + request.getRequestedSessionId() + "<br>"); out.println("Request URL: " + request.getRequestURL() + "<br>"); out.println("Request URI: " + request.getRequestURI() + "<br>"); out.println("Servlet Path: " + request.getServletPath() + "<br>"); out.println("Created : " + session.getCreationTime() + "<br>"); out.println("LastAccessed : " + session.getLastAccessedTime() + "<br>"); out.println("Accept: " + request.getHeader("Accept") + "<br>"); out.println("Host: " + request.getHeader("Host") + "<br>"); out.println("Referer : " + request.getHeader("Referer") + "<br>"); out.println("Accept-Language : " + request.getHeader("Accept-Language") + "<br>"); out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding") + "<br>"); out.println("User-Agent : " + request.getHeader("User-Agent") + "<br>"); out.println("Connection : " + request.getHeader("Connection") + "<br>"); out.println("Cookie : " + request.getHeader("Cookie") + "<br>"); %>
關於request.getHeader("Referer")的解釋
request.getHeader("Referer")獲得來訪者地址。只要經由過程鏈接拜訪以後頁的時刻,能力獲得上一頁的地址;不然request.getHeader("Referer")的值為Null,經由過程window.open翻開以後頁或許直接輸出地址,也為Null。
以上就是小編為年夜家帶來的java 獲得HttpRequest Header的幾種辦法(必看篇)的全體內容了,願望對年夜家有所贊助,多多支撐~