Java passes in the username and password and automatically submits the form to implement the instance code for logging in to another system

  • 2020-06-03 06:21:47
  • OfStack

Instead of single sign-on, it simulates the login page form of the remote project, and automatically submits the form to the login action of this project when visiting this page, so as to realize login to other systems.

ssh Framework project

1. The following is the action code of the local system:


import java.io.IOException;
import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
public class myLoginAction {
  /**
   *  Check if the user is registered 
   * @return
   * @throws Exception 
   */
  public void checkUser() throws Exception{
    Loginer loginer = (Loginer) request.getSession()
    .getAttribute("loginer");
    String url = "http://www.youtest.com/login.php"; // Remote system login action address 
    String param = "username=Tom&password=123456"; // parameter 
    String temp = "alert(' Incorrect user name or password ');";  // Returned information, in this case an error message, for comparison purposes.    It depends 
    boolean result =false ;
    // Verify that the data is logged in 
    result = sendPost(url, param, temp);
    if(result){
      return "login";
    }else{
      return "register";
    }
    }
  // Access remote login action And get the returned information 
  public static boolean sendPost(String url, String param, String temp) {
      PrintWriter out = null;
      BufferedReader in = null;
      boolean result = true;
      try {
        URL realUrl = new URL(url);
          //  Open and URL The connection between 
          URLConnection conn = realUrl.openConnection();
          //  Sets the generic request properties 
          conn.setRequestProperty("accept", "*/*");
          conn.setRequestProperty("connection", "Keep-Alive");
          conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
          //  send POST The request must be set to the following two lines 
          conn.setDoOutput(true);
          conn.setDoInput(true);
          //  To obtain URLConnection Object corresponding output stream 
          out = new PrintWriter(conn.getOutputStream());
          //  Send request parameters 
          out.print(param);
          // flush Output stream buffering 
          out.flush();
          //  define BufferedReader Input stream to read URL The response of the 
          in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
          String line;
          while ((line = in.readLine()) != null) {
            if(temp.equals((line.trim()))) {
              System.out.println(" The wrong line:"+line);
              result = false;
            }
          }
      } catch (Exception e) {
        result = false;
        logger.error(" send  POST  Request exception! "+e);
          System.out.println(" send  POST  Request exception! "+e);
          e.printStackTrace();
      }finally{
          try{
            if(out!=null){
                out.close();
            }
            if(in!=null){
                in.close();
            }
          }catch(IOException ex){
            logger.error(ex);
            ex.printStackTrace();
          }
      }
      return result;
    } 
}

2. Simulated login page:


<html>
<head></head>
<body>
    <script type="text/javascript">
     var iframe = document.createElement("iframe");
     iframe.src = "http://www.youtest.com/login.php?UNAME=<%=userName%>&UPWD=<%=pwd%>";
     iframe.style.display="none";
     
     var sta="false;"
     if (iframe.attachEvent){
       iframe.attachEvent("onload", function(){
         window.location.href="http://www.youtest.com/index.html";
       });
     } else {
       iframe.onload = function(){
         window.location.href="http://www.youtest.com/index.html";
       };
     }
     document.body.appendChild(iframe);
 </script>
  </body>
</html>

Related articles: