In the process of developing an application, if there is more than one application, it is usually integrated through an portal portal, which is the portal to all applications, and requires similar single sign-on (SSO) Once the user enters another system after the portal logs in. into each subsystem, you do not need to log in again, of course, similar functions, you can through a professional single sign-on software to achieve, you can write your own database token and other ways to achieve. In fact, there is a relatively simple way, through the portal package has logged in the user's message, write to the HTTP header, and then the request forward to each subsystem, and each subsystem from the HTTP header to get the user name, As a checksum or a valid checksum that has been logged in.
Summarizes several ways to handle HTTP headers:
Using HttpServletRequest
Import Javax.servlet.http.HttpServletRequest;
//...
private HttpServletRequest request;
Get request Headers
private map<string, string> Getheadersinfo () {
map<string, string> Map = new Ha Shmap<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.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": "No",
"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
)
Use spring MVC to get an example of a 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 H
Ashmap<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 might say that HTTP headers can be modeled, so they can be constructed to deceive these systems, yes, indeed, so when it is worth it to use the HTTP header, it is important to remember that all requests must be handled by the portal and then forward To the subsystems, this problem will not occur. Because the portal first blocks all requests initiated by the user, if the constructed user, the sessiion in the portal is also not recorded, it will still jump to the login page, if recorded in the session of Protal, and also recorded in the Http Header. Then the subsystem is the legitimate user, and then you can handle the business logic according to some requirements.
Jsp/java Get HTTP header information (Request) example
<%//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>"); %>
A description of Request.getheader ("Referer")
Request.getheader ("Referer") gets the caller's address. The address of the previous page can only be obtained when the current page is accessed by a link , otherwise the value of Request.getheader ("Referer") is null, the current page is opened by window.open, or the address is entered directly, or null.
The above is a small series for you to bring Java to get HttpRequest header of several methods (must see article) of the entire content, hope to help everyone, a lot of support cloud Habitat Community ~