During the process of developing applications, if there are multiple applications, they will usually be integrated through a portal portal. This portal is the entrance to all applications. Once the user logs in to portal and enters another system, he or she needs a similar single sign-on (SSO). When entering each subsystem, he or she does not need to log in again. Of course, you can implement similar functions through professional single sign-on software, or you can write database tokens by yourself, etc. In fact, there is another relatively simple method, which is to encapsulate the logged-in user's messages through portal, write it to the http header, and then forward the request to each subsystem, and each subsystem obtains the user name from the http header as a verification of whether you have logged in or a legal verification.
Several methods for dealing with http Header are summarized:
Utilize 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; }A typical example is as follows:
"headers" : { "Host" : "yihaomen.com", "Accept-Encoding" : "gzip,deflate", "X-Forwarded-For" : "66.249.xx", "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"}Get user-agent
import javax.servlet.http.HttpServletRequest; //... private HttpServletRequest request; private String getUserAgent() { return request.getHeader("user-agent"); }A typical example is as follows:
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
Example of using spring mvc to get 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 modelView = new ModelAndView("result"); modelView.addObject("user-agent", getUserAgent()); modelView.addObject("headers", getHeadersInfo()); return modelView; } //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; }}Some people may say that Http Header can be simulated, so you can construct a system to deceive these. Yes, that's true. So when using Http Header to pass it worth it, you must remember that all requests must be processed through portal and forwarded to each subsystem, and this problem will not occur. Because portal first intercepts all requests initiated by the user. If it is a constructed user, there is no record in the portal session and it will still jump to the login page. If it is recorded in the protal session and there are also records in the Http Header, then it is a legal user in the subsystem, and then you can handle the business logic according to some requirements.
JSP/Java obtains HTTP header information (request) example
<%//header.jspout.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("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>");%>Notes about request.getHeader("Referer")
request.getHeader("Referer") gets the address of the visitor. Only when accessing the current page through a link can you get the address of the previous page; otherwise, the value of request.getHeader("Referer") is Null, and the current page is opened through window.open or directly enter the address is also Null.
The above is the full content of several methods (must-read) for obtaining HttpRequest Header from java brought to you by the editor. I hope it will be helpful to you and support Wulin.com more~