Java to obtain HttpRequest header several methods (must see article) _java

Source: Internet
Author: User

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&lt;string, string&gt; Getheadersinfo () {map&lt;string, string&gt; Map = new H
    Ashmap&lt;string, string&gt; ();
    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

&lt;%//header.jsp out.println ("Protocol:" + request.getprotocol () + "&lt;br&gt;");
Out.println ("Scheme:" + request.getscheme () + "&lt;br&gt;");
Out.println ("Server Name:" + request.getservername () + "&lt;br&gt;");
Out.println ("Server Port:" + request.getserverport () + "&lt;br&gt;");
Out.println ("Protocol:" + request.getprotocol () + "&lt;br&gt;");
Out.println ("Server Info:" + getservletconfig (). Getservletcontext (). Getserverinfo () + "&lt;br&gt;");
Out.println ("Remote Addr:" + request.getremoteaddr () + "&lt;br&gt;");
Out.println ("Remote Host:" + request.getremotehost () + "&lt;br&gt;");
Out.println ("Character Encoding:" + request.getcharacterencoding () + "&lt;br&gt;");
Out.println ("Content Length:" + request.getcontentlength () + "&lt;br&gt;");
Out.println ("Content Type:" + request.getcontenttype () + "&lt;br&gt;");
Out.println ("Auth Type:" + request.getauthtype () + "&lt;br&gt;");
Out.println ("HTTP method:" + request.getmethod () + "&lt;br&gt;"); Out.println ("Path Info:" + reqUest.getpathinfo () + "&lt;br&gt;");
Out.println ("Path Trans:" + request.getpathtranslated () + "&lt;br&gt;");
Out.println ("Query String:" + request.getquerystring () + "&lt;br&gt;");
Out.println ("Remote User:" + request.getremoteuser () + "&lt;br&gt;");
Out.println ("Session Id:" + request.getrequestedsessionid () + "&lt;br&gt;");
Out.println ("Request URL:" + request.getrequesturl () + "&lt;br&gt;");
Out.println ("Request URI:" + request.getrequesturi () + "&lt;br&gt;");
Out.println ("Servlet Path:" + request.getservletpath () + "&lt;br&gt;");
Out.println ("Created:" + session.getcreationtime () + "&lt;br&gt;");

Out.println ("lastaccessed:" + session.getlastaccessedtime () + "&lt;br&gt;");
Out.println ("Accept:" + request.getheader ("Accept") + "&lt;br&gt;");
Out.println ("Host:" + Request.getheader ("host") + "&lt;br&gt;");
Out.println ("Referer:" + request.getheader ("Referer") + "&lt;br&gt;");
Out.println ("Accept-language:" + request.getheader ("accept-language") + "&lt;br&gt;"); Out.pRintln ("accept-encoding:" + request.getheader ("accept-encoding") + "&lt;br&gt;");
Out.println ("user-agent:" + request.getheader ("user-agent") + "&lt;br&gt;");
Out.println ("Connection:" + request.getheader ("Connection") + "&lt;br&gt;");
Out.println ("Cookie:" + request.getheader ("cookie") + "&lt;br&gt;"); %&gt;

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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.