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 message of a logged in user 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 ; //... private HttpServletRequest request; //get request headers private Map<String, String> getHeadersInfo() { Map<String, String> map = new HashMap<String, String>(); Enumeration headerNames = (); while (()) { String key = (String) (); String value = (key); (key, value); } return map; }
A typical example is as follows:
"headers" : { "Host" : "", "Accept-Encoding" : "gzip,deflate", "X-Forwarded-For" : "66.", "X-Forwarded-Proto" : "http", "User-Agent" : "Mozilla/5.0 (compatible; Googlebot/2.1; + / )", "X-Request-Start" : "1389158003923", "Accept" : "*/*", "Connection" : "close", "X-Forwarded-Port" : "80", "From" : "googlebot(at)" }
Get user-agent
import ; //... private HttpServletRequest request; private String getUserAgent() { return ("user-agent"); }
A typical example is as follows:
Mozilla/5.0 (compatible; Googlebot/2.1; + / )
Use spring mvc to obtain an example of HttpRequest Header
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; @Controller @RequestMapping("/site") public class SiteController { @Autowired private HttpServletRequest request; @RequestMapping(value = "/{input:.+}", method = ) public ModelAndView getDomain(@PathVariable("input") String input) { ModelAndView modelandView = new ModelAndView("result"); ("user-agent", getUserAgent()); ("headers", getHeadersInfo()); return modelandView; } //get user agent private String getUserAgent() { return ("user-agent"); } //get request headers private Map<String, String> getHeadersInfo() { Map<String, String> map = new HashMap<String, String>(); Enumeration headerNames = (); while (()) { String key = (String) (); String value = (key); (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 then forward 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
<% // ("Protocol: " + () + "<br>"); ("Scheme: " + () + "<br>"); ("Server Name: " + () + "<br>" ); ("Server Port: " + () + "<br>"); ("Protocol: " + () + "<br>"); ("Server Info: " + getServletConfig().getServletContext().getServerInfo() + "<br>"); ("Remote Addr: " + () + "<br>"); ("Remote Host: " + () + "<br>"); ("Character Encoding: " + () + "<br>"); ("Content Length: " + () + "<br>"); ("Content Type: "+ () + "<br>"); ("Auth Type: " + () + "<br>"); ("HTTP Method: " + () + "<br>"); ("Path Info: " + () + "<br>"); ("Path Trans: " + () + "<br>"); ("Query String: " + () + "<br>"); ("Remote User: " + () + "<br>"); ("Session Id: " + () + "<br>"); ("Request URL: " + () + "<br>"); ("Request URI: " + () + "<br>"); ("Servlet Path: " + () + "<br>"); ("Created : " + () + "<br>"); ("LastAccessed : " + () + "<br>"); ("Accept: " + ("Accept") + "<br>"); ("Host: " + ("Host") + "<br>"); ("Referer : " + ("Referer") + "<br>"); ("Accept-Language : " + ("Accept-Language") + "<br>"); ("Accept-Encoding : " + ("Accept-Encoding") + "<br>"); ("User-Agent : " + ("User-Agent") + "<br>"); ("Connection : " + ("Connection") + "<br>"); ("Cookie : " + ("Cookie") + "<br>"); %>
Notes on ("Referer")
("Referer") Obtain the address of the visitor. Only throughLinkOnly when accessing the current page can you get the address of the previous page; otherwise ("Referer") value is Null, which is also Null by opening the current page or directly entering the address.
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 everyone and support me more~