title: java - How do I prevent cross-site forgery request
body: <p>So, I've been advised that the application I've begun working on has preventive measures in place for Cross-Site Forgery Request (CSFR) when it comes to post request, but NOT for get request. </p>
  
  <p>I've also been advised that I can obtain the source of the origin and the target of the origin, compare the host of each, and depending if they match or not do something. As of now, I currently have the below after doing some research, but I'm not there yet and could use some additional help. I'm very new to the profession, so I'm not even sure if I am on the right track.</p>
  
  <pre><code>    String refererHost = ((HttpServletRequest) request).getHeader("referer");
  
      String targetHost = request.getServerName();
  
      try {       
          refererHost = new URI(refererHost).getHost();
  
          if(!refererHost.equals(targetHost))
          {
              ((HttpServletResponse) response).sendRedirect( ((HttpServletRequest)request).getContextPath()+ADMIN_LOGOFF_URL);
          }
      } catch (MalformedURIException | NullPointerException e) {
          request.getRequestDispatcher(ADMIN_LOGOFF_URL).forward(request, response);
      }
  </code></pre>