在開發應用程序的過程當中,若是有多個應用,一般會經過一個portal 門戶來集成,這個portal 是全部應用程序的入口,用戶一旦在portal 登陸以後,進入另一個系統,就須要相似的單點登陸(SSO). 進入各個子系統的時候,就不須要再次登陸, 固然相似的功能,你能夠經過專業的單點登陸軟件來實現,也能夠本身寫數據庫token 等方式來實現。其實還有一個比較簡單的方法,就是經過 portal 封裝已經登陸過的用戶的消息,寫到http header 之中,而後把請求forward 到各個子系統中去,而各子系統從 http header 中獲取用戶名,做爲是否登陸過的校驗或者合法的校驗。總結了幾種處理http Header 的方法:html
利用 HttpServletRequestjava
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; }
一個典型的例子以下:web
"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.google.com/bot.html )", "X-Request-Start" : "1389158003923", "Accept" : "*/*", "Connection" : "close", "X-Forwarded-Port" : "80", "From" : "googlebot(at)googlebot.com" }
獲取 user-agentspring
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.google.com/bot.html )
利用 spring mvc 獲取 HttpRequest Header 的例子
session
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 中也有記錄,那麼在子系統就是合法的用戶,而後本身能夠根據一些要求處理業務邏輯了mvc
JSP/Java獲取HTTP header信息(request)例子app
<%
//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>");jsp
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>");
%>ui
request.getHeader("Referer")獲取來訪者地址。只有經過連接訪問當前頁的時候,才能獲取上一頁的地址;不然request.getHeader("Referer")的值爲Null,經過window.open打開當前頁或者直接輸入地址,也爲Null。
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Map map = new HashMap(); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); String[] paramValues = request.getParameterValues(paramName); if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() != 0) { System.out.println("參數:" + paramName + "=" + paramValue); map.put(paramName, paramValue); } } }